blob: 90d0d62bd0b40648406bd1db328c280fc572dbfe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02002 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/delay.h>
Clemens Ladisch102fa902006-10-11 12:05:59 +020022#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/interrupt.h>
25#include <linux/pci.h>
26#include <linux/sched.h>
27#include <linux/slab.h>
28#include <linux/vmalloc.h>
David Woodhouseb82a82d2008-05-29 14:40:00 +030029#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
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;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +020087 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020088 } while (time_before(jiffies, end_time));
Takashi Iwai99b359b2005-10-20 18:26:44 +020089 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 -070090 return -EBUSY;
91}
92
Takashi Iwai208a1b42005-11-17 14:53:41 +010093static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Takashi Iwai208a1b42005-11-17 14:53:41 +010095 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 u32 cmd;
97
98 snd_ymfpci_codec_ready(chip, 0);
99 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
100 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
101}
102
Takashi Iwai208a1b42005-11-17 14:53:41 +0100103static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100105 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 if (snd_ymfpci_codec_ready(chip, 0))
108 return ~0;
109 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
110 if (snd_ymfpci_codec_ready(chip, 0))
111 return ~0;
112 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
113 int i;
114 for (i = 0; i < 600; i++)
115 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
116 }
117 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
118}
119
120/*
121 * Misc routines
122 */
123
124static u32 snd_ymfpci_calc_delta(u32 rate)
125{
126 switch (rate) {
127 case 8000: return 0x02aaab00;
128 case 11025: return 0x03accd00;
129 case 16000: return 0x05555500;
130 case 22050: return 0x07599a00;
131 case 32000: return 0x0aaaab00;
132 case 44100: return 0x0eb33300;
133 default: return ((rate << 16) / 375) << 5;
134 }
135}
136
137static u32 def_rate[8] = {
138 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
139};
140
141static u32 snd_ymfpci_calc_lpfK(u32 rate)
142{
143 u32 i;
144 static u32 val[8] = {
145 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
146 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
147 };
148
149 if (rate == 44100)
150 return 0x40000000; /* FIXME: What's the right value? */
151 for (i = 0; i < 8; i++)
152 if (rate <= def_rate[i])
153 return val[i];
154 return val[0];
155}
156
157static u32 snd_ymfpci_calc_lpfQ(u32 rate)
158{
159 u32 i;
160 static u32 val[8] = {
161 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
162 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
163 };
164
165 if (rate == 44100)
166 return 0x370A0000;
167 for (i = 0; i < 8; i++)
168 if (rate <= def_rate[i])
169 return val[i];
170 return val[0];
171}
172
173/*
174 * Hardware start management
175 */
176
Takashi Iwai208a1b42005-11-17 14:53:41 +0100177static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 unsigned long flags;
180
181 spin_lock_irqsave(&chip->reg_lock, flags);
182 if (chip->start_count++ > 0)
183 goto __end;
184 snd_ymfpci_writel(chip, YDSXGR_MODE,
185 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
186 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
187 __end:
188 spin_unlock_irqrestore(&chip->reg_lock, flags);
189}
190
Takashi Iwai208a1b42005-11-17 14:53:41 +0100191static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 unsigned long flags;
194 long timeout = 1000;
195
196 spin_lock_irqsave(&chip->reg_lock, flags);
197 if (--chip->start_count > 0)
198 goto __end;
199 snd_ymfpci_writel(chip, YDSXGR_MODE,
200 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
201 while (timeout-- > 0) {
202 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
203 break;
204 }
205 if (atomic_read(&chip->interrupt_sleep_count)) {
206 atomic_set(&chip->interrupt_sleep_count, 0);
207 wake_up(&chip->interrupt_sleep);
208 }
209 __end:
210 spin_unlock_irqrestore(&chip->reg_lock, flags);
211}
212
213/*
214 * Playback voice management
215 */
216
Takashi Iwai208a1b42005-11-17 14:53:41 +0100217static int voice_alloc(struct snd_ymfpci *chip,
218 enum snd_ymfpci_voice_type type, int pair,
219 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100221 struct snd_ymfpci_voice *voice, *voice2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int idx;
223
224 *rvoice = NULL;
225 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
226 voice = &chip->voices[idx];
227 voice2 = pair ? &chip->voices[idx+1] : NULL;
228 if (voice->use || (voice2 && voice2->use))
229 continue;
230 voice->use = 1;
231 if (voice2)
232 voice2->use = 1;
233 switch (type) {
234 case YMFPCI_PCM:
235 voice->pcm = 1;
236 if (voice2)
237 voice2->pcm = 1;
238 break;
239 case YMFPCI_SYNTH:
240 voice->synth = 1;
241 break;
242 case YMFPCI_MIDI:
243 voice->midi = 1;
244 break;
245 }
246 snd_ymfpci_hw_start(chip);
247 if (voice2)
248 snd_ymfpci_hw_start(chip);
249 *rvoice = voice;
250 return 0;
251 }
252 return -ENOMEM;
253}
254
Takashi Iwai208a1b42005-11-17 14:53:41 +0100255static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
256 enum snd_ymfpci_voice_type type, int pair,
257 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 unsigned long flags;
260 int result;
261
Takashi Iwaida3cec32008-08-08 17:12:14 +0200262 if (snd_BUG_ON(!rvoice))
263 return -EINVAL;
264 if (snd_BUG_ON(pair && type != YMFPCI_PCM))
265 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 spin_lock_irqsave(&chip->voice_lock, flags);
268 for (;;) {
269 result = voice_alloc(chip, type, pair, rvoice);
270 if (result == 0 || type != YMFPCI_PCM)
271 break;
272 /* TODO: synth/midi voice deallocation */
273 break;
274 }
275 spin_unlock_irqrestore(&chip->voice_lock, flags);
276 return result;
277}
278
Takashi Iwai208a1b42005-11-17 14:53:41 +0100279static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 unsigned long flags;
282
Takashi Iwaida3cec32008-08-08 17:12:14 +0200283 if (snd_BUG_ON(!pvoice))
284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 snd_ymfpci_hw_stop(chip);
286 spin_lock_irqsave(&chip->voice_lock, flags);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100287 if (pvoice->number == chip->src441_used) {
288 chip->src441_used = -1;
289 pvoice->ypcm->use_441_slot = 0;
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
292 pvoice->ypcm = NULL;
293 pvoice->interrupt = NULL;
294 spin_unlock_irqrestore(&chip->voice_lock, flags);
295 return 0;
296}
297
298/*
299 * PCM part
300 */
301
Takashi Iwai208a1b42005-11-17 14:53:41 +0100302static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100304 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 u32 pos, delta;
306
307 if ((ypcm = voice->ypcm) == NULL)
308 return;
309 if (ypcm->substream == NULL)
310 return;
311 spin_lock(&chip->reg_lock);
312 if (ypcm->running) {
313 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
314 if (pos < ypcm->last_pos)
315 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
316 else
317 delta = pos - ypcm->last_pos;
318 ypcm->period_pos += delta;
319 ypcm->last_pos = pos;
320 if (ypcm->period_pos >= ypcm->period_size) {
321 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
322 ypcm->period_pos %= ypcm->period_size;
323 spin_unlock(&chip->reg_lock);
324 snd_pcm_period_elapsed(ypcm->substream);
325 spin_lock(&chip->reg_lock);
326 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200327
328 if (unlikely(ypcm->update_pcm_vol)) {
329 unsigned int subs = ypcm->substream->number;
330 unsigned int next_bank = 1 - chip->active_bank;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100331 struct snd_ymfpci_playback_bank *bank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200332 u32 volume;
333
334 bank = &voice->bank[next_bank];
335 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
336 bank->left_gain_end = volume;
337 if (ypcm->output_rear)
338 bank->eff2_gain_end = volume;
339 if (ypcm->voices[1])
340 bank = &ypcm->voices[1]->bank[next_bank];
341 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
342 bank->right_gain_end = volume;
343 if (ypcm->output_rear)
344 bank->eff3_gain_end = volume;
345 ypcm->update_pcm_vol--;
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 spin_unlock(&chip->reg_lock);
349}
350
Takashi Iwai208a1b42005-11-17 14:53:41 +0100351static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100353 struct snd_pcm_runtime *runtime = substream->runtime;
354 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
355 struct snd_ymfpci *chip = ypcm->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 u32 pos, delta;
357
358 spin_lock(&chip->reg_lock);
359 if (ypcm->running) {
360 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
361 if (pos < ypcm->last_pos)
362 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
363 else
364 delta = pos - ypcm->last_pos;
365 ypcm->period_pos += delta;
366 ypcm->last_pos = pos;
367 if (ypcm->period_pos >= ypcm->period_size) {
368 ypcm->period_pos %= ypcm->period_size;
369 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
370 spin_unlock(&chip->reg_lock);
371 snd_pcm_period_elapsed(substream);
372 spin_lock(&chip->reg_lock);
373 }
374 }
375 spin_unlock(&chip->reg_lock);
376}
377
Takashi Iwai208a1b42005-11-17 14:53:41 +0100378static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int cmd)
380{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100381 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
382 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200383 struct snd_kcontrol *kctl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 int result = 0;
385
386 spin_lock(&chip->reg_lock);
387 if (ypcm->voices[0] == NULL) {
388 result = -EINVAL;
389 goto __unlock;
390 }
391 switch (cmd) {
392 case SNDRV_PCM_TRIGGER_START:
393 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
394 case SNDRV_PCM_TRIGGER_RESUME:
395 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100396 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
398 ypcm->running = 1;
399 break;
400 case SNDRV_PCM_TRIGGER_STOP:
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200401 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
402 kctl = chip->pcm_mixer[substream->number].ctl;
403 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
404 }
405 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
407 case SNDRV_PCM_TRIGGER_SUSPEND:
408 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100409 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
411 ypcm->running = 0;
412 break;
413 default:
414 result = -EINVAL;
415 break;
416 }
417 __unlock:
418 spin_unlock(&chip->reg_lock);
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200419 if (kctl)
420 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return result;
422}
Takashi Iwai208a1b42005-11-17 14:53:41 +0100423static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 int cmd)
425{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100426 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
427 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 int result = 0;
429 u32 tmp;
430
431 spin_lock(&chip->reg_lock);
432 switch (cmd) {
433 case SNDRV_PCM_TRIGGER_START:
434 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
435 case SNDRV_PCM_TRIGGER_RESUME:
436 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
437 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
438 ypcm->running = 1;
439 break;
440 case SNDRV_PCM_TRIGGER_STOP:
441 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
442 case SNDRV_PCM_TRIGGER_SUSPEND:
443 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
444 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
445 ypcm->running = 0;
446 break;
447 default:
448 result = -EINVAL;
449 break;
450 }
451 spin_unlock(&chip->reg_lock);
452 return result;
453}
454
Takashi Iwai208a1b42005-11-17 14:53:41 +0100455static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 int err;
458
459 if (ypcm->voices[1] != NULL && voices < 2) {
460 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
461 ypcm->voices[1] = NULL;
462 }
463 if (voices == 1 && ypcm->voices[0] != NULL)
464 return 0; /* already allocated */
465 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
466 return 0; /* already allocated */
467 if (voices > 1) {
468 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
469 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
470 ypcm->voices[0] = NULL;
471 }
472 }
473 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
474 if (err < 0)
475 return err;
476 ypcm->voices[0]->ypcm = ypcm;
477 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
478 if (voices > 1) {
479 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
480 ypcm->voices[1]->ypcm = ypcm;
481 }
482 return 0;
483}
484
Takashi Iwai208a1b42005-11-17 14:53:41 +0100485static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
486 struct snd_pcm_runtime *runtime,
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200487 int has_pcm_volume)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100489 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 u32 format;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200491 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
492 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
493 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
Takashi Iwai208a1b42005-11-17 14:53:41 +0100494 struct snd_ymfpci_playback_bank *bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 unsigned int nbank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200496 u32 vol_left, vol_right;
497 u8 use_left, use_right;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100498 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Takashi Iwaida3cec32008-08-08 17:12:14 +0200500 if (snd_BUG_ON(!voice))
501 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;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100529 }
530 if (ypcm->chip->src441_used == voice->number &&
531 (format & 0x10000000) == 0) {
532 ypcm->chip->src441_used = -1;
533 ypcm->use_441_slot = 0;
534 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200535 if (runtime->channels == 2 && (voiceidx & 1) != 0)
536 format |= 1;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100537 spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 for (nbank = 0; nbank < 2; nbank++) {
539 bank = &voice->bank[nbank];
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200540 memset(bank, 0, sizeof(*bank));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 bank->format = cpu_to_le32(format);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200542 bank->base = cpu_to_le32(runtime->dma_addr);
543 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 bank->lpfQ = cpu_to_le32(lpfQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 bank->delta =
546 bank->delta_end = cpu_to_le32(delta);
547 bank->lpfK =
548 bank->lpfK_end = cpu_to_le32(lpfK);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200549 bank->eg_gain =
550 bank->eg_gain_end = cpu_to_le32(0x40000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200552 if (ypcm->output_front) {
553 if (use_left) {
554 bank->left_gain =
555 bank->left_gain_end = vol_left;
556 }
557 if (use_right) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 bank->right_gain =
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200559 bank->right_gain_end = vol_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200561 }
562 if (ypcm->output_rear) {
Jaroslav Kysela5a25c5c2006-01-18 08:02:24 +0100563 if (!ypcm->swap_rear) {
564 if (use_left) {
565 bank->eff2_gain =
566 bank->eff2_gain_end = vol_left;
567 }
568 if (use_right) {
569 bank->eff3_gain =
570 bank->eff3_gain_end = vol_right;
571 }
572 } else {
573 /* The SPDIF out channels seem to be swapped, so we have
574 * to swap them here, too. The rear analog out channels
575 * will be wrong, but otherwise AC3 would not work.
576 */
577 if (use_left) {
578 bank->eff3_gain =
579 bank->eff3_gain_end = vol_left;
580 }
581 if (use_right) {
582 bank->eff2_gain =
583 bank->eff2_gain_end = vol_right;
584 }
585 }
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588}
589
Takashi Iwai208a1b42005-11-17 14:53:41 +0100590static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
593 4096, &chip->ac3_tmp_base) < 0)
594 return -ENOMEM;
595
596 chip->bank_effect[3][0]->base =
597 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
598 chip->bank_effect[3][0]->loop_end =
599 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
600 chip->bank_effect[4][0]->base =
601 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
602 chip->bank_effect[4][0]->loop_end =
603 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
604
605 spin_lock_irq(&chip->reg_lock);
606 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
607 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
608 spin_unlock_irq(&chip->reg_lock);
609 return 0;
610}
611
Takashi Iwai208a1b42005-11-17 14:53:41 +0100612static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 spin_lock_irq(&chip->reg_lock);
615 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
616 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
617 spin_unlock_irq(&chip->reg_lock);
618 // snd_ymfpci_irq_wait(chip);
619 if (chip->ac3_tmp_base.area) {
620 snd_dma_free_pages(&chip->ac3_tmp_base);
621 chip->ac3_tmp_base.area = NULL;
622 }
623 return 0;
624}
625
Takashi Iwai208a1b42005-11-17 14:53:41 +0100626static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
627 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100629 struct snd_pcm_runtime *runtime = substream->runtime;
630 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int err;
632
633 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
634 return err;
635 if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
636 return err;
637 return 0;
638}
639
Takashi Iwai208a1b42005-11-17 14:53:41 +0100640static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100642 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
643 struct snd_pcm_runtime *runtime = substream->runtime;
644 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 if (runtime->private_data == NULL)
647 return 0;
648 ypcm = runtime->private_data;
649
650 /* wait, until the PCI operations are not finished */
651 snd_ymfpci_irq_wait(chip);
652 snd_pcm_lib_free_pages(substream);
653 if (ypcm->voices[1]) {
654 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
655 ypcm->voices[1] = NULL;
656 }
657 if (ypcm->voices[0]) {
658 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
659 ypcm->voices[0] = NULL;
660 }
661 return 0;
662}
663
Takashi Iwai208a1b42005-11-17 14:53:41 +0100664static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100666 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
667 struct snd_pcm_runtime *runtime = substream->runtime;
668 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200669 struct snd_kcontrol *kctl;
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);
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200679
680 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
681 kctl = chip->pcm_mixer[substream->number].ctl;
682 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
683 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return 0;
686}
687
Takashi Iwai208a1b42005-11-17 14:53:41 +0100688static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
689 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
692}
693
Takashi Iwai208a1b42005-11-17 14:53:41 +0100694static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100696 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* wait, until the PCI operations are not finished */
699 snd_ymfpci_irq_wait(chip);
700 return snd_pcm_lib_free_pages(substream);
701}
702
Takashi Iwai208a1b42005-11-17 14:53:41 +0100703static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100705 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
706 struct snd_pcm_runtime *runtime = substream->runtime;
707 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
708 struct snd_ymfpci_capture_bank * bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int nbank;
710 u32 rate, format;
711
712 ypcm->period_size = runtime->period_size;
713 ypcm->buffer_size = runtime->buffer_size;
714 ypcm->period_pos = 0;
715 ypcm->last_pos = 0;
716 ypcm->shift = 0;
717 rate = ((48000 * 4096) / runtime->rate) - 1;
718 format = 0;
719 if (runtime->channels == 2) {
720 format |= 2;
721 ypcm->shift++;
722 }
723 if (snd_pcm_format_width(runtime->format) == 8)
724 format |= 1;
725 else
726 ypcm->shift++;
727 switch (ypcm->capture_bank_number) {
728 case 0:
729 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
730 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
731 break;
732 case 1:
733 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
734 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
735 break;
736 }
737 for (nbank = 0; nbank < 2; nbank++) {
738 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
739 bank->base = cpu_to_le32(runtime->dma_addr);
740 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
741 bank->start = 0;
742 bank->num_of_loops = 0;
743 }
744 return 0;
745}
746
Takashi Iwai208a1b42005-11-17 14:53:41 +0100747static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100749 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
750 struct snd_pcm_runtime *runtime = substream->runtime;
751 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
752 struct snd_ymfpci_voice *voice = ypcm->voices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (!(ypcm->running && voice))
755 return 0;
756 return le32_to_cpu(voice->bank[chip->active_bank].start);
757}
758
Takashi Iwai208a1b42005-11-17 14:53:41 +0100759static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100761 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
762 struct snd_pcm_runtime *runtime = substream->runtime;
763 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 if (!ypcm->running)
766 return 0;
767 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
768}
769
Takashi Iwai208a1b42005-11-17 14:53:41 +0100770static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 wait_queue_t wait;
773 int loops = 4;
774
775 while (loops-- > 0) {
776 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
777 continue;
778 init_waitqueue_entry(&wait, current);
779 add_wait_queue(&chip->interrupt_sleep, &wait);
780 atomic_inc(&chip->interrupt_sleep_count);
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200781 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 remove_wait_queue(&chip->interrupt_sleep, &wait);
783 }
784}
785
David Howells7d12e782006-10-05 14:55:46 +0100786static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100788 struct snd_ymfpci *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 u32 status, nvoice, mode;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100790 struct snd_ymfpci_voice *voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
793 if (status & 0x80000000) {
794 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
795 spin_lock(&chip->voice_lock);
796 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
797 voice = &chip->voices[nvoice];
798 if (voice->interrupt)
799 voice->interrupt(chip, voice);
800 }
801 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
802 if (chip->capture_substream[nvoice])
803 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
804 }
805#if 0
806 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
807 if (chip->effect_substream[nvoice])
808 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
809 }
810#endif
811 spin_unlock(&chip->voice_lock);
812 spin_lock(&chip->reg_lock);
813 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
814 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
815 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
816 spin_unlock(&chip->reg_lock);
817
818 if (atomic_read(&chip->interrupt_sleep_count)) {
819 atomic_set(&chip->interrupt_sleep_count, 0);
820 wake_up(&chip->interrupt_sleep);
821 }
822 }
823
824 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
825 if (status & 1) {
826 if (chip->timer)
827 snd_timer_interrupt(chip->timer, chip->timer->sticks);
828 }
829 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
830
831 if (chip->rawmidi)
David Howells7d12e782006-10-05 14:55:46 +0100832 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return IRQ_HANDLED;
834}
835
Takashi Iwai208a1b42005-11-17 14:53:41 +0100836static struct snd_pcm_hardware snd_ymfpci_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 .info = (SNDRV_PCM_INFO_MMAP |
839 SNDRV_PCM_INFO_MMAP_VALID |
840 SNDRV_PCM_INFO_INTERLEAVED |
841 SNDRV_PCM_INFO_BLOCK_TRANSFER |
842 SNDRV_PCM_INFO_PAUSE |
843 SNDRV_PCM_INFO_RESUME),
844 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
845 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
846 .rate_min = 8000,
847 .rate_max = 48000,
848 .channels_min = 1,
849 .channels_max = 2,
850 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
851 .period_bytes_min = 64,
852 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
853 .periods_min = 3,
854 .periods_max = 1024,
855 .fifo_size = 0,
856};
857
Takashi Iwai208a1b42005-11-17 14:53:41 +0100858static struct snd_pcm_hardware snd_ymfpci_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 .info = (SNDRV_PCM_INFO_MMAP |
861 SNDRV_PCM_INFO_MMAP_VALID |
862 SNDRV_PCM_INFO_INTERLEAVED |
863 SNDRV_PCM_INFO_BLOCK_TRANSFER |
864 SNDRV_PCM_INFO_PAUSE |
865 SNDRV_PCM_INFO_RESUME),
866 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
867 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
868 .rate_min = 8000,
869 .rate_max = 48000,
870 .channels_min = 1,
871 .channels_max = 2,
872 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
873 .period_bytes_min = 64,
874 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
875 .periods_min = 3,
876 .periods_max = 1024,
877 .fifo_size = 0,
878};
879
Takashi Iwai208a1b42005-11-17 14:53:41 +0100880static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Jesper Juhl4d572772005-05-30 17:30:32 +0200882 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Takashi Iwai208a1b42005-11-17 14:53:41 +0100885static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100887 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
888 struct snd_pcm_runtime *runtime = substream->runtime;
889 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200891 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (ypcm == NULL)
893 return -ENOMEM;
894 ypcm->chip = chip;
895 ypcm->type = PLAYBACK_VOICE;
896 ypcm->substream = substream;
897 runtime->hw = snd_ymfpci_playback;
898 runtime->private_data = ypcm;
899 runtime->private_free = snd_ymfpci_pcm_free_substream;
900 /* FIXME? True value is 256/48 = 5.33333 ms */
901 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
902 return 0;
903}
904
905/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100906static void ymfpci_open_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 if (! chip->rear_opened) {
909 if (! chip->spdif_opened) /* set AC3 */
910 snd_ymfpci_writel(chip, YDSXGR_MODE,
911 snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
912 /* enable second codec (4CHEN) */
913 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
914 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
915 }
916}
917
918/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100919static void ymfpci_close_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 if (! chip->rear_opened) {
922 if (! chip->spdif_opened)
923 snd_ymfpci_writel(chip, YDSXGR_MODE,
924 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
925 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
926 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
927 }
928}
929
Takashi Iwai208a1b42005-11-17 14:53:41 +0100930static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100932 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
933 struct snd_pcm_runtime *runtime = substream->runtime;
934 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 int err;
936
937 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
938 return err;
939 ypcm = runtime->private_data;
940 ypcm->output_front = 1;
941 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
Glen Masgaid9301262006-10-10 09:27:19 +0200942 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 spin_lock_irq(&chip->reg_lock);
944 if (ypcm->output_rear) {
945 ymfpci_open_extension(chip);
946 chip->rear_opened++;
947 }
948 spin_unlock_irq(&chip->reg_lock);
949 return 0;
950}
951
Takashi Iwai208a1b42005-11-17 14:53:41 +0100952static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100954 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
955 struct snd_pcm_runtime *runtime = substream->runtime;
956 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 int err;
958
959 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
960 return err;
961 ypcm = runtime->private_data;
962 ypcm->output_front = 0;
963 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200964 ypcm->swap_rear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 spin_lock_irq(&chip->reg_lock);
966 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
967 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
968 ymfpci_open_extension(chip);
969 chip->spdif_pcm_bits = chip->spdif_bits;
970 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
971 chip->spdif_opened++;
972 spin_unlock_irq(&chip->reg_lock);
973
974 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
975 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
976 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
977 return 0;
978}
979
Takashi Iwai208a1b42005-11-17 14:53:41 +0100980static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100982 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
983 struct snd_pcm_runtime *runtime = substream->runtime;
984 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int err;
986
987 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
988 return err;
989 ypcm = runtime->private_data;
990 ypcm->output_front = 0;
991 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200992 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 spin_lock_irq(&chip->reg_lock);
994 ymfpci_open_extension(chip);
995 chip->rear_opened++;
996 spin_unlock_irq(&chip->reg_lock);
997 return 0;
998}
999
Takashi Iwai208a1b42005-11-17 14:53:41 +01001000static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 u32 capture_bank_number)
1002{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001003 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1004 struct snd_pcm_runtime *runtime = substream->runtime;
1005 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001007 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (ypcm == NULL)
1009 return -ENOMEM;
1010 ypcm->chip = chip;
1011 ypcm->type = capture_bank_number + CAPTURE_REC;
1012 ypcm->substream = substream;
1013 ypcm->capture_bank_number = capture_bank_number;
1014 chip->capture_substream[capture_bank_number] = substream;
1015 runtime->hw = snd_ymfpci_capture;
1016 /* FIXME? True value is 256/48 = 5.33333 ms */
1017 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
1018 runtime->private_data = ypcm;
1019 runtime->private_free = snd_ymfpci_pcm_free_substream;
1020 snd_ymfpci_hw_start(chip);
1021 return 0;
1022}
1023
Takashi Iwai208a1b42005-11-17 14:53:41 +01001024static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 return snd_ymfpci_capture_open(substream, 0);
1027}
1028
Takashi Iwai208a1b42005-11-17 14:53:41 +01001029static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 return snd_ymfpci_capture_open(substream, 1);
1032}
1033
Takashi Iwai208a1b42005-11-17 14:53:41 +01001034static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 return 0;
1037}
1038
Takashi Iwai208a1b42005-11-17 14:53:41 +01001039static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001041 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1042 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
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);
1050 return snd_ymfpci_playback_close_1(substream);
1051}
1052
Takashi Iwai208a1b42005-11-17 14:53:41 +01001053static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001055 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 spin_lock_irq(&chip->reg_lock);
1058 chip->spdif_opened = 0;
1059 ymfpci_close_extension(chip);
1060 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1061 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1062 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1063 spin_unlock_irq(&chip->reg_lock);
1064 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1065 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1066 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1067 return snd_ymfpci_playback_close_1(substream);
1068}
1069
Takashi Iwai208a1b42005-11-17 14:53:41 +01001070static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001072 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 spin_lock_irq(&chip->reg_lock);
1075 if (chip->rear_opened > 0) {
1076 chip->rear_opened--;
1077 ymfpci_close_extension(chip);
1078 }
1079 spin_unlock_irq(&chip->reg_lock);
1080 return snd_ymfpci_playback_close_1(substream);
1081}
1082
Takashi Iwai208a1b42005-11-17 14:53:41 +01001083static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001085 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1086 struct snd_pcm_runtime *runtime = substream->runtime;
1087 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 if (ypcm != NULL) {
1090 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1091 snd_ymfpci_hw_stop(chip);
1092 }
1093 return 0;
1094}
1095
Takashi Iwai208a1b42005-11-17 14:53:41 +01001096static struct snd_pcm_ops snd_ymfpci_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 .open = snd_ymfpci_playback_open,
1098 .close = snd_ymfpci_playback_close,
1099 .ioctl = snd_pcm_lib_ioctl,
1100 .hw_params = snd_ymfpci_playback_hw_params,
1101 .hw_free = snd_ymfpci_playback_hw_free,
1102 .prepare = snd_ymfpci_playback_prepare,
1103 .trigger = snd_ymfpci_playback_trigger,
1104 .pointer = snd_ymfpci_playback_pointer,
1105};
1106
Takashi Iwai208a1b42005-11-17 14:53:41 +01001107static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 .open = snd_ymfpci_capture_rec_open,
1109 .close = snd_ymfpci_capture_close,
1110 .ioctl = snd_pcm_lib_ioctl,
1111 .hw_params = snd_ymfpci_capture_hw_params,
1112 .hw_free = snd_ymfpci_capture_hw_free,
1113 .prepare = snd_ymfpci_capture_prepare,
1114 .trigger = snd_ymfpci_capture_trigger,
1115 .pointer = snd_ymfpci_capture_pointer,
1116};
1117
Takashi Iwai208a1b42005-11-17 14:53:41 +01001118int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001120 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int err;
1122
1123 if (rpcm)
1124 *rpcm = NULL;
1125 if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1126 return err;
1127 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1130 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1131
1132 /* global setup */
1133 pcm->info_flags = 0;
1134 strcpy(pcm->name, "YMFPCI");
1135 chip->pcm = pcm;
1136
1137 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1138 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1139
1140 if (rpcm)
1141 *rpcm = pcm;
1142 return 0;
1143}
1144
Takashi Iwai208a1b42005-11-17 14:53:41 +01001145static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 .open = snd_ymfpci_capture_ac97_open,
1147 .close = snd_ymfpci_capture_close,
1148 .ioctl = snd_pcm_lib_ioctl,
1149 .hw_params = snd_ymfpci_capture_hw_params,
1150 .hw_free = snd_ymfpci_capture_hw_free,
1151 .prepare = snd_ymfpci_capture_prepare,
1152 .trigger = snd_ymfpci_capture_trigger,
1153 .pointer = snd_ymfpci_capture_pointer,
1154};
1155
Takashi Iwai208a1b42005-11-17 14:53:41 +01001156int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001158 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int err;
1160
1161 if (rpcm)
1162 *rpcm = NULL;
1163 if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1164 return err;
1165 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1168
1169 /* global setup */
1170 pcm->info_flags = 0;
1171 sprintf(pcm->name, "YMFPCI - %s",
1172 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1173 chip->pcm2 = pcm;
1174
1175 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1176 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1177
1178 if (rpcm)
1179 *rpcm = pcm;
1180 return 0;
1181}
1182
Takashi Iwai208a1b42005-11-17 14:53:41 +01001183static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 .open = snd_ymfpci_playback_spdif_open,
1185 .close = snd_ymfpci_playback_spdif_close,
1186 .ioctl = snd_pcm_lib_ioctl,
1187 .hw_params = snd_ymfpci_playback_hw_params,
1188 .hw_free = snd_ymfpci_playback_hw_free,
1189 .prepare = snd_ymfpci_playback_prepare,
1190 .trigger = snd_ymfpci_playback_trigger,
1191 .pointer = snd_ymfpci_playback_pointer,
1192};
1193
Takashi Iwai208a1b42005-11-17 14:53:41 +01001194int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001196 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 int err;
1198
1199 if (rpcm)
1200 *rpcm = NULL;
1201 if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1202 return err;
1203 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1206
1207 /* global setup */
1208 pcm->info_flags = 0;
1209 strcpy(pcm->name, "YMFPCI - IEC958");
1210 chip->pcm_spdif = pcm;
1211
1212 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1213 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1214
1215 if (rpcm)
1216 *rpcm = pcm;
1217 return 0;
1218}
1219
Takashi Iwai208a1b42005-11-17 14:53:41 +01001220static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 .open = snd_ymfpci_playback_4ch_open,
1222 .close = snd_ymfpci_playback_4ch_close,
1223 .ioctl = snd_pcm_lib_ioctl,
1224 .hw_params = snd_ymfpci_playback_hw_params,
1225 .hw_free = snd_ymfpci_playback_hw_free,
1226 .prepare = snd_ymfpci_playback_prepare,
1227 .trigger = snd_ymfpci_playback_trigger,
1228 .pointer = snd_ymfpci_playback_pointer,
1229};
1230
Takashi Iwai208a1b42005-11-17 14:53:41 +01001231int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001233 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 int err;
1235
1236 if (rpcm)
1237 *rpcm = NULL;
1238 if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1239 return err;
1240 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1243
1244 /* global setup */
1245 pcm->info_flags = 0;
1246 strcpy(pcm->name, "YMFPCI - Rear PCM");
1247 chip->pcm_4ch = pcm;
1248
1249 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1250 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1251
1252 if (rpcm)
1253 *rpcm = pcm;
1254 return 0;
1255}
1256
Takashi Iwai208a1b42005-11-17 14:53:41 +01001257static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1260 uinfo->count = 1;
1261 return 0;
1262}
1263
Takashi Iwai208a1b42005-11-17 14:53:41 +01001264static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1265 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001267 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 spin_lock_irq(&chip->reg_lock);
1270 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1271 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001272 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 spin_unlock_irq(&chip->reg_lock);
1274 return 0;
1275}
1276
Takashi Iwai208a1b42005-11-17 14:53:41 +01001277static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1278 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001280 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 unsigned int val;
1282 int change;
1283
1284 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1285 (ucontrol->value.iec958.status[1] << 8);
1286 spin_lock_irq(&chip->reg_lock);
1287 change = chip->spdif_bits != val;
1288 chip->spdif_bits = val;
1289 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1290 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1291 spin_unlock_irq(&chip->reg_lock);
1292 return change;
1293}
1294
Takashi Iwai208a1b42005-11-17 14:53:41 +01001295static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1298 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1299 .info = snd_ymfpci_spdif_default_info,
1300 .get = snd_ymfpci_spdif_default_get,
1301 .put = snd_ymfpci_spdif_default_put
1302};
1303
Takashi Iwai208a1b42005-11-17 14:53:41 +01001304static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1307 uinfo->count = 1;
1308 return 0;
1309}
1310
Takashi Iwai208a1b42005-11-17 14:53:41 +01001311static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1312 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001314 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 spin_lock_irq(&chip->reg_lock);
1317 ucontrol->value.iec958.status[0] = 0x3e;
1318 ucontrol->value.iec958.status[1] = 0xff;
1319 spin_unlock_irq(&chip->reg_lock);
1320 return 0;
1321}
1322
Takashi Iwai208a1b42005-11-17 14:53:41 +01001323static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1326 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1327 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1328 .info = snd_ymfpci_spdif_mask_info,
1329 .get = snd_ymfpci_spdif_mask_get,
1330};
1331
Takashi Iwai208a1b42005-11-17 14:53:41 +01001332static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1335 uinfo->count = 1;
1336 return 0;
1337}
1338
Takashi Iwai208a1b42005-11-17 14:53:41 +01001339static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1340 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001342 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 spin_lock_irq(&chip->reg_lock);
1345 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1346 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001347 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 spin_unlock_irq(&chip->reg_lock);
1349 return 0;
1350}
1351
Takashi Iwai208a1b42005-11-17 14:53:41 +01001352static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1353 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001355 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 unsigned int val;
1357 int change;
1358
1359 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1360 (ucontrol->value.iec958.status[1] << 8);
1361 spin_lock_irq(&chip->reg_lock);
1362 change = chip->spdif_pcm_bits != val;
1363 chip->spdif_pcm_bits = val;
1364 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1365 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1366 spin_unlock_irq(&chip->reg_lock);
1367 return change;
1368}
1369
Takashi Iwai208a1b42005-11-17 14:53:41 +01001370static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1373 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1374 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1375 .info = snd_ymfpci_spdif_stream_info,
1376 .get = snd_ymfpci_spdif_stream_get,
1377 .put = snd_ymfpci_spdif_stream_put
1378};
1379
Takashi Iwai208a1b42005-11-17 14:53:41 +01001380static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};
1383
1384 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1385 info->count = 1;
1386 info->value.enumerated.items = 3;
1387 if (info->value.enumerated.item > 2)
1388 info->value.enumerated.item = 2;
1389 strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
1390 return 0;
1391}
1392
Takashi Iwai208a1b42005-11-17 14:53:41 +01001393static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001395 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 u16 reg;
1397
1398 spin_lock_irq(&chip->reg_lock);
1399 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1400 spin_unlock_irq(&chip->reg_lock);
1401 if (!(reg & 0x100))
1402 value->value.enumerated.item[0] = 0;
1403 else
1404 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1405 return 0;
1406}
1407
Takashi Iwai208a1b42005-11-17 14:53:41 +01001408static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001410 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 u16 reg, old_reg;
1412
1413 spin_lock_irq(&chip->reg_lock);
1414 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1415 if (value->value.enumerated.item[0] == 0)
1416 reg = old_reg & ~0x100;
1417 else
1418 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1419 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1420 spin_unlock_irq(&chip->reg_lock);
1421 return reg != old_reg;
1422}
1423
Takashi Iwai208a1b42005-11-17 14:53:41 +01001424static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1426 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1427 .name = "Direct Recording Source",
1428 .info = snd_ymfpci_drec_source_info,
1429 .get = snd_ymfpci_drec_source_get,
1430 .put = snd_ymfpci_drec_source_put
1431};
1432
1433/*
1434 * Mixer controls
1435 */
1436
Glen Masgaid602c882005-09-28 08:19:05 +02001437#define YMFPCI_SINGLE(xname, xindex, reg, shift) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1439 .info = snd_ymfpci_info_single, \
1440 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
Glen Masgaid602c882005-09-28 08:19:05 +02001441 .private_value = ((reg) | ((shift) << 16)) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001443#define snd_ymfpci_info_single snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Takashi Iwai208a1b42005-11-17 14:53:41 +01001445static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1446 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001448 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001449 int reg = kcontrol->private_value & 0xffff;
1450 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1451 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Glen Masgaid602c882005-09-28 08:19:05 +02001453 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 case YDSXGR_SPDIFOUTCTRL: break;
1455 case YDSXGR_SPDIFINCTRL: break;
1456 default: return -EINVAL;
1457 }
Glen Masgaid602c882005-09-28 08:19:05 +02001458 ucontrol->value.integer.value[0] =
1459 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return 0;
1461}
1462
Takashi Iwai208a1b42005-11-17 14:53:41 +01001463static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1464 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001466 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001467 int reg = kcontrol->private_value & 0xffff;
1468 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1469 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 int change;
1471 unsigned int val, oval;
1472
Glen Masgaid602c882005-09-28 08:19:05 +02001473 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 case YDSXGR_SPDIFOUTCTRL: break;
1475 case YDSXGR_SPDIFINCTRL: break;
1476 default: return -EINVAL;
1477 }
1478 val = (ucontrol->value.integer.value[0] & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 val <<= shift;
1480 spin_lock_irq(&chip->reg_lock);
1481 oval = snd_ymfpci_readl(chip, reg);
1482 val = (oval & ~(mask << shift)) | val;
1483 change = val != oval;
1484 snd_ymfpci_writel(chip, reg, val);
1485 spin_unlock_irq(&chip->reg_lock);
1486 return change;
1487}
1488
Takashi Iwai0cb29ea2007-01-29 15:33:49 +01001489static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
Takashi Iwai33925182006-08-28 13:29:42 +02001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491#define YMFPCI_DOUBLE(xname, xindex, reg) \
1492{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Takashi Iwai33925182006-08-28 13:29:42 +02001493 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 .info = snd_ymfpci_info_double, \
1495 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
Takashi Iwai33925182006-08-28 13:29:42 +02001496 .private_value = reg, \
1497 .tlv = { .p = db_scale_native } }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Takashi Iwai208a1b42005-11-17 14:53:41 +01001499static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 unsigned int reg = kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 if (reg < 0x80 || reg >= 0xc0)
1504 return -EINVAL;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001505 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 uinfo->count = 2;
1507 uinfo->value.integer.min = 0;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001508 uinfo->value.integer.max = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return 0;
1510}
1511
Takashi Iwai208a1b42005-11-17 14:53:41 +01001512static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001514 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001516 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 unsigned int val;
1518
1519 if (reg < 0x80 || reg >= 0xc0)
1520 return -EINVAL;
1521 spin_lock_irq(&chip->reg_lock);
1522 val = snd_ymfpci_readl(chip, reg);
1523 spin_unlock_irq(&chip->reg_lock);
1524 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1525 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return 0;
1527}
1528
Takashi Iwai208a1b42005-11-17 14:53:41 +01001529static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001531 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001533 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 int change;
1535 unsigned int val1, val2, oval;
1536
1537 if (reg < 0x80 || reg >= 0xc0)
1538 return -EINVAL;
1539 val1 = ucontrol->value.integer.value[0] & mask;
1540 val2 = ucontrol->value.integer.value[1] & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 val1 <<= shift_left;
1542 val2 <<= shift_right;
1543 spin_lock_irq(&chip->reg_lock);
1544 oval = snd_ymfpci_readl(chip, reg);
1545 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1546 change = val1 != oval;
1547 snd_ymfpci_writel(chip, reg, val1);
1548 spin_unlock_irq(&chip->reg_lock);
1549 return change;
1550}
1551
Clemens Ladisch177a7cd2007-07-23 17:38:44 +02001552static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1553 struct snd_ctl_elem_value *ucontrol)
1554{
1555 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1556 unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1557 unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1558 int change;
1559 unsigned int value, oval;
1560
1561 value = ucontrol->value.integer.value[0] & 0x3fff;
1562 value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1563 spin_lock_irq(&chip->reg_lock);
1564 oval = snd_ymfpci_readl(chip, reg);
1565 change = value != oval;
1566 snd_ymfpci_writel(chip, reg, value);
1567 snd_ymfpci_writel(chip, reg2, value);
1568 spin_unlock_irq(&chip->reg_lock);
1569 return change;
1570}
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572/*
1573 * 4ch duplication
1574 */
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001575#define snd_ymfpci_info_dup4ch snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Takashi Iwai208a1b42005-11-17 14:53:41 +01001577static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001579 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1581 return 0;
1582}
1583
Takashi Iwai208a1b42005-11-17 14:53:41 +01001584static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001586 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 int change;
1588 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1589 if (change)
1590 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1591 return change;
1592}
1593
1594
Takashi Iwai208a1b42005-11-17 14:53:41 +01001595static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
Clemens Ladisch177a7cd2007-07-23 17:38:44 +02001596{
1597 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1598 .name = "Wave Playback Volume",
1599 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1600 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1601 .info = snd_ymfpci_info_double,
1602 .get = snd_ymfpci_get_double,
1603 .put = snd_ymfpci_put_nativedacvol,
1604 .private_value = YDSXGR_NATIVEDACOUTVOL,
1605 .tlv = { .p = db_scale_native },
1606},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1608YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1609YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1610YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1611YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1612YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1613YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1614YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
1615YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1616YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1617YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1618YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
Glen Masgaid602c882005-09-28 08:19:05 +02001619YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1620YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1621YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1624 .name = "4ch Duplication",
1625 .info = snd_ymfpci_info_dup4ch,
1626 .get = snd_ymfpci_get_dup4ch,
1627 .put = snd_ymfpci_put_dup4ch,
1628},
1629};
1630
1631
1632/*
1633 * GPIO
1634 */
1635
Takashi Iwai208a1b42005-11-17 14:53:41 +01001636static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 u16 reg, mode;
1639 unsigned long flags;
1640
1641 spin_lock_irqsave(&chip->reg_lock, flags);
1642 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1643 reg &= ~(1 << (pin + 8));
1644 reg |= (1 << pin);
1645 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1646 /* set the level mode for input line */
1647 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1648 mode &= ~(3 << (pin * 2));
1649 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1650 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1651 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1652 spin_unlock_irqrestore(&chip->reg_lock, flags);
1653 return (mode >> pin) & 1;
1654}
1655
Takashi Iwai208a1b42005-11-17 14:53:41 +01001656static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 u16 reg;
1659 unsigned long flags;
1660
1661 spin_lock_irqsave(&chip->reg_lock, flags);
1662 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1663 reg &= ~(1 << pin);
1664 reg &= ~(1 << (pin + 8));
1665 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1666 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1667 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1668 spin_unlock_irqrestore(&chip->reg_lock, flags);
1669
1670 return 0;
1671}
1672
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001673#define snd_ymfpci_gpio_sw_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Takashi Iwai208a1b42005-11-17 14:53:41 +01001675static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001677 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 int pin = (int)kcontrol->private_value;
1679 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1680 return 0;
1681}
1682
Takashi Iwai208a1b42005-11-17 14:53:41 +01001683static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001685 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 int pin = (int)kcontrol->private_value;
1687
1688 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1689 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1690 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1691 return 1;
1692 }
1693 return 0;
1694}
1695
Takashi Iwai208a1b42005-11-17 14:53:41 +01001696static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 .name = "Shared Rear/Line-In Switch",
1698 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1699 .info = snd_ymfpci_gpio_sw_info,
1700 .get = snd_ymfpci_gpio_sw_get,
1701 .put = snd_ymfpci_gpio_sw_put,
1702 .private_value = 2,
1703};
1704
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001705/*
1706 * PCM voice volume
1707 */
1708
Takashi Iwai208a1b42005-11-17 14:53:41 +01001709static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1710 struct snd_ctl_elem_info *uinfo)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001711{
1712 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1713 uinfo->count = 2;
1714 uinfo->value.integer.min = 0;
1715 uinfo->value.integer.max = 0x8000;
1716 return 0;
1717}
1718
Takashi Iwai208a1b42005-11-17 14:53:41 +01001719static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1720 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001721{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001722 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001723 unsigned int subs = kcontrol->id.subdevice;
1724
1725 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1726 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1727 return 0;
1728}
1729
Takashi Iwai208a1b42005-11-17 14:53:41 +01001730static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1731 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001732{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001733 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001734 unsigned int subs = kcontrol->id.subdevice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001735 struct snd_pcm_substream *substream;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001736 unsigned long flags;
1737
1738 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1739 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1740 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1741 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001742 if (chip->pcm_mixer[subs].left > 0x8000)
1743 chip->pcm_mixer[subs].left = 0x8000;
1744 if (chip->pcm_mixer[subs].right > 0x8000)
1745 chip->pcm_mixer[subs].right = 0x8000;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001746
Takashi Iwai208a1b42005-11-17 14:53:41 +01001747 substream = (struct snd_pcm_substream *)kcontrol->private_value;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001748 spin_lock_irqsave(&chip->voice_lock, flags);
1749 if (substream->runtime && substream->runtime->private_data) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01001750 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01001751 if (!ypcm->use_441_slot)
1752 ypcm->update_pcm_vol = 2;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001753 }
1754 spin_unlock_irqrestore(&chip->voice_lock, flags);
1755 return 1;
1756 }
1757 return 0;
1758}
1759
Takashi Iwai208a1b42005-11-17 14:53:41 +01001760static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001761 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1762 .name = "PCM Playback Volume",
1763 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1764 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1765 .info = snd_ymfpci_pcm_vol_info,
1766 .get = snd_ymfpci_pcm_vol_get,
1767 .put = snd_ymfpci_pcm_vol_put,
1768};
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771/*
1772 * Mixer routines
1773 */
1774
Takashi Iwai208a1b42005-11-17 14:53:41 +01001775static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001777 struct snd_ymfpci *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 chip->ac97_bus = NULL;
1779}
1780
Takashi Iwai208a1b42005-11-17 14:53:41 +01001781static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001783 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 chip->ac97 = NULL;
1785}
1786
Glen Masgaid9301262006-10-10 09:27:19 +02001787int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001789 struct snd_ac97_template ac97;
1790 struct snd_kcontrol *kctl;
1791 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 unsigned int idx;
1793 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001794 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 .write = snd_ymfpci_codec_write,
1796 .read = snd_ymfpci_codec_read,
1797 };
1798
1799 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1800 return err;
1801 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1802 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1803
1804 memset(&ac97, 0, sizeof(ac97));
1805 ac97.private_data = chip;
1806 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1807 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1808 return err;
1809
1810 /* to be sure */
1811 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1812 AC97_EA_VRA|AC97_EA_VRM, 0);
1813
1814 for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1815 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1816 return err;
1817 }
1818
1819 /* add S/PDIF control */
Takashi Iwaida3cec32008-08-08 17:12:14 +02001820 if (snd_BUG_ON(!chip->pcm_spdif))
1821 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1823 return err;
1824 kctl->id.device = chip->pcm_spdif->device;
1825 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1826 return err;
1827 kctl->id.device = chip->pcm_spdif->device;
1828 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1829 return err;
1830 kctl->id.device = chip->pcm_spdif->device;
1831 chip->spdif_pcm_ctl = kctl;
1832
1833 /* direct recording source */
1834 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1835 (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1836 return err;
1837
1838 /*
1839 * shared rear/line-in
1840 */
1841 if (rear_switch) {
1842 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1843 return err;
1844 }
1845
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001846 /* per-voice volume */
1847 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1848 for (idx = 0; idx < 32; ++idx) {
1849 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1850 if (!kctl)
1851 return -ENOMEM;
1852 kctl->id.device = chip->pcm->device;
1853 kctl->id.subdevice = idx;
1854 kctl->private_value = (unsigned long)substream;
1855 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1856 return err;
1857 chip->pcm_mixer[idx].left = 0x8000;
1858 chip->pcm_mixer[idx].right = 0x8000;
1859 chip->pcm_mixer[idx].ctl = kctl;
1860 substream = substream->next;
1861 }
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 return 0;
1864}
1865
1866
1867/*
1868 * timer
1869 */
1870
Takashi Iwai208a1b42005-11-17 14:53:41 +01001871static int snd_ymfpci_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001873 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 unsigned long flags;
1875 unsigned int count;
1876
1877 chip = snd_timer_chip(timer);
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001878 count = (timer->sticks << 1) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 spin_lock_irqsave(&chip->reg_lock, flags);
1880 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1881 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1882 spin_unlock_irqrestore(&chip->reg_lock, flags);
1883 return 0;
1884}
1885
Takashi Iwai208a1b42005-11-17 14:53:41 +01001886static int snd_ymfpci_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001888 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 unsigned long flags;
1890
1891 chip = snd_timer_chip(timer);
1892 spin_lock_irqsave(&chip->reg_lock, flags);
1893 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1894 spin_unlock_irqrestore(&chip->reg_lock, flags);
1895 return 0;
1896}
1897
Takashi Iwai208a1b42005-11-17 14:53:41 +01001898static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 unsigned long *num, unsigned long *den)
1900{
1901 *num = 1;
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001902 *den = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return 0;
1904}
1905
Takashi Iwai208a1b42005-11-17 14:53:41 +01001906static struct snd_timer_hardware snd_ymfpci_timer_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 .flags = SNDRV_TIMER_HW_AUTO,
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001908 .resolution = 20833, /* 1/fs = 20.8333...us */
1909 .ticks = 0x8000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 .start = snd_ymfpci_timer_start,
1911 .stop = snd_ymfpci_timer_stop,
1912 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1913};
1914
Takashi Iwai208a1b42005-11-17 14:53:41 +01001915int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001917 struct snd_timer *timer = NULL;
1918 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 int err;
1920
1921 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1922 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1923 tid.card = chip->card->number;
1924 tid.device = device;
1925 tid.subdevice = 0;
1926 if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1927 strcpy(timer->name, "YMFPCI timer");
1928 timer->private_data = chip;
1929 timer->hw = snd_ymfpci_timer_hw;
1930 }
1931 chip->timer = timer;
1932 return err;
1933}
1934
1935
1936/*
1937 * proc interface
1938 */
1939
Takashi Iwai208a1b42005-11-17 14:53:41 +01001940static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1941 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001943 struct snd_ymfpci *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 int i;
1945
1946 snd_iprintf(buffer, "YMFPCI\n\n");
1947 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1948 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1949}
1950
Takashi Iwai208a1b42005-11-17 14:53:41 +01001951static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001953 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 if (! snd_card_proc_new(card, "ymfpci", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001956 snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return 0;
1958}
1959
1960/*
1961 * initialization routines
1962 */
1963
1964static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1965{
1966 u8 cmd;
1967
1968 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1969#if 0 // force to reset
1970 if (cmd & 0x03) {
1971#endif
1972 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1973 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1974 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1975 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1976 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1977#if 0
1978 }
1979#endif
1980}
1981
Takashi Iwai208a1b42005-11-17 14:53:41 +01001982static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1985}
1986
Takashi Iwai208a1b42005-11-17 14:53:41 +01001987static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 u32 val;
1990 int timeout = 1000;
1991
1992 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1993 if (val)
1994 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
1995 while (timeout-- > 0) {
1996 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
1997 if ((val & 0x00000002) == 0)
1998 break;
1999 }
2000}
2001
Clemens Ladisch102fa902006-10-11 12:05:59 +02002002static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2003{
2004 int err, is_1e;
2005 const char *name;
2006
2007 err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2008 &chip->pci->dev);
2009 if (err >= 0) {
David Woodhouseb82a82d2008-05-29 14:40:00 +03002010 if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
Clemens Ladisch102fa902006-10-11 12:05:59 +02002011 snd_printk(KERN_ERR "DSP microcode has wrong size\n");
2012 err = -EINVAL;
2013 }
2014 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002015 if (err < 0)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002016 return err;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002017 is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2018 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2019 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2020 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2021 name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2022 err = request_firmware(&chip->controller_microcode, name,
2023 &chip->pci->dev);
2024 if (err >= 0) {
David Woodhouseb82a82d2008-05-29 14:40:00 +03002025 if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
Clemens Ladisch102fa902006-10-11 12:05:59 +02002026 snd_printk(KERN_ERR "controller microcode"
2027 " has wrong size\n");
2028 err = -EINVAL;
2029 }
2030 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002031 if (err < 0)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002032 return err;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002033 return 0;
2034}
Clemens Ladisch7e0af292007-05-03 17:59:54 +02002035
2036MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
2037MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
2038MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
2039
Takashi Iwai208a1b42005-11-17 14:53:41 +01002040static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
2042 int i;
2043 u16 ctrl;
David Woodhouseb82a82d2008-05-29 14:40:00 +03002044 const __le32 *inst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2047 snd_ymfpci_disable_dsp(chip);
2048 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2049 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2050 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2051 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2052 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2053 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2054 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2055 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2056 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2057
2058 /* setup DSP instruction code */
David Woodhouseb82a82d2008-05-29 14:40:00 +03002059 inst = (const __le32 *)chip->dsp_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
David Woodhouseb82a82d2008-05-29 14:40:00 +03002061 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2062 le32_to_cpu(inst[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 /* setup control instruction code */
David Woodhouseb82a82d2008-05-29 14:40:00 +03002065 inst = (const __le32 *)chip->controller_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
David Woodhouseb82a82d2008-05-29 14:40:00 +03002067 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2068 le32_to_cpu(inst[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 snd_ymfpci_enable_dsp(chip);
2071}
2072
Takashi Iwai208a1b42005-11-17 14:53:41 +01002073static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
2075 long size, playback_ctrl_size;
2076 int voice, bank, reg;
2077 u8 *ptr;
2078 dma_addr_t ptr_addr;
2079
2080 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2081 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2082 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2083 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2084 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2085
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002086 size = ALIGN(playback_ctrl_size, 0x100) +
2087 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2088 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2089 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 chip->work_size;
2091 /* work_ptr must be aligned to 256 bytes, but it's already
2092 covered with the kernel page allocation mechanism */
2093 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2094 size, &chip->work_ptr) < 0)
2095 return -ENOMEM;
2096 ptr = chip->work_ptr.area;
2097 ptr_addr = chip->work_ptr.addr;
2098 memset(ptr, 0, size); /* for sure */
2099
2100 chip->bank_base_playback = ptr;
2101 chip->bank_base_playback_addr = ptr_addr;
2102 chip->ctrl_playback = (u32 *)ptr;
2103 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002104 ptr += ALIGN(playback_ctrl_size, 0x100);
2105 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2107 chip->voices[voice].number = voice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002108 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 chip->voices[voice].bank_addr = ptr_addr;
2110 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002111 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 ptr += chip->bank_size_playback;
2113 ptr_addr += chip->bank_size_playback;
2114 }
2115 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002116 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2117 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 chip->bank_base_capture = ptr;
2119 chip->bank_base_capture_addr = ptr_addr;
2120 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2121 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002122 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 ptr += chip->bank_size_capture;
2124 ptr_addr += chip->bank_size_capture;
2125 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002126 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2127 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 chip->bank_base_effect = ptr;
2129 chip->bank_base_effect_addr = ptr_addr;
2130 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2131 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002132 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 ptr += chip->bank_size_effect;
2134 ptr_addr += chip->bank_size_effect;
2135 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002136 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2137 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 chip->work_base = ptr;
2139 chip->work_base_addr = ptr_addr;
2140
Takashi Iwaida3cec32008-08-08 17:12:14 +02002141 snd_BUG_ON(ptr + chip->work_size !=
2142 chip->work_ptr.area + chip->work_ptr.bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2145 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2146 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2147 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2148 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2149
2150 /* S/PDIF output initialization */
2151 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2152 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2153 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2154
2155 /* S/PDIF input initialization */
2156 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2157
2158 /* digital mixer setup */
2159 for (reg = 0x80; reg < 0xc0; reg += 4)
2160 snd_ymfpci_writel(chip, reg, 0);
2161 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
Takashi Iwai4a3b6982008-06-25 17:17:00 +02002162 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2164 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2165 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2166 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2167 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2168 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2169
2170 return 0;
2171}
2172
Takashi Iwai208a1b42005-11-17 14:53:41 +01002173static int snd_ymfpci_free(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
2175 u16 ctrl;
2176
Takashi Iwaida3cec32008-08-08 17:12:14 +02002177 if (snd_BUG_ON(!chip))
2178 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 if (chip->res_reg_area) { /* don't touch busy hardware */
2181 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2182 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2183 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2184 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2185 snd_ymfpci_disable_dsp(chip);
2186 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2187 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2188 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2189 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2190 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2191 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2192 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2193 }
2194
2195 snd_ymfpci_ac3_done(chip);
2196
2197 /* Set PCI device to D3 state */
2198#if 0
2199 /* FIXME: temporarily disabled, otherwise we cannot fire up
2200 * the chip again unless reboot. ACPI bug?
2201 */
2202 pci_set_power_state(chip->pci, 3);
2203#endif
2204
2205#ifdef CONFIG_PM
2206 vfree(chip->saved_regs);
2207#endif
Takashi Iwai95866d382008-03-22 10:11:08 +01002208 if (chip->irq >= 0)
2209 free_irq(chip->irq, chip);
Takashi Iwaib1d57762005-10-10 11:56:31 +02002210 release_and_free_resource(chip->mpu_res);
2211 release_and_free_resource(chip->fm_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 snd_ymfpci_free_gameport(chip);
2213 if (chip->reg_area_virt)
2214 iounmap(chip->reg_area_virt);
2215 if (chip->work_ptr.area)
2216 snd_dma_free_pages(&chip->work_ptr);
2217
Takashi Iwaib1d57762005-10-10 11:56:31 +02002218 release_and_free_resource(chip->res_reg_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2221
2222 pci_disable_device(chip->pci);
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002223 release_firmware(chip->dsp_microcode);
2224 release_firmware(chip->controller_microcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 kfree(chip);
2226 return 0;
2227}
2228
Takashi Iwai208a1b42005-11-17 14:53:41 +01002229static int snd_ymfpci_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002231 struct snd_ymfpci *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return snd_ymfpci_free(chip);
2233}
2234
2235#ifdef CONFIG_PM
2236static int saved_regs_index[] = {
2237 /* spdif */
2238 YDSXGR_SPDIFOUTCTRL,
2239 YDSXGR_SPDIFOUTSTATUS,
2240 YDSXGR_SPDIFINCTRL,
2241 /* volumes */
2242 YDSXGR_PRIADCLOOPVOL,
2243 YDSXGR_NATIVEDACINVOL,
2244 YDSXGR_NATIVEDACOUTVOL,
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01002245 YDSXGR_BUF441OUTVOL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 YDSXGR_NATIVEADCINVOL,
2247 YDSXGR_SPDIFLOOPVOL,
2248 YDSXGR_SPDIFOUTVOL,
2249 YDSXGR_ZVOUTVOL,
2250 YDSXGR_LEGACYOUTVOL,
2251 /* address bases */
2252 YDSXGR_PLAYCTRLBASE,
2253 YDSXGR_RECCTRLBASE,
2254 YDSXGR_EFFCTRLBASE,
2255 YDSXGR_WORKBASE,
2256 /* capture set up */
2257 YDSXGR_MAPOFREC,
2258 YDSXGR_RECFORMAT,
2259 YDSXGR_RECSLOTSR,
2260 YDSXGR_ADCFORMAT,
2261 YDSXGR_ADCSLOTSR,
2262};
2263#define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
2264
Takashi Iwaided46232005-11-17 16:09:43 +01002265int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
Takashi Iwaided46232005-11-17 16:09:43 +01002267 struct snd_card *card = pci_get_drvdata(pci);
2268 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 unsigned int i;
2270
Takashi Iwaided46232005-11-17 16:09:43 +01002271 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 snd_pcm_suspend_all(chip->pcm);
2273 snd_pcm_suspend_all(chip->pcm2);
2274 snd_pcm_suspend_all(chip->pcm_spdif);
2275 snd_pcm_suspend_all(chip->pcm_4ch);
2276 snd_ac97_suspend(chip->ac97);
2277 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2278 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2279 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2280 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
Takashi Iwai4a3b6982008-06-25 17:17:00 +02002281 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 snd_ymfpci_disable_dsp(chip);
Takashi Iwaided46232005-11-17 16:09:43 +01002283 pci_disable_device(pci);
2284 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002285 pci_set_power_state(pci, pci_choose_state(pci, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return 0;
2287}
2288
Takashi Iwaided46232005-11-17 16:09:43 +01002289int snd_ymfpci_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Takashi Iwaided46232005-11-17 16:09:43 +01002291 struct snd_card *card = pci_get_drvdata(pci);
2292 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 unsigned int i;
2294
Takashi Iwai30b35392006-10-11 18:52:53 +02002295 pci_set_power_state(pci, PCI_D0);
Takashi Iwaided46232005-11-17 16:09:43 +01002296 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002297 if (pci_enable_device(pci) < 0) {
2298 printk(KERN_ERR "ymfpci: pci_enable_device failed, "
2299 "disabling device\n");
2300 snd_card_disconnect(card);
2301 return -EIO;
2302 }
Takashi Iwaided46232005-11-17 16:09:43 +01002303 pci_set_master(pci);
2304 snd_ymfpci_aclink_reset(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 snd_ymfpci_codec_ready(chip, 0);
2306 snd_ymfpci_download_image(chip);
2307 udelay(100);
2308
2309 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2310 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2311
2312 snd_ac97_resume(chip->ac97);
2313
2314 /* start hw again */
2315 if (chip->start_count > 0) {
2316 spin_lock_irq(&chip->reg_lock);
2317 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2318 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2319 spin_unlock_irq(&chip->reg_lock);
2320 }
Takashi Iwaided46232005-11-17 16:09:43 +01002321 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return 0;
2323}
2324#endif /* CONFIG_PM */
2325
Takashi Iwai208a1b42005-11-17 14:53:41 +01002326int __devinit snd_ymfpci_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 struct pci_dev * pci,
2328 unsigned short old_legacy_ctrl,
Takashi Iwai208a1b42005-11-17 14:53:41 +01002329 struct snd_ymfpci ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002331 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002333 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 .dev_free = snd_ymfpci_dev_free,
2335 };
2336
2337 *rchip = NULL;
2338
2339 /* enable PCI device */
2340 if ((err = pci_enable_device(pci)) < 0)
2341 return err;
2342
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002343 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (chip == NULL) {
2345 pci_disable_device(pci);
2346 return -ENOMEM;
2347 }
2348 chip->old_legacy_ctrl = old_legacy_ctrl;
2349 spin_lock_init(&chip->reg_lock);
2350 spin_lock_init(&chip->voice_lock);
2351 init_waitqueue_head(&chip->interrupt_sleep);
2352 atomic_set(&chip->interrupt_sleep_count, 0);
2353 chip->card = card;
2354 chip->pci = pci;
2355 chip->irq = -1;
2356 chip->device_id = pci->device;
Auke Kok44c10132007-06-08 15:46:36 -07002357 chip->rev = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 chip->reg_area_phys = pci_resource_start(pci, 0);
2359 chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2360 pci_set_master(pci);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01002361 chip->src441_used = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002364 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 -07002365 snd_ymfpci_free(chip);
2366 return -EBUSY;
2367 }
Takashi Iwai437a5a42006-11-21 12:14:23 +01002368 if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2369 "YMFPCI", chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002370 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 snd_ymfpci_free(chip);
2372 return -EBUSY;
2373 }
2374 chip->irq = pci->irq;
2375
2376 snd_ymfpci_aclink_reset(pci);
2377 if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2378 snd_ymfpci_free(chip);
2379 return -EIO;
2380 }
2381
Clemens Ladisch102fa902006-10-11 12:05:59 +02002382 err = snd_ymfpci_request_firmware(chip);
2383 if (err < 0) {
2384 snd_printk(KERN_ERR "firmware request failed: %d\n", err);
2385 snd_ymfpci_free(chip);
2386 return err;
2387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 snd_ymfpci_download_image(chip);
2389
2390 udelay(100); /* seems we need a delay after downloading image.. */
2391
2392 if (snd_ymfpci_memalloc(chip) < 0) {
2393 snd_ymfpci_free(chip);
2394 return -EIO;
2395 }
2396
2397 if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
2398 snd_ymfpci_free(chip);
2399 return err;
2400 }
2401
2402#ifdef CONFIG_PM
2403 chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
2404 if (chip->saved_regs == NULL) {
2405 snd_ymfpci_free(chip);
2406 return -ENOMEM;
2407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408#endif
2409
2410 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2411 snd_ymfpci_free(chip);
2412 return err;
2413 }
2414
2415 snd_ymfpci_proc_init(card, chip);
2416
2417 snd_card_set_dev(card, &pci->dev);
2418
2419 *rchip = chip;
2420 return 0;
2421}