blob: 29b3056c51098b708aaa6e860af148a011bae098 [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>
29
30#include <sound/core.h>
31#include <sound/control.h>
32#include <sound/info.h>
Takashi Iwai33925182006-08-28 13:29:42 +020033#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <sound/ymfpci.h>
35#include <sound/asoundef.h>
36#include <sound/mpu401.h>
37
38#include <asm/io.h>
Clemens Ladisch102fa902006-10-11 12:05:59 +020039#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * common I/O routines
43 */
44
Takashi Iwai208a1b42005-11-17 14:53:41 +010045static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Takashi Iwai208a1b42005-11-17 14:53:41 +010047static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 return readb(chip->reg_area_virt + offset);
50}
51
Takashi Iwai208a1b42005-11-17 14:53:41 +010052static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 writeb(val, chip->reg_area_virt + offset);
55}
56
Takashi Iwai208a1b42005-11-17 14:53:41 +010057static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 return readw(chip->reg_area_virt + offset);
60}
61
Takashi Iwai208a1b42005-11-17 14:53:41 +010062static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 writew(val, chip->reg_area_virt + offset);
65}
66
Takashi Iwai208a1b42005-11-17 14:53:41 +010067static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 return readl(chip->reg_area_virt + offset);
70}
71
Takashi Iwai208a1b42005-11-17 14:53:41 +010072static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 writel(val, chip->reg_area_virt + offset);
75}
76
Takashi Iwai208a1b42005-11-17 14:53:41 +010077static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020079 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
81
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020082 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 do {
84 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
85 return 0;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +020086 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020087 } while (time_before(jiffies, end_time));
Takashi Iwai99b359b2005-10-20 18:26:44 +020088 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 -070089 return -EBUSY;
90}
91
Takashi Iwai208a1b42005-11-17 14:53:41 +010092static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Takashi Iwai208a1b42005-11-17 14:53:41 +010094 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 u32 cmd;
96
97 snd_ymfpci_codec_ready(chip, 0);
98 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
99 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
100}
101
Takashi Iwai208a1b42005-11-17 14:53:41 +0100102static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100104 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 if (snd_ymfpci_codec_ready(chip, 0))
107 return ~0;
108 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
109 if (snd_ymfpci_codec_ready(chip, 0))
110 return ~0;
111 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
112 int i;
113 for (i = 0; i < 600; i++)
114 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
115 }
116 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
117}
118
119/*
120 * Misc routines
121 */
122
123static u32 snd_ymfpci_calc_delta(u32 rate)
124{
125 switch (rate) {
126 case 8000: return 0x02aaab00;
127 case 11025: return 0x03accd00;
128 case 16000: return 0x05555500;
129 case 22050: return 0x07599a00;
130 case 32000: return 0x0aaaab00;
131 case 44100: return 0x0eb33300;
132 default: return ((rate << 16) / 375) << 5;
133 }
134}
135
136static u32 def_rate[8] = {
137 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
138};
139
140static u32 snd_ymfpci_calc_lpfK(u32 rate)
141{
142 u32 i;
143 static u32 val[8] = {
144 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
145 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
146 };
147
148 if (rate == 44100)
149 return 0x40000000; /* FIXME: What's the right value? */
150 for (i = 0; i < 8; i++)
151 if (rate <= def_rate[i])
152 return val[i];
153 return val[0];
154}
155
156static u32 snd_ymfpci_calc_lpfQ(u32 rate)
157{
158 u32 i;
159 static u32 val[8] = {
160 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
161 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
162 };
163
164 if (rate == 44100)
165 return 0x370A0000;
166 for (i = 0; i < 8; i++)
167 if (rate <= def_rate[i])
168 return val[i];
169 return val[0];
170}
171
172/*
173 * Hardware start management
174 */
175
Takashi Iwai208a1b42005-11-17 14:53:41 +0100176static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 unsigned long flags;
179
180 spin_lock_irqsave(&chip->reg_lock, flags);
181 if (chip->start_count++ > 0)
182 goto __end;
183 snd_ymfpci_writel(chip, YDSXGR_MODE,
184 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
185 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
186 __end:
187 spin_unlock_irqrestore(&chip->reg_lock, flags);
188}
189
Takashi Iwai208a1b42005-11-17 14:53:41 +0100190static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 unsigned long flags;
193 long timeout = 1000;
194
195 spin_lock_irqsave(&chip->reg_lock, flags);
196 if (--chip->start_count > 0)
197 goto __end;
198 snd_ymfpci_writel(chip, YDSXGR_MODE,
199 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
200 while (timeout-- > 0) {
201 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
202 break;
203 }
204 if (atomic_read(&chip->interrupt_sleep_count)) {
205 atomic_set(&chip->interrupt_sleep_count, 0);
206 wake_up(&chip->interrupt_sleep);
207 }
208 __end:
209 spin_unlock_irqrestore(&chip->reg_lock, flags);
210}
211
212/*
213 * Playback voice management
214 */
215
Takashi Iwai208a1b42005-11-17 14:53:41 +0100216static int voice_alloc(struct snd_ymfpci *chip,
217 enum snd_ymfpci_voice_type type, int pair,
218 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100220 struct snd_ymfpci_voice *voice, *voice2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 int idx;
222
223 *rvoice = NULL;
224 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
225 voice = &chip->voices[idx];
226 voice2 = pair ? &chip->voices[idx+1] : NULL;
227 if (voice->use || (voice2 && voice2->use))
228 continue;
229 voice->use = 1;
230 if (voice2)
231 voice2->use = 1;
232 switch (type) {
233 case YMFPCI_PCM:
234 voice->pcm = 1;
235 if (voice2)
236 voice2->pcm = 1;
237 break;
238 case YMFPCI_SYNTH:
239 voice->synth = 1;
240 break;
241 case YMFPCI_MIDI:
242 voice->midi = 1;
243 break;
244 }
245 snd_ymfpci_hw_start(chip);
246 if (voice2)
247 snd_ymfpci_hw_start(chip);
248 *rvoice = voice;
249 return 0;
250 }
251 return -ENOMEM;
252}
253
Takashi Iwai208a1b42005-11-17 14:53:41 +0100254static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
255 enum snd_ymfpci_voice_type type, int pair,
256 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 unsigned long flags;
259 int result;
260
261 snd_assert(rvoice != NULL, return -EINVAL);
262 snd_assert(!pair || type == YMFPCI_PCM, return -EINVAL);
263
264 spin_lock_irqsave(&chip->voice_lock, flags);
265 for (;;) {
266 result = voice_alloc(chip, type, pair, rvoice);
267 if (result == 0 || type != YMFPCI_PCM)
268 break;
269 /* TODO: synth/midi voice deallocation */
270 break;
271 }
272 spin_unlock_irqrestore(&chip->voice_lock, flags);
273 return result;
274}
275
Takashi Iwai208a1b42005-11-17 14:53:41 +0100276static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 unsigned long flags;
279
280 snd_assert(pvoice != NULL, return -EINVAL);
281 snd_ymfpci_hw_stop(chip);
282 spin_lock_irqsave(&chip->voice_lock, flags);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100283 if (pvoice->number == chip->src441_used) {
284 chip->src441_used = -1;
285 pvoice->ypcm->use_441_slot = 0;
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
288 pvoice->ypcm = NULL;
289 pvoice->interrupt = NULL;
290 spin_unlock_irqrestore(&chip->voice_lock, flags);
291 return 0;
292}
293
294/*
295 * PCM part
296 */
297
Takashi Iwai208a1b42005-11-17 14:53:41 +0100298static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100300 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 u32 pos, delta;
302
303 if ((ypcm = voice->ypcm) == NULL)
304 return;
305 if (ypcm->substream == NULL)
306 return;
307 spin_lock(&chip->reg_lock);
308 if (ypcm->running) {
309 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
310 if (pos < ypcm->last_pos)
311 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
312 else
313 delta = pos - ypcm->last_pos;
314 ypcm->period_pos += delta;
315 ypcm->last_pos = pos;
316 if (ypcm->period_pos >= ypcm->period_size) {
317 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
318 ypcm->period_pos %= ypcm->period_size;
319 spin_unlock(&chip->reg_lock);
320 snd_pcm_period_elapsed(ypcm->substream);
321 spin_lock(&chip->reg_lock);
322 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200323
324 if (unlikely(ypcm->update_pcm_vol)) {
325 unsigned int subs = ypcm->substream->number;
326 unsigned int next_bank = 1 - chip->active_bank;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100327 struct snd_ymfpci_playback_bank *bank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200328 u32 volume;
329
330 bank = &voice->bank[next_bank];
331 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
332 bank->left_gain_end = volume;
333 if (ypcm->output_rear)
334 bank->eff2_gain_end = volume;
335 if (ypcm->voices[1])
336 bank = &ypcm->voices[1]->bank[next_bank];
337 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
338 bank->right_gain_end = volume;
339 if (ypcm->output_rear)
340 bank->eff3_gain_end = volume;
341 ypcm->update_pcm_vol--;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 spin_unlock(&chip->reg_lock);
345}
346
Takashi Iwai208a1b42005-11-17 14:53:41 +0100347static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100349 struct snd_pcm_runtime *runtime = substream->runtime;
350 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
351 struct snd_ymfpci *chip = ypcm->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 u32 pos, delta;
353
354 spin_lock(&chip->reg_lock);
355 if (ypcm->running) {
356 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
357 if (pos < ypcm->last_pos)
358 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
359 else
360 delta = pos - ypcm->last_pos;
361 ypcm->period_pos += delta;
362 ypcm->last_pos = pos;
363 if (ypcm->period_pos >= ypcm->period_size) {
364 ypcm->period_pos %= ypcm->period_size;
365 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
366 spin_unlock(&chip->reg_lock);
367 snd_pcm_period_elapsed(substream);
368 spin_lock(&chip->reg_lock);
369 }
370 }
371 spin_unlock(&chip->reg_lock);
372}
373
Takashi Iwai208a1b42005-11-17 14:53:41 +0100374static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 int cmd)
376{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100377 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
378 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200379 struct snd_kcontrol *kctl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int result = 0;
381
382 spin_lock(&chip->reg_lock);
383 if (ypcm->voices[0] == NULL) {
384 result = -EINVAL;
385 goto __unlock;
386 }
387 switch (cmd) {
388 case SNDRV_PCM_TRIGGER_START:
389 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
390 case SNDRV_PCM_TRIGGER_RESUME:
391 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100392 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
394 ypcm->running = 1;
395 break;
396 case SNDRV_PCM_TRIGGER_STOP:
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200397 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
398 kctl = chip->pcm_mixer[substream->number].ctl;
399 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
400 }
401 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
403 case SNDRV_PCM_TRIGGER_SUSPEND:
404 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100405 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
407 ypcm->running = 0;
408 break;
409 default:
410 result = -EINVAL;
411 break;
412 }
413 __unlock:
414 spin_unlock(&chip->reg_lock);
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200415 if (kctl)
416 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return result;
418}
Takashi Iwai208a1b42005-11-17 14:53:41 +0100419static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 int cmd)
421{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100422 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
423 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 int result = 0;
425 u32 tmp;
426
427 spin_lock(&chip->reg_lock);
428 switch (cmd) {
429 case SNDRV_PCM_TRIGGER_START:
430 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
431 case SNDRV_PCM_TRIGGER_RESUME:
432 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
433 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
434 ypcm->running = 1;
435 break;
436 case SNDRV_PCM_TRIGGER_STOP:
437 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
438 case SNDRV_PCM_TRIGGER_SUSPEND:
439 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
440 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
441 ypcm->running = 0;
442 break;
443 default:
444 result = -EINVAL;
445 break;
446 }
447 spin_unlock(&chip->reg_lock);
448 return result;
449}
450
Takashi Iwai208a1b42005-11-17 14:53:41 +0100451static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 int err;
454
455 if (ypcm->voices[1] != NULL && voices < 2) {
456 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
457 ypcm->voices[1] = NULL;
458 }
459 if (voices == 1 && ypcm->voices[0] != NULL)
460 return 0; /* already allocated */
461 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
462 return 0; /* already allocated */
463 if (voices > 1) {
464 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
465 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
466 ypcm->voices[0] = NULL;
467 }
468 }
469 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
470 if (err < 0)
471 return err;
472 ypcm->voices[0]->ypcm = ypcm;
473 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
474 if (voices > 1) {
475 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
476 ypcm->voices[1]->ypcm = ypcm;
477 }
478 return 0;
479}
480
Takashi Iwai208a1b42005-11-17 14:53:41 +0100481static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
482 struct snd_pcm_runtime *runtime,
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200483 int has_pcm_volume)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100485 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 u32 format;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200487 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
488 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
489 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
Takashi Iwai208a1b42005-11-17 14:53:41 +0100490 struct snd_ymfpci_playback_bank *bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 unsigned int nbank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200492 u32 vol_left, vol_right;
493 u8 use_left, use_right;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100494 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 snd_assert(voice != NULL, return);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200497 if (runtime->channels == 1) {
498 use_left = 1;
499 use_right = 1;
500 } else {
501 use_left = (voiceidx & 1) == 0;
502 use_right = !use_left;
503 }
504 if (has_pcm_volume) {
505 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
506 [ypcm->substream->number].left << 15);
507 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
508 [ypcm->substream->number].right << 15);
509 } else {
510 vol_left = cpu_to_le32(0x40000000);
511 vol_right = cpu_to_le32(0x40000000);
512 }
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100513 spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200514 format = runtime->channels == 2 ? 0x00010000 : 0;
515 if (snd_pcm_format_width(runtime->format) == 8)
516 format |= 0x80000000;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100517 else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
518 runtime->rate == 44100 && runtime->channels == 2 &&
519 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
520 ypcm->chip->src441_used == voice->number)) {
521 ypcm->chip->src441_used = voice->number;
522 ypcm->use_441_slot = 1;
523 format |= 0x10000000;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100524 }
525 if (ypcm->chip->src441_used == voice->number &&
526 (format & 0x10000000) == 0) {
527 ypcm->chip->src441_used = -1;
528 ypcm->use_441_slot = 0;
529 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200530 if (runtime->channels == 2 && (voiceidx & 1) != 0)
531 format |= 1;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100532 spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 for (nbank = 0; nbank < 2; nbank++) {
534 bank = &voice->bank[nbank];
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200535 memset(bank, 0, sizeof(*bank));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 bank->format = cpu_to_le32(format);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200537 bank->base = cpu_to_le32(runtime->dma_addr);
538 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 bank->lpfQ = cpu_to_le32(lpfQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 bank->delta =
541 bank->delta_end = cpu_to_le32(delta);
542 bank->lpfK =
543 bank->lpfK_end = cpu_to_le32(lpfK);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200544 bank->eg_gain =
545 bank->eg_gain_end = cpu_to_le32(0x40000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200547 if (ypcm->output_front) {
548 if (use_left) {
549 bank->left_gain =
550 bank->left_gain_end = vol_left;
551 }
552 if (use_right) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 bank->right_gain =
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200554 bank->right_gain_end = vol_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200556 }
557 if (ypcm->output_rear) {
Jaroslav Kysela5a25c5c2006-01-18 08:02:24 +0100558 if (!ypcm->swap_rear) {
559 if (use_left) {
560 bank->eff2_gain =
561 bank->eff2_gain_end = vol_left;
562 }
563 if (use_right) {
564 bank->eff3_gain =
565 bank->eff3_gain_end = vol_right;
566 }
567 } else {
568 /* The SPDIF out channels seem to be swapped, so we have
569 * to swap them here, too. The rear analog out channels
570 * will be wrong, but otherwise AC3 would not work.
571 */
572 if (use_left) {
573 bank->eff3_gain =
574 bank->eff3_gain_end = vol_left;
575 }
576 if (use_right) {
577 bank->eff2_gain =
578 bank->eff2_gain_end = vol_right;
579 }
580 }
581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583}
584
Takashi Iwai208a1b42005-11-17 14:53:41 +0100585static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
588 4096, &chip->ac3_tmp_base) < 0)
589 return -ENOMEM;
590
591 chip->bank_effect[3][0]->base =
592 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
593 chip->bank_effect[3][0]->loop_end =
594 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
595 chip->bank_effect[4][0]->base =
596 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
597 chip->bank_effect[4][0]->loop_end =
598 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
599
600 spin_lock_irq(&chip->reg_lock);
601 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
602 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
603 spin_unlock_irq(&chip->reg_lock);
604 return 0;
605}
606
Takashi Iwai208a1b42005-11-17 14:53:41 +0100607static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 spin_lock_irq(&chip->reg_lock);
610 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
611 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
612 spin_unlock_irq(&chip->reg_lock);
613 // snd_ymfpci_irq_wait(chip);
614 if (chip->ac3_tmp_base.area) {
615 snd_dma_free_pages(&chip->ac3_tmp_base);
616 chip->ac3_tmp_base.area = NULL;
617 }
618 return 0;
619}
620
Takashi Iwai208a1b42005-11-17 14:53:41 +0100621static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
622 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100624 struct snd_pcm_runtime *runtime = substream->runtime;
625 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 int err;
627
628 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
629 return err;
630 if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
631 return err;
632 return 0;
633}
634
Takashi Iwai208a1b42005-11-17 14:53:41 +0100635static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100637 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
638 struct snd_pcm_runtime *runtime = substream->runtime;
639 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 if (runtime->private_data == NULL)
642 return 0;
643 ypcm = runtime->private_data;
644
645 /* wait, until the PCI operations are not finished */
646 snd_ymfpci_irq_wait(chip);
647 snd_pcm_lib_free_pages(substream);
648 if (ypcm->voices[1]) {
649 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
650 ypcm->voices[1] = NULL;
651 }
652 if (ypcm->voices[0]) {
653 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
654 ypcm->voices[0] = NULL;
655 }
656 return 0;
657}
658
Takashi Iwai208a1b42005-11-17 14:53:41 +0100659static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100661 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
662 struct snd_pcm_runtime *runtime = substream->runtime;
663 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200664 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 unsigned int nvoice;
666
667 ypcm->period_size = runtime->period_size;
668 ypcm->buffer_size = runtime->buffer_size;
669 ypcm->period_pos = 0;
670 ypcm->last_pos = 0;
671 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200672 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
673 substream->pcm == chip->pcm);
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200674
675 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
676 kctl = chip->pcm_mixer[substream->number].ctl;
677 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
678 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
681}
682
Takashi Iwai208a1b42005-11-17 14:53:41 +0100683static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
684 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
687}
688
Takashi Iwai208a1b42005-11-17 14:53:41 +0100689static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100691 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* wait, until the PCI operations are not finished */
694 snd_ymfpci_irq_wait(chip);
695 return snd_pcm_lib_free_pages(substream);
696}
697
Takashi Iwai208a1b42005-11-17 14:53:41 +0100698static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100700 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
701 struct snd_pcm_runtime *runtime = substream->runtime;
702 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
703 struct snd_ymfpci_capture_bank * bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int nbank;
705 u32 rate, format;
706
707 ypcm->period_size = runtime->period_size;
708 ypcm->buffer_size = runtime->buffer_size;
709 ypcm->period_pos = 0;
710 ypcm->last_pos = 0;
711 ypcm->shift = 0;
712 rate = ((48000 * 4096) / runtime->rate) - 1;
713 format = 0;
714 if (runtime->channels == 2) {
715 format |= 2;
716 ypcm->shift++;
717 }
718 if (snd_pcm_format_width(runtime->format) == 8)
719 format |= 1;
720 else
721 ypcm->shift++;
722 switch (ypcm->capture_bank_number) {
723 case 0:
724 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
725 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
726 break;
727 case 1:
728 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
729 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
730 break;
731 }
732 for (nbank = 0; nbank < 2; nbank++) {
733 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
734 bank->base = cpu_to_le32(runtime->dma_addr);
735 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
736 bank->start = 0;
737 bank->num_of_loops = 0;
738 }
739 return 0;
740}
741
Takashi Iwai208a1b42005-11-17 14:53:41 +0100742static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100744 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
745 struct snd_pcm_runtime *runtime = substream->runtime;
746 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
747 struct snd_ymfpci_voice *voice = ypcm->voices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (!(ypcm->running && voice))
750 return 0;
751 return le32_to_cpu(voice->bank[chip->active_bank].start);
752}
753
Takashi Iwai208a1b42005-11-17 14:53:41 +0100754static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100756 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
757 struct snd_pcm_runtime *runtime = substream->runtime;
758 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (!ypcm->running)
761 return 0;
762 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
763}
764
Takashi Iwai208a1b42005-11-17 14:53:41 +0100765static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 wait_queue_t wait;
768 int loops = 4;
769
770 while (loops-- > 0) {
771 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
772 continue;
773 init_waitqueue_entry(&wait, current);
774 add_wait_queue(&chip->interrupt_sleep, &wait);
775 atomic_inc(&chip->interrupt_sleep_count);
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200776 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 remove_wait_queue(&chip->interrupt_sleep, &wait);
778 }
779}
780
David Howells7d12e782006-10-05 14:55:46 +0100781static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100783 struct snd_ymfpci *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 u32 status, nvoice, mode;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100785 struct snd_ymfpci_voice *voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
788 if (status & 0x80000000) {
789 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
790 spin_lock(&chip->voice_lock);
791 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
792 voice = &chip->voices[nvoice];
793 if (voice->interrupt)
794 voice->interrupt(chip, voice);
795 }
796 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
797 if (chip->capture_substream[nvoice])
798 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
799 }
800#if 0
801 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
802 if (chip->effect_substream[nvoice])
803 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
804 }
805#endif
806 spin_unlock(&chip->voice_lock);
807 spin_lock(&chip->reg_lock);
808 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
809 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
810 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
811 spin_unlock(&chip->reg_lock);
812
813 if (atomic_read(&chip->interrupt_sleep_count)) {
814 atomic_set(&chip->interrupt_sleep_count, 0);
815 wake_up(&chip->interrupt_sleep);
816 }
817 }
818
819 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
820 if (status & 1) {
821 if (chip->timer)
822 snd_timer_interrupt(chip->timer, chip->timer->sticks);
823 }
824 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
825
826 if (chip->rawmidi)
David Howells7d12e782006-10-05 14:55:46 +0100827 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return IRQ_HANDLED;
829}
830
Takashi Iwai208a1b42005-11-17 14:53:41 +0100831static struct snd_pcm_hardware snd_ymfpci_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 .info = (SNDRV_PCM_INFO_MMAP |
834 SNDRV_PCM_INFO_MMAP_VALID |
835 SNDRV_PCM_INFO_INTERLEAVED |
836 SNDRV_PCM_INFO_BLOCK_TRANSFER |
837 SNDRV_PCM_INFO_PAUSE |
838 SNDRV_PCM_INFO_RESUME),
839 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
840 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
841 .rate_min = 8000,
842 .rate_max = 48000,
843 .channels_min = 1,
844 .channels_max = 2,
845 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
846 .period_bytes_min = 64,
847 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
848 .periods_min = 3,
849 .periods_max = 1024,
850 .fifo_size = 0,
851};
852
Takashi Iwai208a1b42005-11-17 14:53:41 +0100853static struct snd_pcm_hardware snd_ymfpci_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 .info = (SNDRV_PCM_INFO_MMAP |
856 SNDRV_PCM_INFO_MMAP_VALID |
857 SNDRV_PCM_INFO_INTERLEAVED |
858 SNDRV_PCM_INFO_BLOCK_TRANSFER |
859 SNDRV_PCM_INFO_PAUSE |
860 SNDRV_PCM_INFO_RESUME),
861 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
862 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
863 .rate_min = 8000,
864 .rate_max = 48000,
865 .channels_min = 1,
866 .channels_max = 2,
867 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
868 .period_bytes_min = 64,
869 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
870 .periods_min = 3,
871 .periods_max = 1024,
872 .fifo_size = 0,
873};
874
Takashi Iwai208a1b42005-11-17 14:53:41 +0100875static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Jesper Juhl4d572772005-05-30 17:30:32 +0200877 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Takashi Iwai208a1b42005-11-17 14:53:41 +0100880static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100882 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
883 struct snd_pcm_runtime *runtime = substream->runtime;
884 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200886 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (ypcm == NULL)
888 return -ENOMEM;
889 ypcm->chip = chip;
890 ypcm->type = PLAYBACK_VOICE;
891 ypcm->substream = substream;
892 runtime->hw = snd_ymfpci_playback;
893 runtime->private_data = ypcm;
894 runtime->private_free = snd_ymfpci_pcm_free_substream;
895 /* FIXME? True value is 256/48 = 5.33333 ms */
896 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
897 return 0;
898}
899
900/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100901static void ymfpci_open_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 if (! chip->rear_opened) {
904 if (! chip->spdif_opened) /* set AC3 */
905 snd_ymfpci_writel(chip, YDSXGR_MODE,
906 snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
907 /* enable second codec (4CHEN) */
908 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
909 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
910 }
911}
912
913/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100914static void ymfpci_close_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 if (! chip->rear_opened) {
917 if (! chip->spdif_opened)
918 snd_ymfpci_writel(chip, YDSXGR_MODE,
919 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
920 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
921 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
922 }
923}
924
Takashi Iwai208a1b42005-11-17 14:53:41 +0100925static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100927 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
928 struct snd_pcm_runtime *runtime = substream->runtime;
929 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 int err;
931
932 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
933 return err;
934 ypcm = runtime->private_data;
935 ypcm->output_front = 1;
936 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
Glen Masgaid9301262006-10-10 09:27:19 +0200937 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 spin_lock_irq(&chip->reg_lock);
939 if (ypcm->output_rear) {
940 ymfpci_open_extension(chip);
941 chip->rear_opened++;
942 }
943 spin_unlock_irq(&chip->reg_lock);
944 return 0;
945}
946
Takashi Iwai208a1b42005-11-17 14:53:41 +0100947static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100949 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
950 struct snd_pcm_runtime *runtime = substream->runtime;
951 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 int err;
953
954 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
955 return err;
956 ypcm = runtime->private_data;
957 ypcm->output_front = 0;
958 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200959 ypcm->swap_rear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 spin_lock_irq(&chip->reg_lock);
961 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
962 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
963 ymfpci_open_extension(chip);
964 chip->spdif_pcm_bits = chip->spdif_bits;
965 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
966 chip->spdif_opened++;
967 spin_unlock_irq(&chip->reg_lock);
968
969 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
970 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
971 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
972 return 0;
973}
974
Takashi Iwai208a1b42005-11-17 14:53:41 +0100975static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100977 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
978 struct snd_pcm_runtime *runtime = substream->runtime;
979 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int err;
981
982 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
983 return err;
984 ypcm = runtime->private_data;
985 ypcm->output_front = 0;
986 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200987 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 spin_lock_irq(&chip->reg_lock);
989 ymfpci_open_extension(chip);
990 chip->rear_opened++;
991 spin_unlock_irq(&chip->reg_lock);
992 return 0;
993}
994
Takashi Iwai208a1b42005-11-17 14:53:41 +0100995static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 u32 capture_bank_number)
997{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100998 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
999 struct snd_pcm_runtime *runtime = substream->runtime;
1000 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001002 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (ypcm == NULL)
1004 return -ENOMEM;
1005 ypcm->chip = chip;
1006 ypcm->type = capture_bank_number + CAPTURE_REC;
1007 ypcm->substream = substream;
1008 ypcm->capture_bank_number = capture_bank_number;
1009 chip->capture_substream[capture_bank_number] = substream;
1010 runtime->hw = snd_ymfpci_capture;
1011 /* FIXME? True value is 256/48 = 5.33333 ms */
1012 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
1013 runtime->private_data = ypcm;
1014 runtime->private_free = snd_ymfpci_pcm_free_substream;
1015 snd_ymfpci_hw_start(chip);
1016 return 0;
1017}
1018
Takashi Iwai208a1b42005-11-17 14:53:41 +01001019static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 return snd_ymfpci_capture_open(substream, 0);
1022}
1023
Takashi Iwai208a1b42005-11-17 14:53:41 +01001024static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 return snd_ymfpci_capture_open(substream, 1);
1027}
1028
Takashi Iwai208a1b42005-11-17 14:53:41 +01001029static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 return 0;
1032}
1033
Takashi Iwai208a1b42005-11-17 14:53:41 +01001034static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001036 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1037 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 spin_lock_irq(&chip->reg_lock);
1040 if (ypcm->output_rear && chip->rear_opened > 0) {
1041 chip->rear_opened--;
1042 ymfpci_close_extension(chip);
1043 }
1044 spin_unlock_irq(&chip->reg_lock);
1045 return snd_ymfpci_playback_close_1(substream);
1046}
1047
Takashi Iwai208a1b42005-11-17 14:53:41 +01001048static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001050 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 spin_lock_irq(&chip->reg_lock);
1053 chip->spdif_opened = 0;
1054 ymfpci_close_extension(chip);
1055 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1056 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1057 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1058 spin_unlock_irq(&chip->reg_lock);
1059 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1060 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1061 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1062 return snd_ymfpci_playback_close_1(substream);
1063}
1064
Takashi Iwai208a1b42005-11-17 14:53:41 +01001065static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001067 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 spin_lock_irq(&chip->reg_lock);
1070 if (chip->rear_opened > 0) {
1071 chip->rear_opened--;
1072 ymfpci_close_extension(chip);
1073 }
1074 spin_unlock_irq(&chip->reg_lock);
1075 return snd_ymfpci_playback_close_1(substream);
1076}
1077
Takashi Iwai208a1b42005-11-17 14:53:41 +01001078static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001080 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1081 struct snd_pcm_runtime *runtime = substream->runtime;
1082 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 if (ypcm != NULL) {
1085 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1086 snd_ymfpci_hw_stop(chip);
1087 }
1088 return 0;
1089}
1090
Takashi Iwai208a1b42005-11-17 14:53:41 +01001091static struct snd_pcm_ops snd_ymfpci_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 .open = snd_ymfpci_playback_open,
1093 .close = snd_ymfpci_playback_close,
1094 .ioctl = snd_pcm_lib_ioctl,
1095 .hw_params = snd_ymfpci_playback_hw_params,
1096 .hw_free = snd_ymfpci_playback_hw_free,
1097 .prepare = snd_ymfpci_playback_prepare,
1098 .trigger = snd_ymfpci_playback_trigger,
1099 .pointer = snd_ymfpci_playback_pointer,
1100};
1101
Takashi Iwai208a1b42005-11-17 14:53:41 +01001102static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 .open = snd_ymfpci_capture_rec_open,
1104 .close = snd_ymfpci_capture_close,
1105 .ioctl = snd_pcm_lib_ioctl,
1106 .hw_params = snd_ymfpci_capture_hw_params,
1107 .hw_free = snd_ymfpci_capture_hw_free,
1108 .prepare = snd_ymfpci_capture_prepare,
1109 .trigger = snd_ymfpci_capture_trigger,
1110 .pointer = snd_ymfpci_capture_pointer,
1111};
1112
Takashi Iwai208a1b42005-11-17 14:53:41 +01001113int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001115 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 int err;
1117
1118 if (rpcm)
1119 *rpcm = NULL;
1120 if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1121 return err;
1122 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1125 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1126
1127 /* global setup */
1128 pcm->info_flags = 0;
1129 strcpy(pcm->name, "YMFPCI");
1130 chip->pcm = pcm;
1131
1132 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1133 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1134
1135 if (rpcm)
1136 *rpcm = pcm;
1137 return 0;
1138}
1139
Takashi Iwai208a1b42005-11-17 14:53:41 +01001140static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 .open = snd_ymfpci_capture_ac97_open,
1142 .close = snd_ymfpci_capture_close,
1143 .ioctl = snd_pcm_lib_ioctl,
1144 .hw_params = snd_ymfpci_capture_hw_params,
1145 .hw_free = snd_ymfpci_capture_hw_free,
1146 .prepare = snd_ymfpci_capture_prepare,
1147 .trigger = snd_ymfpci_capture_trigger,
1148 .pointer = snd_ymfpci_capture_pointer,
1149};
1150
Takashi Iwai208a1b42005-11-17 14:53:41 +01001151int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001153 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 int err;
1155
1156 if (rpcm)
1157 *rpcm = NULL;
1158 if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1159 return err;
1160 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1163
1164 /* global setup */
1165 pcm->info_flags = 0;
1166 sprintf(pcm->name, "YMFPCI - %s",
1167 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1168 chip->pcm2 = pcm;
1169
1170 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1171 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1172
1173 if (rpcm)
1174 *rpcm = pcm;
1175 return 0;
1176}
1177
Takashi Iwai208a1b42005-11-17 14:53:41 +01001178static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 .open = snd_ymfpci_playback_spdif_open,
1180 .close = snd_ymfpci_playback_spdif_close,
1181 .ioctl = snd_pcm_lib_ioctl,
1182 .hw_params = snd_ymfpci_playback_hw_params,
1183 .hw_free = snd_ymfpci_playback_hw_free,
1184 .prepare = snd_ymfpci_playback_prepare,
1185 .trigger = snd_ymfpci_playback_trigger,
1186 .pointer = snd_ymfpci_playback_pointer,
1187};
1188
Takashi Iwai208a1b42005-11-17 14:53:41 +01001189int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001191 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int err;
1193
1194 if (rpcm)
1195 *rpcm = NULL;
1196 if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1197 return err;
1198 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1201
1202 /* global setup */
1203 pcm->info_flags = 0;
1204 strcpy(pcm->name, "YMFPCI - IEC958");
1205 chip->pcm_spdif = pcm;
1206
1207 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1208 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1209
1210 if (rpcm)
1211 *rpcm = pcm;
1212 return 0;
1213}
1214
Takashi Iwai208a1b42005-11-17 14:53:41 +01001215static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 .open = snd_ymfpci_playback_4ch_open,
1217 .close = snd_ymfpci_playback_4ch_close,
1218 .ioctl = snd_pcm_lib_ioctl,
1219 .hw_params = snd_ymfpci_playback_hw_params,
1220 .hw_free = snd_ymfpci_playback_hw_free,
1221 .prepare = snd_ymfpci_playback_prepare,
1222 .trigger = snd_ymfpci_playback_trigger,
1223 .pointer = snd_ymfpci_playback_pointer,
1224};
1225
Takashi Iwai208a1b42005-11-17 14:53:41 +01001226int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001228 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int err;
1230
1231 if (rpcm)
1232 *rpcm = NULL;
1233 if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1234 return err;
1235 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1238
1239 /* global setup */
1240 pcm->info_flags = 0;
1241 strcpy(pcm->name, "YMFPCI - Rear PCM");
1242 chip->pcm_4ch = pcm;
1243
1244 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1245 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1246
1247 if (rpcm)
1248 *rpcm = pcm;
1249 return 0;
1250}
1251
Takashi Iwai208a1b42005-11-17 14:53:41 +01001252static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1255 uinfo->count = 1;
1256 return 0;
1257}
1258
Takashi Iwai208a1b42005-11-17 14:53:41 +01001259static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1260 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001262 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 spin_lock_irq(&chip->reg_lock);
1265 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1266 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001267 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 spin_unlock_irq(&chip->reg_lock);
1269 return 0;
1270}
1271
Takashi Iwai208a1b42005-11-17 14:53:41 +01001272static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1273 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001275 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 unsigned int val;
1277 int change;
1278
1279 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1280 (ucontrol->value.iec958.status[1] << 8);
1281 spin_lock_irq(&chip->reg_lock);
1282 change = chip->spdif_bits != val;
1283 chip->spdif_bits = val;
1284 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1285 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1286 spin_unlock_irq(&chip->reg_lock);
1287 return change;
1288}
1289
Takashi Iwai208a1b42005-11-17 14:53:41 +01001290static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1293 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1294 .info = snd_ymfpci_spdif_default_info,
1295 .get = snd_ymfpci_spdif_default_get,
1296 .put = snd_ymfpci_spdif_default_put
1297};
1298
Takashi Iwai208a1b42005-11-17 14:53:41 +01001299static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1302 uinfo->count = 1;
1303 return 0;
1304}
1305
Takashi Iwai208a1b42005-11-17 14:53:41 +01001306static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1307 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001309 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 spin_lock_irq(&chip->reg_lock);
1312 ucontrol->value.iec958.status[0] = 0x3e;
1313 ucontrol->value.iec958.status[1] = 0xff;
1314 spin_unlock_irq(&chip->reg_lock);
1315 return 0;
1316}
1317
Takashi Iwai208a1b42005-11-17 14:53:41 +01001318static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1321 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1322 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1323 .info = snd_ymfpci_spdif_mask_info,
1324 .get = snd_ymfpci_spdif_mask_get,
1325};
1326
Takashi Iwai208a1b42005-11-17 14:53:41 +01001327static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1330 uinfo->count = 1;
1331 return 0;
1332}
1333
Takashi Iwai208a1b42005-11-17 14:53:41 +01001334static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1335 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001337 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 spin_lock_irq(&chip->reg_lock);
1340 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1341 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001342 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 spin_unlock_irq(&chip->reg_lock);
1344 return 0;
1345}
1346
Takashi Iwai208a1b42005-11-17 14:53:41 +01001347static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1348 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001350 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 unsigned int val;
1352 int change;
1353
1354 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1355 (ucontrol->value.iec958.status[1] << 8);
1356 spin_lock_irq(&chip->reg_lock);
1357 change = chip->spdif_pcm_bits != val;
1358 chip->spdif_pcm_bits = val;
1359 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1360 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1361 spin_unlock_irq(&chip->reg_lock);
1362 return change;
1363}
1364
Takashi Iwai208a1b42005-11-17 14:53:41 +01001365static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1368 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1369 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1370 .info = snd_ymfpci_spdif_stream_info,
1371 .get = snd_ymfpci_spdif_stream_get,
1372 .put = snd_ymfpci_spdif_stream_put
1373};
1374
Takashi Iwai208a1b42005-11-17 14:53:41 +01001375static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};
1378
1379 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1380 info->count = 1;
1381 info->value.enumerated.items = 3;
1382 if (info->value.enumerated.item > 2)
1383 info->value.enumerated.item = 2;
1384 strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
1385 return 0;
1386}
1387
Takashi Iwai208a1b42005-11-17 14:53:41 +01001388static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001390 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 u16 reg;
1392
1393 spin_lock_irq(&chip->reg_lock);
1394 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1395 spin_unlock_irq(&chip->reg_lock);
1396 if (!(reg & 0x100))
1397 value->value.enumerated.item[0] = 0;
1398 else
1399 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1400 return 0;
1401}
1402
Takashi Iwai208a1b42005-11-17 14:53:41 +01001403static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001405 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 u16 reg, old_reg;
1407
1408 spin_lock_irq(&chip->reg_lock);
1409 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1410 if (value->value.enumerated.item[0] == 0)
1411 reg = old_reg & ~0x100;
1412 else
1413 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1414 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1415 spin_unlock_irq(&chip->reg_lock);
1416 return reg != old_reg;
1417}
1418
Takashi Iwai208a1b42005-11-17 14:53:41 +01001419static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1421 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1422 .name = "Direct Recording Source",
1423 .info = snd_ymfpci_drec_source_info,
1424 .get = snd_ymfpci_drec_source_get,
1425 .put = snd_ymfpci_drec_source_put
1426};
1427
1428/*
1429 * Mixer controls
1430 */
1431
Glen Masgaid602c882005-09-28 08:19:05 +02001432#define YMFPCI_SINGLE(xname, xindex, reg, shift) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1434 .info = snd_ymfpci_info_single, \
1435 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
Glen Masgaid602c882005-09-28 08:19:05 +02001436 .private_value = ((reg) | ((shift) << 16)) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001438#define snd_ymfpci_info_single snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Takashi Iwai208a1b42005-11-17 14:53:41 +01001440static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1441 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001443 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001444 int reg = kcontrol->private_value & 0xffff;
1445 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1446 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Glen Masgaid602c882005-09-28 08:19:05 +02001448 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 case YDSXGR_SPDIFOUTCTRL: break;
1450 case YDSXGR_SPDIFINCTRL: break;
1451 default: return -EINVAL;
1452 }
Glen Masgaid602c882005-09-28 08:19:05 +02001453 ucontrol->value.integer.value[0] =
1454 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return 0;
1456}
1457
Takashi Iwai208a1b42005-11-17 14:53:41 +01001458static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1459 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001461 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001462 int reg = kcontrol->private_value & 0xffff;
1463 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1464 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 int change;
1466 unsigned int val, oval;
1467
Glen Masgaid602c882005-09-28 08:19:05 +02001468 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 case YDSXGR_SPDIFOUTCTRL: break;
1470 case YDSXGR_SPDIFINCTRL: break;
1471 default: return -EINVAL;
1472 }
1473 val = (ucontrol->value.integer.value[0] & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 val <<= shift;
1475 spin_lock_irq(&chip->reg_lock);
1476 oval = snd_ymfpci_readl(chip, reg);
1477 val = (oval & ~(mask << shift)) | val;
1478 change = val != oval;
1479 snd_ymfpci_writel(chip, reg, val);
1480 spin_unlock_irq(&chip->reg_lock);
1481 return change;
1482}
1483
Takashi Iwai0cb29ea2007-01-29 15:33:49 +01001484static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
Takashi Iwai33925182006-08-28 13:29:42 +02001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486#define YMFPCI_DOUBLE(xname, xindex, reg) \
1487{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Takashi Iwai33925182006-08-28 13:29:42 +02001488 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 .info = snd_ymfpci_info_double, \
1490 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
Takashi Iwai33925182006-08-28 13:29:42 +02001491 .private_value = reg, \
1492 .tlv = { .p = db_scale_native } }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Takashi Iwai208a1b42005-11-17 14:53:41 +01001494static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 unsigned int reg = kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 if (reg < 0x80 || reg >= 0xc0)
1499 return -EINVAL;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001500 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 uinfo->count = 2;
1502 uinfo->value.integer.min = 0;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001503 uinfo->value.integer.max = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return 0;
1505}
1506
Takashi Iwai208a1b42005-11-17 14:53:41 +01001507static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001509 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001511 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 unsigned int val;
1513
1514 if (reg < 0x80 || reg >= 0xc0)
1515 return -EINVAL;
1516 spin_lock_irq(&chip->reg_lock);
1517 val = snd_ymfpci_readl(chip, reg);
1518 spin_unlock_irq(&chip->reg_lock);
1519 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1520 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return 0;
1522}
1523
Takashi Iwai208a1b42005-11-17 14:53:41 +01001524static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001526 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001528 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 int change;
1530 unsigned int val1, val2, oval;
1531
1532 if (reg < 0x80 || reg >= 0xc0)
1533 return -EINVAL;
1534 val1 = ucontrol->value.integer.value[0] & mask;
1535 val2 = ucontrol->value.integer.value[1] & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 val1 <<= shift_left;
1537 val2 <<= shift_right;
1538 spin_lock_irq(&chip->reg_lock);
1539 oval = snd_ymfpci_readl(chip, reg);
1540 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1541 change = val1 != oval;
1542 snd_ymfpci_writel(chip, reg, val1);
1543 spin_unlock_irq(&chip->reg_lock);
1544 return change;
1545}
1546
Clemens Ladisch177a7cd2007-07-23 17:38:44 +02001547static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1548 struct snd_ctl_elem_value *ucontrol)
1549{
1550 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1551 unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1552 unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1553 int change;
1554 unsigned int value, oval;
1555
1556 value = ucontrol->value.integer.value[0] & 0x3fff;
1557 value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1558 spin_lock_irq(&chip->reg_lock);
1559 oval = snd_ymfpci_readl(chip, reg);
1560 change = value != oval;
1561 snd_ymfpci_writel(chip, reg, value);
1562 snd_ymfpci_writel(chip, reg2, value);
1563 spin_unlock_irq(&chip->reg_lock);
1564 return change;
1565}
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567/*
1568 * 4ch duplication
1569 */
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001570#define snd_ymfpci_info_dup4ch snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Takashi Iwai208a1b42005-11-17 14:53:41 +01001572static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001574 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1576 return 0;
1577}
1578
Takashi Iwai208a1b42005-11-17 14:53:41 +01001579static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001581 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 int change;
1583 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1584 if (change)
1585 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1586 return change;
1587}
1588
1589
Takashi Iwai208a1b42005-11-17 14:53:41 +01001590static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
Clemens Ladisch177a7cd2007-07-23 17:38:44 +02001591{
1592 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1593 .name = "Wave Playback Volume",
1594 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1595 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1596 .info = snd_ymfpci_info_double,
1597 .get = snd_ymfpci_get_double,
1598 .put = snd_ymfpci_put_nativedacvol,
1599 .private_value = YDSXGR_NATIVEDACOUTVOL,
1600 .tlv = { .p = db_scale_native },
1601},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1603YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1604YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1605YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1606YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1607YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1608YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1609YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
1610YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1611YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1612YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1613YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
Glen Masgaid602c882005-09-28 08:19:05 +02001614YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1615YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1616YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1619 .name = "4ch Duplication",
1620 .info = snd_ymfpci_info_dup4ch,
1621 .get = snd_ymfpci_get_dup4ch,
1622 .put = snd_ymfpci_put_dup4ch,
1623},
1624};
1625
1626
1627/*
1628 * GPIO
1629 */
1630
Takashi Iwai208a1b42005-11-17 14:53:41 +01001631static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 u16 reg, mode;
1634 unsigned long flags;
1635
1636 spin_lock_irqsave(&chip->reg_lock, flags);
1637 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1638 reg &= ~(1 << (pin + 8));
1639 reg |= (1 << pin);
1640 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1641 /* set the level mode for input line */
1642 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1643 mode &= ~(3 << (pin * 2));
1644 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1645 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1646 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1647 spin_unlock_irqrestore(&chip->reg_lock, flags);
1648 return (mode >> pin) & 1;
1649}
1650
Takashi Iwai208a1b42005-11-17 14:53:41 +01001651static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
1653 u16 reg;
1654 unsigned long flags;
1655
1656 spin_lock_irqsave(&chip->reg_lock, flags);
1657 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1658 reg &= ~(1 << pin);
1659 reg &= ~(1 << (pin + 8));
1660 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1661 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1662 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1663 spin_unlock_irqrestore(&chip->reg_lock, flags);
1664
1665 return 0;
1666}
1667
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001668#define snd_ymfpci_gpio_sw_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Takashi Iwai208a1b42005-11-17 14:53:41 +01001670static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001672 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 int pin = (int)kcontrol->private_value;
1674 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1675 return 0;
1676}
1677
Takashi Iwai208a1b42005-11-17 14:53:41 +01001678static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001680 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 int pin = (int)kcontrol->private_value;
1682
1683 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1684 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1685 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1686 return 1;
1687 }
1688 return 0;
1689}
1690
Takashi Iwai208a1b42005-11-17 14:53:41 +01001691static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 .name = "Shared Rear/Line-In Switch",
1693 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1694 .info = snd_ymfpci_gpio_sw_info,
1695 .get = snd_ymfpci_gpio_sw_get,
1696 .put = snd_ymfpci_gpio_sw_put,
1697 .private_value = 2,
1698};
1699
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001700/*
1701 * PCM voice volume
1702 */
1703
Takashi Iwai208a1b42005-11-17 14:53:41 +01001704static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1705 struct snd_ctl_elem_info *uinfo)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001706{
1707 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1708 uinfo->count = 2;
1709 uinfo->value.integer.min = 0;
1710 uinfo->value.integer.max = 0x8000;
1711 return 0;
1712}
1713
Takashi Iwai208a1b42005-11-17 14:53:41 +01001714static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001716{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001717 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001718 unsigned int subs = kcontrol->id.subdevice;
1719
1720 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1721 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1722 return 0;
1723}
1724
Takashi Iwai208a1b42005-11-17 14:53:41 +01001725static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1726 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001727{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001728 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001729 unsigned int subs = kcontrol->id.subdevice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001730 struct snd_pcm_substream *substream;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001731 unsigned long flags;
1732
1733 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1734 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1735 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1736 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001737 if (chip->pcm_mixer[subs].left > 0x8000)
1738 chip->pcm_mixer[subs].left = 0x8000;
1739 if (chip->pcm_mixer[subs].right > 0x8000)
1740 chip->pcm_mixer[subs].right = 0x8000;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001741
Takashi Iwai208a1b42005-11-17 14:53:41 +01001742 substream = (struct snd_pcm_substream *)kcontrol->private_value;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001743 spin_lock_irqsave(&chip->voice_lock, flags);
1744 if (substream->runtime && substream->runtime->private_data) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01001745 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01001746 if (!ypcm->use_441_slot)
1747 ypcm->update_pcm_vol = 2;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001748 }
1749 spin_unlock_irqrestore(&chip->voice_lock, flags);
1750 return 1;
1751 }
1752 return 0;
1753}
1754
Takashi Iwai208a1b42005-11-17 14:53:41 +01001755static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001756 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1757 .name = "PCM Playback Volume",
1758 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1759 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1760 .info = snd_ymfpci_pcm_vol_info,
1761 .get = snd_ymfpci_pcm_vol_get,
1762 .put = snd_ymfpci_pcm_vol_put,
1763};
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766/*
1767 * Mixer routines
1768 */
1769
Takashi Iwai208a1b42005-11-17 14:53:41 +01001770static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001772 struct snd_ymfpci *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 chip->ac97_bus = NULL;
1774}
1775
Takashi Iwai208a1b42005-11-17 14:53:41 +01001776static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001778 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 chip->ac97 = NULL;
1780}
1781
Glen Masgaid9301262006-10-10 09:27:19 +02001782int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001784 struct snd_ac97_template ac97;
1785 struct snd_kcontrol *kctl;
1786 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 unsigned int idx;
1788 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001789 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 .write = snd_ymfpci_codec_write,
1791 .read = snd_ymfpci_codec_read,
1792 };
1793
1794 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1795 return err;
1796 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1797 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1798
1799 memset(&ac97, 0, sizeof(ac97));
1800 ac97.private_data = chip;
1801 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1802 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1803 return err;
1804
1805 /* to be sure */
1806 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1807 AC97_EA_VRA|AC97_EA_VRM, 0);
1808
1809 for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1810 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1811 return err;
1812 }
1813
1814 /* add S/PDIF control */
1815 snd_assert(chip->pcm_spdif != NULL, return -EIO);
1816 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1817 return err;
1818 kctl->id.device = chip->pcm_spdif->device;
1819 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1820 return err;
1821 kctl->id.device = chip->pcm_spdif->device;
1822 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1823 return err;
1824 kctl->id.device = chip->pcm_spdif->device;
1825 chip->spdif_pcm_ctl = kctl;
1826
1827 /* direct recording source */
1828 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1829 (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1830 return err;
1831
1832 /*
1833 * shared rear/line-in
1834 */
1835 if (rear_switch) {
1836 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1837 return err;
1838 }
1839
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001840 /* per-voice volume */
1841 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1842 for (idx = 0; idx < 32; ++idx) {
1843 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1844 if (!kctl)
1845 return -ENOMEM;
1846 kctl->id.device = chip->pcm->device;
1847 kctl->id.subdevice = idx;
1848 kctl->private_value = (unsigned long)substream;
1849 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1850 return err;
1851 chip->pcm_mixer[idx].left = 0x8000;
1852 chip->pcm_mixer[idx].right = 0x8000;
1853 chip->pcm_mixer[idx].ctl = kctl;
1854 substream = substream->next;
1855 }
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return 0;
1858}
1859
1860
1861/*
1862 * timer
1863 */
1864
Takashi Iwai208a1b42005-11-17 14:53:41 +01001865static int snd_ymfpci_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001867 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 unsigned long flags;
1869 unsigned int count;
1870
1871 chip = snd_timer_chip(timer);
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001872 count = (timer->sticks << 1) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 spin_lock_irqsave(&chip->reg_lock, flags);
1874 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1875 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1876 spin_unlock_irqrestore(&chip->reg_lock, flags);
1877 return 0;
1878}
1879
Takashi Iwai208a1b42005-11-17 14:53:41 +01001880static int snd_ymfpci_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001882 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 unsigned long flags;
1884
1885 chip = snd_timer_chip(timer);
1886 spin_lock_irqsave(&chip->reg_lock, flags);
1887 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1888 spin_unlock_irqrestore(&chip->reg_lock, flags);
1889 return 0;
1890}
1891
Takashi Iwai208a1b42005-11-17 14:53:41 +01001892static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 unsigned long *num, unsigned long *den)
1894{
1895 *num = 1;
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001896 *den = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return 0;
1898}
1899
Takashi Iwai208a1b42005-11-17 14:53:41 +01001900static struct snd_timer_hardware snd_ymfpci_timer_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 .flags = SNDRV_TIMER_HW_AUTO,
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001902 .resolution = 20833, /* 1/fs = 20.8333...us */
1903 .ticks = 0x8000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 .start = snd_ymfpci_timer_start,
1905 .stop = snd_ymfpci_timer_stop,
1906 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1907};
1908
Takashi Iwai208a1b42005-11-17 14:53:41 +01001909int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001911 struct snd_timer *timer = NULL;
1912 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 int err;
1914
1915 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1916 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1917 tid.card = chip->card->number;
1918 tid.device = device;
1919 tid.subdevice = 0;
1920 if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1921 strcpy(timer->name, "YMFPCI timer");
1922 timer->private_data = chip;
1923 timer->hw = snd_ymfpci_timer_hw;
1924 }
1925 chip->timer = timer;
1926 return err;
1927}
1928
1929
1930/*
1931 * proc interface
1932 */
1933
Takashi Iwai208a1b42005-11-17 14:53:41 +01001934static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1935 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001937 struct snd_ymfpci *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 int i;
1939
1940 snd_iprintf(buffer, "YMFPCI\n\n");
1941 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1942 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1943}
1944
Takashi Iwai208a1b42005-11-17 14:53:41 +01001945static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001947 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 if (! snd_card_proc_new(card, "ymfpci", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001950 snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return 0;
1952}
1953
1954/*
1955 * initialization routines
1956 */
1957
1958static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1959{
1960 u8 cmd;
1961
1962 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1963#if 0 // force to reset
1964 if (cmd & 0x03) {
1965#endif
1966 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1967 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1968 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1969 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1970 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1971#if 0
1972 }
1973#endif
1974}
1975
Takashi Iwai208a1b42005-11-17 14:53:41 +01001976static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1979}
1980
Takashi Iwai208a1b42005-11-17 14:53:41 +01001981static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
1983 u32 val;
1984 int timeout = 1000;
1985
1986 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1987 if (val)
1988 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
1989 while (timeout-- > 0) {
1990 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
1991 if ((val & 0x00000002) == 0)
1992 break;
1993 }
1994}
1995
Takashi Iwai8ad2da12007-02-26 15:55:43 +01001996#ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
Clemens Ladisch102fa902006-10-11 12:05:59 +02001997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998#include "ymfpci_image.h"
1999
Clemens Ladisch102fa902006-10-11 12:05:59 +02002000static struct firmware snd_ymfpci_dsp_microcode = {
2001 .size = YDSXG_DSPLENGTH,
2002 .data = (u8 *)DspInst,
2003};
2004static struct firmware snd_ymfpci_controller_microcode = {
2005 .size = YDSXG_CTRLLENGTH,
2006 .data = (u8 *)CntrlInst,
2007};
2008static struct firmware snd_ymfpci_controller_1e_microcode = {
2009 .size = YDSXG_CTRLLENGTH,
2010 .data = (u8 *)CntrlInst1E,
2011};
2012#endif
2013
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002014#ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
2015static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2016{
2017 chip->dsp_microcode = &snd_ymfpci_dsp_microcode;
2018 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2019 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2020 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2021 chip->device_id == PCI_DEVICE_ID_YAMAHA_754)
2022 chip->controller_microcode =
2023 &snd_ymfpci_controller_1e_microcode;
2024 else
2025 chip->controller_microcode =
2026 &snd_ymfpci_controller_microcode;
2027 return 0;
2028}
2029
2030#else /* use fw_loader */
2031
Clemens Ladisch102fa902006-10-11 12:05:59 +02002032#ifdef __LITTLE_ENDIAN
2033static inline void snd_ymfpci_convert_from_le(const struct firmware *fw) { }
2034#else
2035static void snd_ymfpci_convert_from_le(const struct firmware *fw)
2036{
2037 int i;
2038 u32 *data = (u32 *)fw->data;
2039
2040 for (i = 0; i < fw->size / 4; ++i)
2041 le32_to_cpus(&data[i]);
2042}
2043#endif
2044
2045static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2046{
2047 int err, is_1e;
2048 const char *name;
2049
2050 err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2051 &chip->pci->dev);
2052 if (err >= 0) {
2053 if (chip->dsp_microcode->size == YDSXG_DSPLENGTH)
2054 snd_ymfpci_convert_from_le(chip->dsp_microcode);
2055 else {
2056 snd_printk(KERN_ERR "DSP microcode has wrong size\n");
2057 err = -EINVAL;
2058 }
2059 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002060 if (err < 0)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002061 return err;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002062 is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2063 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2064 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2065 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2066 name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2067 err = request_firmware(&chip->controller_microcode, name,
2068 &chip->pci->dev);
2069 if (err >= 0) {
2070 if (chip->controller_microcode->size == YDSXG_CTRLLENGTH)
2071 snd_ymfpci_convert_from_le(chip->controller_microcode);
2072 else {
2073 snd_printk(KERN_ERR "controller microcode"
2074 " has wrong size\n");
2075 err = -EINVAL;
2076 }
2077 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002078 if (err < 0)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002079 return err;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002080 return 0;
2081}
Clemens Ladisch7e0af292007-05-03 17:59:54 +02002082
2083MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
2084MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
2085MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
2086
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002087#endif
Clemens Ladisch102fa902006-10-11 12:05:59 +02002088
Takashi Iwai208a1b42005-11-17 14:53:41 +01002089static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 int i;
2092 u16 ctrl;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002093 u32 *inst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2096 snd_ymfpci_disable_dsp(chip);
2097 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2098 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2099 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2100 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2101 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2102 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2103 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2104 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2105 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2106
2107 /* setup DSP instruction code */
Clemens Ladisch102fa902006-10-11 12:05:59 +02002108 inst = (u32 *)chip->dsp_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002110 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), inst[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 /* setup control instruction code */
Clemens Ladisch102fa902006-10-11 12:05:59 +02002113 inst = (u32 *)chip->controller_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2115 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), inst[i]);
2116
2117 snd_ymfpci_enable_dsp(chip);
2118}
2119
Takashi Iwai208a1b42005-11-17 14:53:41 +01002120static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 long size, playback_ctrl_size;
2123 int voice, bank, reg;
2124 u8 *ptr;
2125 dma_addr_t ptr_addr;
2126
2127 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2128 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2129 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2130 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2131 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2132
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002133 size = ALIGN(playback_ctrl_size, 0x100) +
2134 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2135 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2136 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 chip->work_size;
2138 /* work_ptr must be aligned to 256 bytes, but it's already
2139 covered with the kernel page allocation mechanism */
2140 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2141 size, &chip->work_ptr) < 0)
2142 return -ENOMEM;
2143 ptr = chip->work_ptr.area;
2144 ptr_addr = chip->work_ptr.addr;
2145 memset(ptr, 0, size); /* for sure */
2146
2147 chip->bank_base_playback = ptr;
2148 chip->bank_base_playback_addr = ptr_addr;
2149 chip->ctrl_playback = (u32 *)ptr;
2150 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002151 ptr += ALIGN(playback_ctrl_size, 0x100);
2152 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2154 chip->voices[voice].number = voice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002155 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 chip->voices[voice].bank_addr = ptr_addr;
2157 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002158 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 ptr += chip->bank_size_playback;
2160 ptr_addr += chip->bank_size_playback;
2161 }
2162 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002163 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2164 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 chip->bank_base_capture = ptr;
2166 chip->bank_base_capture_addr = ptr_addr;
2167 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2168 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002169 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 ptr += chip->bank_size_capture;
2171 ptr_addr += chip->bank_size_capture;
2172 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002173 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2174 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 chip->bank_base_effect = ptr;
2176 chip->bank_base_effect_addr = ptr_addr;
2177 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2178 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002179 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 ptr += chip->bank_size_effect;
2181 ptr_addr += chip->bank_size_effect;
2182 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002183 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2184 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 chip->work_base = ptr;
2186 chip->work_base_addr = ptr_addr;
2187
2188 snd_assert(ptr + chip->work_size == chip->work_ptr.area + chip->work_ptr.bytes, );
2189
2190 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2191 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2192 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2193 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2194 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2195
2196 /* S/PDIF output initialization */
2197 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2198 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2199 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2200
2201 /* S/PDIF input initialization */
2202 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2203
2204 /* digital mixer setup */
2205 for (reg = 0x80; reg < 0xc0; reg += 4)
2206 snd_ymfpci_writel(chip, reg, 0);
2207 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2208 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2209 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2210 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2211 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2212 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2213 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2214
2215 return 0;
2216}
2217
Takashi Iwai208a1b42005-11-17 14:53:41 +01002218static int snd_ymfpci_free(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
2220 u16 ctrl;
2221
2222 snd_assert(chip != NULL, return -EINVAL);
2223
2224 if (chip->res_reg_area) { /* don't touch busy hardware */
2225 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2226 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2227 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2228 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2229 snd_ymfpci_disable_dsp(chip);
2230 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2231 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2232 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2233 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2234 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2235 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2236 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2237 }
2238
2239 snd_ymfpci_ac3_done(chip);
2240
2241 /* Set PCI device to D3 state */
2242#if 0
2243 /* FIXME: temporarily disabled, otherwise we cannot fire up
2244 * the chip again unless reboot. ACPI bug?
2245 */
2246 pci_set_power_state(chip->pci, 3);
2247#endif
2248
2249#ifdef CONFIG_PM
2250 vfree(chip->saved_regs);
2251#endif
Takashi Iwai95866d382008-03-22 10:11:08 +01002252 if (chip->irq >= 0)
2253 free_irq(chip->irq, chip);
Takashi Iwaib1d57762005-10-10 11:56:31 +02002254 release_and_free_resource(chip->mpu_res);
2255 release_and_free_resource(chip->fm_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 snd_ymfpci_free_gameport(chip);
2257 if (chip->reg_area_virt)
2258 iounmap(chip->reg_area_virt);
2259 if (chip->work_ptr.area)
2260 snd_dma_free_pages(&chip->work_ptr);
2261
Takashi Iwaib1d57762005-10-10 11:56:31 +02002262 release_and_free_resource(chip->res_reg_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2265
2266 pci_disable_device(chip->pci);
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002267#ifndef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
2268 release_firmware(chip->dsp_microcode);
2269 release_firmware(chip->controller_microcode);
Clemens Ladisch102fa902006-10-11 12:05:59 +02002270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 kfree(chip);
2272 return 0;
2273}
2274
Takashi Iwai208a1b42005-11-17 14:53:41 +01002275static int snd_ymfpci_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002277 struct snd_ymfpci *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return snd_ymfpci_free(chip);
2279}
2280
2281#ifdef CONFIG_PM
2282static int saved_regs_index[] = {
2283 /* spdif */
2284 YDSXGR_SPDIFOUTCTRL,
2285 YDSXGR_SPDIFOUTSTATUS,
2286 YDSXGR_SPDIFINCTRL,
2287 /* volumes */
2288 YDSXGR_PRIADCLOOPVOL,
2289 YDSXGR_NATIVEDACINVOL,
2290 YDSXGR_NATIVEDACOUTVOL,
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01002291 YDSXGR_BUF441OUTVOL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 YDSXGR_NATIVEADCINVOL,
2293 YDSXGR_SPDIFLOOPVOL,
2294 YDSXGR_SPDIFOUTVOL,
2295 YDSXGR_ZVOUTVOL,
2296 YDSXGR_LEGACYOUTVOL,
2297 /* address bases */
2298 YDSXGR_PLAYCTRLBASE,
2299 YDSXGR_RECCTRLBASE,
2300 YDSXGR_EFFCTRLBASE,
2301 YDSXGR_WORKBASE,
2302 /* capture set up */
2303 YDSXGR_MAPOFREC,
2304 YDSXGR_RECFORMAT,
2305 YDSXGR_RECSLOTSR,
2306 YDSXGR_ADCFORMAT,
2307 YDSXGR_ADCSLOTSR,
2308};
2309#define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
2310
Takashi Iwaided46232005-11-17 16:09:43 +01002311int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Takashi Iwaided46232005-11-17 16:09:43 +01002313 struct snd_card *card = pci_get_drvdata(pci);
2314 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 unsigned int i;
2316
Takashi Iwaided46232005-11-17 16:09:43 +01002317 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 snd_pcm_suspend_all(chip->pcm);
2319 snd_pcm_suspend_all(chip->pcm2);
2320 snd_pcm_suspend_all(chip->pcm_spdif);
2321 snd_pcm_suspend_all(chip->pcm_4ch);
2322 snd_ac97_suspend(chip->ac97);
2323 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2324 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2325 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2326 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2327 snd_ymfpci_disable_dsp(chip);
Takashi Iwaided46232005-11-17 16:09:43 +01002328 pci_disable_device(pci);
2329 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002330 pci_set_power_state(pci, pci_choose_state(pci, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return 0;
2332}
2333
Takashi Iwaided46232005-11-17 16:09:43 +01002334int snd_ymfpci_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Takashi Iwaided46232005-11-17 16:09:43 +01002336 struct snd_card *card = pci_get_drvdata(pci);
2337 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 unsigned int i;
2339
Takashi Iwai30b35392006-10-11 18:52:53 +02002340 pci_set_power_state(pci, PCI_D0);
Takashi Iwaided46232005-11-17 16:09:43 +01002341 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002342 if (pci_enable_device(pci) < 0) {
2343 printk(KERN_ERR "ymfpci: pci_enable_device failed, "
2344 "disabling device\n");
2345 snd_card_disconnect(card);
2346 return -EIO;
2347 }
Takashi Iwaided46232005-11-17 16:09:43 +01002348 pci_set_master(pci);
2349 snd_ymfpci_aclink_reset(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 snd_ymfpci_codec_ready(chip, 0);
2351 snd_ymfpci_download_image(chip);
2352 udelay(100);
2353
2354 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2355 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2356
2357 snd_ac97_resume(chip->ac97);
2358
2359 /* start hw again */
2360 if (chip->start_count > 0) {
2361 spin_lock_irq(&chip->reg_lock);
2362 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2363 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2364 spin_unlock_irq(&chip->reg_lock);
2365 }
Takashi Iwaided46232005-11-17 16:09:43 +01002366 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return 0;
2368}
2369#endif /* CONFIG_PM */
2370
Takashi Iwai208a1b42005-11-17 14:53:41 +01002371int __devinit snd_ymfpci_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 struct pci_dev * pci,
2373 unsigned short old_legacy_ctrl,
Takashi Iwai208a1b42005-11-17 14:53:41 +01002374 struct snd_ymfpci ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002376 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002378 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 .dev_free = snd_ymfpci_dev_free,
2380 };
2381
2382 *rchip = NULL;
2383
2384 /* enable PCI device */
2385 if ((err = pci_enable_device(pci)) < 0)
2386 return err;
2387
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002388 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (chip == NULL) {
2390 pci_disable_device(pci);
2391 return -ENOMEM;
2392 }
2393 chip->old_legacy_ctrl = old_legacy_ctrl;
2394 spin_lock_init(&chip->reg_lock);
2395 spin_lock_init(&chip->voice_lock);
2396 init_waitqueue_head(&chip->interrupt_sleep);
2397 atomic_set(&chip->interrupt_sleep_count, 0);
2398 chip->card = card;
2399 chip->pci = pci;
2400 chip->irq = -1;
2401 chip->device_id = pci->device;
Auke Kok44c10132007-06-08 15:46:36 -07002402 chip->rev = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 chip->reg_area_phys = pci_resource_start(pci, 0);
2404 chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2405 pci_set_master(pci);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01002406 chip->src441_used = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002409 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 -07002410 snd_ymfpci_free(chip);
2411 return -EBUSY;
2412 }
Takashi Iwai437a5a42006-11-21 12:14:23 +01002413 if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2414 "YMFPCI", chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002415 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 snd_ymfpci_free(chip);
2417 return -EBUSY;
2418 }
2419 chip->irq = pci->irq;
2420
2421 snd_ymfpci_aclink_reset(pci);
2422 if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2423 snd_ymfpci_free(chip);
2424 return -EIO;
2425 }
2426
Clemens Ladisch102fa902006-10-11 12:05:59 +02002427 err = snd_ymfpci_request_firmware(chip);
2428 if (err < 0) {
2429 snd_printk(KERN_ERR "firmware request failed: %d\n", err);
2430 snd_ymfpci_free(chip);
2431 return err;
2432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 snd_ymfpci_download_image(chip);
2434
2435 udelay(100); /* seems we need a delay after downloading image.. */
2436
2437 if (snd_ymfpci_memalloc(chip) < 0) {
2438 snd_ymfpci_free(chip);
2439 return -EIO;
2440 }
2441
2442 if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
2443 snd_ymfpci_free(chip);
2444 return err;
2445 }
2446
2447#ifdef CONFIG_PM
2448 chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
2449 if (chip->saved_regs == NULL) {
2450 snd_ymfpci_free(chip);
2451 return -ENOMEM;
2452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453#endif
2454
2455 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2456 snd_ymfpci_free(chip);
2457 return err;
2458 }
2459
2460 snd_ymfpci_proc_init(card, chip);
2461
2462 snd_card_set_dev(card, &pci->dev);
2463
2464 *rchip = chip;
2465 return 0;
2466}