blob: e33e4aa6855c2c69bf9087c3c8e24de3e11835c1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Routines for control of YMF724/740/744/754 chips
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include <sound/driver.h>
22#include <linux/delay.h>
Clemens Ladisch102fa902006-10-11 12:05:59 +020023#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/pci.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/vmalloc.h>
30
31#include <sound/core.h>
32#include <sound/control.h>
33#include <sound/info.h>
Takashi Iwai33925182006-08-28 13:29:42 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/ymfpci.h>
36#include <sound/asoundef.h>
37#include <sound/mpu401.h>
38
39#include <asm/io.h>
Clemens Ladisch102fa902006-10-11 12:05:59 +020040#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * common I/O routines
44 */
45
Takashi Iwai208a1b42005-11-17 14:53:41 +010046static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Takashi Iwai208a1b42005-11-17 14:53:41 +010048static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 return readb(chip->reg_area_virt + offset);
51}
52
Takashi Iwai208a1b42005-11-17 14:53:41 +010053static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 writeb(val, chip->reg_area_virt + offset);
56}
57
Takashi Iwai208a1b42005-11-17 14:53:41 +010058static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 return readw(chip->reg_area_virt + offset);
61}
62
Takashi Iwai208a1b42005-11-17 14:53:41 +010063static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 writew(val, chip->reg_area_virt + offset);
66}
67
Takashi Iwai208a1b42005-11-17 14:53:41 +010068static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 return readl(chip->reg_area_virt + offset);
71}
72
Takashi Iwai208a1b42005-11-17 14:53:41 +010073static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 writel(val, chip->reg_area_virt + offset);
76}
77
Takashi Iwai208a1b42005-11-17 14:53:41 +010078static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020080 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
82
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020083 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 do {
85 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
86 return 0;
87 set_current_state(TASK_UNINTERRUPTIBLE);
Nishanth Aravamudan8433a502005-10-24 15:02:37 +020088 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020089 } while (time_before(jiffies, end_time));
Takashi Iwai99b359b2005-10-20 18:26:44 +020090 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return -EBUSY;
92}
93
Takashi Iwai208a1b42005-11-17 14:53:41 +010094static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Takashi Iwai208a1b42005-11-17 14:53:41 +010096 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 u32 cmd;
98
99 snd_ymfpci_codec_ready(chip, 0);
100 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
101 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
102}
103
Takashi Iwai208a1b42005-11-17 14:53:41 +0100104static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100106 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if (snd_ymfpci_codec_ready(chip, 0))
109 return ~0;
110 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
111 if (snd_ymfpci_codec_ready(chip, 0))
112 return ~0;
113 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
114 int i;
115 for (i = 0; i < 600; i++)
116 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
117 }
118 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
119}
120
121/*
122 * Misc routines
123 */
124
125static u32 snd_ymfpci_calc_delta(u32 rate)
126{
127 switch (rate) {
128 case 8000: return 0x02aaab00;
129 case 11025: return 0x03accd00;
130 case 16000: return 0x05555500;
131 case 22050: return 0x07599a00;
132 case 32000: return 0x0aaaab00;
133 case 44100: return 0x0eb33300;
134 default: return ((rate << 16) / 375) << 5;
135 }
136}
137
138static u32 def_rate[8] = {
139 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
140};
141
142static u32 snd_ymfpci_calc_lpfK(u32 rate)
143{
144 u32 i;
145 static u32 val[8] = {
146 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
147 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
148 };
149
150 if (rate == 44100)
151 return 0x40000000; /* FIXME: What's the right value? */
152 for (i = 0; i < 8; i++)
153 if (rate <= def_rate[i])
154 return val[i];
155 return val[0];
156}
157
158static u32 snd_ymfpci_calc_lpfQ(u32 rate)
159{
160 u32 i;
161 static u32 val[8] = {
162 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
163 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
164 };
165
166 if (rate == 44100)
167 return 0x370A0000;
168 for (i = 0; i < 8; i++)
169 if (rate <= def_rate[i])
170 return val[i];
171 return val[0];
172}
173
174/*
175 * Hardware start management
176 */
177
Takashi Iwai208a1b42005-11-17 14:53:41 +0100178static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 unsigned long flags;
181
182 spin_lock_irqsave(&chip->reg_lock, flags);
183 if (chip->start_count++ > 0)
184 goto __end;
185 snd_ymfpci_writel(chip, YDSXGR_MODE,
186 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
187 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
188 __end:
189 spin_unlock_irqrestore(&chip->reg_lock, flags);
190}
191
Takashi Iwai208a1b42005-11-17 14:53:41 +0100192static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 unsigned long flags;
195 long timeout = 1000;
196
197 spin_lock_irqsave(&chip->reg_lock, flags);
198 if (--chip->start_count > 0)
199 goto __end;
200 snd_ymfpci_writel(chip, YDSXGR_MODE,
201 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
202 while (timeout-- > 0) {
203 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
204 break;
205 }
206 if (atomic_read(&chip->interrupt_sleep_count)) {
207 atomic_set(&chip->interrupt_sleep_count, 0);
208 wake_up(&chip->interrupt_sleep);
209 }
210 __end:
211 spin_unlock_irqrestore(&chip->reg_lock, flags);
212}
213
214/*
215 * Playback voice management
216 */
217
Takashi Iwai208a1b42005-11-17 14:53:41 +0100218static int voice_alloc(struct snd_ymfpci *chip,
219 enum snd_ymfpci_voice_type type, int pair,
220 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100222 struct snd_ymfpci_voice *voice, *voice2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 int idx;
224
225 *rvoice = NULL;
226 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
227 voice = &chip->voices[idx];
228 voice2 = pair ? &chip->voices[idx+1] : NULL;
229 if (voice->use || (voice2 && voice2->use))
230 continue;
231 voice->use = 1;
232 if (voice2)
233 voice2->use = 1;
234 switch (type) {
235 case YMFPCI_PCM:
236 voice->pcm = 1;
237 if (voice2)
238 voice2->pcm = 1;
239 break;
240 case YMFPCI_SYNTH:
241 voice->synth = 1;
242 break;
243 case YMFPCI_MIDI:
244 voice->midi = 1;
245 break;
246 }
247 snd_ymfpci_hw_start(chip);
248 if (voice2)
249 snd_ymfpci_hw_start(chip);
250 *rvoice = voice;
251 return 0;
252 }
253 return -ENOMEM;
254}
255
Takashi Iwai208a1b42005-11-17 14:53:41 +0100256static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
257 enum snd_ymfpci_voice_type type, int pair,
258 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 unsigned long flags;
261 int result;
262
263 snd_assert(rvoice != NULL, return -EINVAL);
264 snd_assert(!pair || type == YMFPCI_PCM, return -EINVAL);
265
266 spin_lock_irqsave(&chip->voice_lock, flags);
267 for (;;) {
268 result = voice_alloc(chip, type, pair, rvoice);
269 if (result == 0 || type != YMFPCI_PCM)
270 break;
271 /* TODO: synth/midi voice deallocation */
272 break;
273 }
274 spin_unlock_irqrestore(&chip->voice_lock, flags);
275 return result;
276}
277
Takashi Iwai208a1b42005-11-17 14:53:41 +0100278static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 unsigned long flags;
281
282 snd_assert(pvoice != NULL, return -EINVAL);
283 snd_ymfpci_hw_stop(chip);
284 spin_lock_irqsave(&chip->voice_lock, flags);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100285 if (pvoice->number == chip->src441_used) {
286 chip->src441_used = -1;
287 pvoice->ypcm->use_441_slot = 0;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
290 pvoice->ypcm = NULL;
291 pvoice->interrupt = NULL;
292 spin_unlock_irqrestore(&chip->voice_lock, flags);
293 return 0;
294}
295
296/*
297 * PCM part
298 */
299
Takashi Iwai208a1b42005-11-17 14:53:41 +0100300static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100302 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 u32 pos, delta;
304
305 if ((ypcm = voice->ypcm) == NULL)
306 return;
307 if (ypcm->substream == NULL)
308 return;
309 spin_lock(&chip->reg_lock);
310 if (ypcm->running) {
311 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
312 if (pos < ypcm->last_pos)
313 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
314 else
315 delta = pos - ypcm->last_pos;
316 ypcm->period_pos += delta;
317 ypcm->last_pos = pos;
318 if (ypcm->period_pos >= ypcm->period_size) {
319 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
320 ypcm->period_pos %= ypcm->period_size;
321 spin_unlock(&chip->reg_lock);
322 snd_pcm_period_elapsed(ypcm->substream);
323 spin_lock(&chip->reg_lock);
324 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200325
326 if (unlikely(ypcm->update_pcm_vol)) {
327 unsigned int subs = ypcm->substream->number;
328 unsigned int next_bank = 1 - chip->active_bank;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100329 struct snd_ymfpci_playback_bank *bank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200330 u32 volume;
331
332 bank = &voice->bank[next_bank];
333 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
334 bank->left_gain_end = volume;
335 if (ypcm->output_rear)
336 bank->eff2_gain_end = volume;
337 if (ypcm->voices[1])
338 bank = &ypcm->voices[1]->bank[next_bank];
339 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
340 bank->right_gain_end = volume;
341 if (ypcm->output_rear)
342 bank->eff3_gain_end = volume;
343 ypcm->update_pcm_vol--;
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346 spin_unlock(&chip->reg_lock);
347}
348
Takashi Iwai208a1b42005-11-17 14:53:41 +0100349static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100351 struct snd_pcm_runtime *runtime = substream->runtime;
352 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
353 struct snd_ymfpci *chip = ypcm->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 u32 pos, delta;
355
356 spin_lock(&chip->reg_lock);
357 if (ypcm->running) {
358 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
359 if (pos < ypcm->last_pos)
360 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
361 else
362 delta = pos - ypcm->last_pos;
363 ypcm->period_pos += delta;
364 ypcm->last_pos = pos;
365 if (ypcm->period_pos >= ypcm->period_size) {
366 ypcm->period_pos %= ypcm->period_size;
367 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
368 spin_unlock(&chip->reg_lock);
369 snd_pcm_period_elapsed(substream);
370 spin_lock(&chip->reg_lock);
371 }
372 }
373 spin_unlock(&chip->reg_lock);
374}
375
Takashi Iwai208a1b42005-11-17 14:53:41 +0100376static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int cmd)
378{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100379 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
380 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200381 struct snd_kcontrol *kctl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 int result = 0;
383
384 spin_lock(&chip->reg_lock);
385 if (ypcm->voices[0] == NULL) {
386 result = -EINVAL;
387 goto __unlock;
388 }
389 switch (cmd) {
390 case SNDRV_PCM_TRIGGER_START:
391 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
392 case SNDRV_PCM_TRIGGER_RESUME:
393 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100394 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
396 ypcm->running = 1;
397 break;
398 case SNDRV_PCM_TRIGGER_STOP:
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200399 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
400 kctl = chip->pcm_mixer[substream->number].ctl;
401 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
402 }
403 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
405 case SNDRV_PCM_TRIGGER_SUSPEND:
406 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100407 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
409 ypcm->running = 0;
410 break;
411 default:
412 result = -EINVAL;
413 break;
414 }
415 __unlock:
416 spin_unlock(&chip->reg_lock);
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200417 if (kctl)
418 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return result;
420}
Takashi Iwai208a1b42005-11-17 14:53:41 +0100421static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 int cmd)
423{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100424 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
425 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int result = 0;
427 u32 tmp;
428
429 spin_lock(&chip->reg_lock);
430 switch (cmd) {
431 case SNDRV_PCM_TRIGGER_START:
432 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
433 case SNDRV_PCM_TRIGGER_RESUME:
434 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
435 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
436 ypcm->running = 1;
437 break;
438 case SNDRV_PCM_TRIGGER_STOP:
439 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
440 case SNDRV_PCM_TRIGGER_SUSPEND:
441 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
442 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
443 ypcm->running = 0;
444 break;
445 default:
446 result = -EINVAL;
447 break;
448 }
449 spin_unlock(&chip->reg_lock);
450 return result;
451}
452
Takashi Iwai208a1b42005-11-17 14:53:41 +0100453static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 int err;
456
457 if (ypcm->voices[1] != NULL && voices < 2) {
458 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
459 ypcm->voices[1] = NULL;
460 }
461 if (voices == 1 && ypcm->voices[0] != NULL)
462 return 0; /* already allocated */
463 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
464 return 0; /* already allocated */
465 if (voices > 1) {
466 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
467 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
468 ypcm->voices[0] = NULL;
469 }
470 }
471 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
472 if (err < 0)
473 return err;
474 ypcm->voices[0]->ypcm = ypcm;
475 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
476 if (voices > 1) {
477 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
478 ypcm->voices[1]->ypcm = ypcm;
479 }
480 return 0;
481}
482
Takashi Iwai208a1b42005-11-17 14:53:41 +0100483static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
484 struct snd_pcm_runtime *runtime,
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200485 int has_pcm_volume)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100487 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 u32 format;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200489 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
490 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
491 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
Takashi Iwai208a1b42005-11-17 14:53:41 +0100492 struct snd_ymfpci_playback_bank *bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 unsigned int nbank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200494 u32 vol_left, vol_right;
495 u8 use_left, use_right;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100496 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 snd_assert(voice != NULL, return);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200499 if (runtime->channels == 1) {
500 use_left = 1;
501 use_right = 1;
502 } else {
503 use_left = (voiceidx & 1) == 0;
504 use_right = !use_left;
505 }
506 if (has_pcm_volume) {
507 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
508 [ypcm->substream->number].left << 15);
509 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
510 [ypcm->substream->number].right << 15);
511 } else {
512 vol_left = cpu_to_le32(0x40000000);
513 vol_right = cpu_to_le32(0x40000000);
514 }
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100515 spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200516 format = runtime->channels == 2 ? 0x00010000 : 0;
517 if (snd_pcm_format_width(runtime->format) == 8)
518 format |= 0x80000000;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100519 else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
520 runtime->rate == 44100 && runtime->channels == 2 &&
521 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
522 ypcm->chip->src441_used == voice->number)) {
523 ypcm->chip->src441_used = voice->number;
524 ypcm->use_441_slot = 1;
525 format |= 0x10000000;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100526 }
527 if (ypcm->chip->src441_used == voice->number &&
528 (format & 0x10000000) == 0) {
529 ypcm->chip->src441_used = -1;
530 ypcm->use_441_slot = 0;
531 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200532 if (runtime->channels == 2 && (voiceidx & 1) != 0)
533 format |= 1;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100534 spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 for (nbank = 0; nbank < 2; nbank++) {
536 bank = &voice->bank[nbank];
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200537 memset(bank, 0, sizeof(*bank));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 bank->format = cpu_to_le32(format);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200539 bank->base = cpu_to_le32(runtime->dma_addr);
540 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 bank->lpfQ = cpu_to_le32(lpfQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 bank->delta =
543 bank->delta_end = cpu_to_le32(delta);
544 bank->lpfK =
545 bank->lpfK_end = cpu_to_le32(lpfK);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200546 bank->eg_gain =
547 bank->eg_gain_end = cpu_to_le32(0x40000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200549 if (ypcm->output_front) {
550 if (use_left) {
551 bank->left_gain =
552 bank->left_gain_end = vol_left;
553 }
554 if (use_right) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 bank->right_gain =
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200556 bank->right_gain_end = vol_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200558 }
559 if (ypcm->output_rear) {
Jaroslav Kysela5a25c5c2006-01-18 08:02:24 +0100560 if (!ypcm->swap_rear) {
561 if (use_left) {
562 bank->eff2_gain =
563 bank->eff2_gain_end = vol_left;
564 }
565 if (use_right) {
566 bank->eff3_gain =
567 bank->eff3_gain_end = vol_right;
568 }
569 } else {
570 /* The SPDIF out channels seem to be swapped, so we have
571 * to swap them here, too. The rear analog out channels
572 * will be wrong, but otherwise AC3 would not work.
573 */
574 if (use_left) {
575 bank->eff3_gain =
576 bank->eff3_gain_end = vol_left;
577 }
578 if (use_right) {
579 bank->eff2_gain =
580 bank->eff2_gain_end = vol_right;
581 }
582 }
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585}
586
Takashi Iwai208a1b42005-11-17 14:53:41 +0100587static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
590 4096, &chip->ac3_tmp_base) < 0)
591 return -ENOMEM;
592
593 chip->bank_effect[3][0]->base =
594 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
595 chip->bank_effect[3][0]->loop_end =
596 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
597 chip->bank_effect[4][0]->base =
598 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
599 chip->bank_effect[4][0]->loop_end =
600 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
601
602 spin_lock_irq(&chip->reg_lock);
603 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
604 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
605 spin_unlock_irq(&chip->reg_lock);
606 return 0;
607}
608
Takashi Iwai208a1b42005-11-17 14:53:41 +0100609static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 spin_lock_irq(&chip->reg_lock);
612 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
613 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
614 spin_unlock_irq(&chip->reg_lock);
615 // snd_ymfpci_irq_wait(chip);
616 if (chip->ac3_tmp_base.area) {
617 snd_dma_free_pages(&chip->ac3_tmp_base);
618 chip->ac3_tmp_base.area = NULL;
619 }
620 return 0;
621}
622
Takashi Iwai208a1b42005-11-17 14:53:41 +0100623static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
624 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100626 struct snd_pcm_runtime *runtime = substream->runtime;
627 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 int err;
629
630 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
631 return err;
632 if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
633 return err;
634 return 0;
635}
636
Takashi Iwai208a1b42005-11-17 14:53:41 +0100637static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100639 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
640 struct snd_pcm_runtime *runtime = substream->runtime;
641 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (runtime->private_data == NULL)
644 return 0;
645 ypcm = runtime->private_data;
646
647 /* wait, until the PCI operations are not finished */
648 snd_ymfpci_irq_wait(chip);
649 snd_pcm_lib_free_pages(substream);
650 if (ypcm->voices[1]) {
651 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
652 ypcm->voices[1] = NULL;
653 }
654 if (ypcm->voices[0]) {
655 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
656 ypcm->voices[0] = NULL;
657 }
658 return 0;
659}
660
Takashi Iwai208a1b42005-11-17 14:53:41 +0100661static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100663 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
664 struct snd_pcm_runtime *runtime = substream->runtime;
665 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200666 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 unsigned int nvoice;
668
669 ypcm->period_size = runtime->period_size;
670 ypcm->buffer_size = runtime->buffer_size;
671 ypcm->period_pos = 0;
672 ypcm->last_pos = 0;
673 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200674 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
675 substream->pcm == chip->pcm);
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200676
677 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
678 kctl = chip->pcm_mixer[substream->number].ctl;
679 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
680 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 0;
683}
684
Takashi Iwai208a1b42005-11-17 14:53:41 +0100685static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
686 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
689}
690
Takashi Iwai208a1b42005-11-17 14:53:41 +0100691static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100693 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* wait, until the PCI operations are not finished */
696 snd_ymfpci_irq_wait(chip);
697 return snd_pcm_lib_free_pages(substream);
698}
699
Takashi Iwai208a1b42005-11-17 14:53:41 +0100700static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100702 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
703 struct snd_pcm_runtime *runtime = substream->runtime;
704 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
705 struct snd_ymfpci_capture_bank * bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 int nbank;
707 u32 rate, format;
708
709 ypcm->period_size = runtime->period_size;
710 ypcm->buffer_size = runtime->buffer_size;
711 ypcm->period_pos = 0;
712 ypcm->last_pos = 0;
713 ypcm->shift = 0;
714 rate = ((48000 * 4096) / runtime->rate) - 1;
715 format = 0;
716 if (runtime->channels == 2) {
717 format |= 2;
718 ypcm->shift++;
719 }
720 if (snd_pcm_format_width(runtime->format) == 8)
721 format |= 1;
722 else
723 ypcm->shift++;
724 switch (ypcm->capture_bank_number) {
725 case 0:
726 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
727 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
728 break;
729 case 1:
730 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
731 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
732 break;
733 }
734 for (nbank = 0; nbank < 2; nbank++) {
735 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
736 bank->base = cpu_to_le32(runtime->dma_addr);
737 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
738 bank->start = 0;
739 bank->num_of_loops = 0;
740 }
741 return 0;
742}
743
Takashi Iwai208a1b42005-11-17 14:53:41 +0100744static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100746 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
747 struct snd_pcm_runtime *runtime = substream->runtime;
748 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
749 struct snd_ymfpci_voice *voice = ypcm->voices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (!(ypcm->running && voice))
752 return 0;
753 return le32_to_cpu(voice->bank[chip->active_bank].start);
754}
755
Takashi Iwai208a1b42005-11-17 14:53:41 +0100756static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100758 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
759 struct snd_pcm_runtime *runtime = substream->runtime;
760 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (!ypcm->running)
763 return 0;
764 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
765}
766
Takashi Iwai208a1b42005-11-17 14:53:41 +0100767static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 wait_queue_t wait;
770 int loops = 4;
771
772 while (loops-- > 0) {
773 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
774 continue;
775 init_waitqueue_entry(&wait, current);
776 add_wait_queue(&chip->interrupt_sleep, &wait);
777 atomic_inc(&chip->interrupt_sleep_count);
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200778 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 remove_wait_queue(&chip->interrupt_sleep, &wait);
780 }
781}
782
David Howells7d12e782006-10-05 14:55:46 +0100783static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100785 struct snd_ymfpci *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 u32 status, nvoice, mode;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100787 struct snd_ymfpci_voice *voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
790 if (status & 0x80000000) {
791 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
792 spin_lock(&chip->voice_lock);
793 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
794 voice = &chip->voices[nvoice];
795 if (voice->interrupt)
796 voice->interrupt(chip, voice);
797 }
798 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
799 if (chip->capture_substream[nvoice])
800 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
801 }
802#if 0
803 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
804 if (chip->effect_substream[nvoice])
805 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
806 }
807#endif
808 spin_unlock(&chip->voice_lock);
809 spin_lock(&chip->reg_lock);
810 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
811 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
812 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
813 spin_unlock(&chip->reg_lock);
814
815 if (atomic_read(&chip->interrupt_sleep_count)) {
816 atomic_set(&chip->interrupt_sleep_count, 0);
817 wake_up(&chip->interrupt_sleep);
818 }
819 }
820
821 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
822 if (status & 1) {
823 if (chip->timer)
824 snd_timer_interrupt(chip->timer, chip->timer->sticks);
825 }
826 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
827
828 if (chip->rawmidi)
David Howells7d12e782006-10-05 14:55:46 +0100829 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return IRQ_HANDLED;
831}
832
Takashi Iwai208a1b42005-11-17 14:53:41 +0100833static struct snd_pcm_hardware snd_ymfpci_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 .info = (SNDRV_PCM_INFO_MMAP |
836 SNDRV_PCM_INFO_MMAP_VALID |
837 SNDRV_PCM_INFO_INTERLEAVED |
838 SNDRV_PCM_INFO_BLOCK_TRANSFER |
839 SNDRV_PCM_INFO_PAUSE |
840 SNDRV_PCM_INFO_RESUME),
841 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
842 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
843 .rate_min = 8000,
844 .rate_max = 48000,
845 .channels_min = 1,
846 .channels_max = 2,
847 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
848 .period_bytes_min = 64,
849 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
850 .periods_min = 3,
851 .periods_max = 1024,
852 .fifo_size = 0,
853};
854
Takashi Iwai208a1b42005-11-17 14:53:41 +0100855static struct snd_pcm_hardware snd_ymfpci_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 .info = (SNDRV_PCM_INFO_MMAP |
858 SNDRV_PCM_INFO_MMAP_VALID |
859 SNDRV_PCM_INFO_INTERLEAVED |
860 SNDRV_PCM_INFO_BLOCK_TRANSFER |
861 SNDRV_PCM_INFO_PAUSE |
862 SNDRV_PCM_INFO_RESUME),
863 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
864 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
865 .rate_min = 8000,
866 .rate_max = 48000,
867 .channels_min = 1,
868 .channels_max = 2,
869 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
870 .period_bytes_min = 64,
871 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
872 .periods_min = 3,
873 .periods_max = 1024,
874 .fifo_size = 0,
875};
876
Takashi Iwai208a1b42005-11-17 14:53:41 +0100877static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Jesper Juhl4d572772005-05-30 17:30:32 +0200879 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Takashi Iwai208a1b42005-11-17 14:53:41 +0100882static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100884 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
885 struct snd_pcm_runtime *runtime = substream->runtime;
886 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200888 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (ypcm == NULL)
890 return -ENOMEM;
891 ypcm->chip = chip;
892 ypcm->type = PLAYBACK_VOICE;
893 ypcm->substream = substream;
894 runtime->hw = snd_ymfpci_playback;
895 runtime->private_data = ypcm;
896 runtime->private_free = snd_ymfpci_pcm_free_substream;
897 /* FIXME? True value is 256/48 = 5.33333 ms */
898 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
899 return 0;
900}
901
902/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100903static void ymfpci_open_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 if (! chip->rear_opened) {
906 if (! chip->spdif_opened) /* set AC3 */
907 snd_ymfpci_writel(chip, YDSXGR_MODE,
908 snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
909 /* enable second codec (4CHEN) */
910 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
911 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
912 }
913}
914
915/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100916static void ymfpci_close_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 if (! chip->rear_opened) {
919 if (! chip->spdif_opened)
920 snd_ymfpci_writel(chip, YDSXGR_MODE,
921 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
922 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
923 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
924 }
925}
926
Takashi Iwai208a1b42005-11-17 14:53:41 +0100927static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100929 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
930 struct snd_pcm_runtime *runtime = substream->runtime;
931 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int err;
933
934 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
935 return err;
936 ypcm = runtime->private_data;
937 ypcm->output_front = 1;
938 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
Glen Masgaid9301262006-10-10 09:27:19 +0200939 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 spin_lock_irq(&chip->reg_lock);
941 if (ypcm->output_rear) {
942 ymfpci_open_extension(chip);
943 chip->rear_opened++;
944 }
945 spin_unlock_irq(&chip->reg_lock);
946 return 0;
947}
948
Takashi Iwai208a1b42005-11-17 14:53:41 +0100949static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100951 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
952 struct snd_pcm_runtime *runtime = substream->runtime;
953 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 int err;
955
956 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
957 return err;
958 ypcm = runtime->private_data;
959 ypcm->output_front = 0;
960 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200961 ypcm->swap_rear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 spin_lock_irq(&chip->reg_lock);
963 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
964 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
965 ymfpci_open_extension(chip);
966 chip->spdif_pcm_bits = chip->spdif_bits;
967 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
968 chip->spdif_opened++;
969 spin_unlock_irq(&chip->reg_lock);
970
971 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
972 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
973 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
974 return 0;
975}
976
Takashi Iwai208a1b42005-11-17 14:53:41 +0100977static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100979 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
980 struct snd_pcm_runtime *runtime = substream->runtime;
981 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int err;
983
984 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
985 return err;
986 ypcm = runtime->private_data;
987 ypcm->output_front = 0;
988 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200989 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 spin_lock_irq(&chip->reg_lock);
991 ymfpci_open_extension(chip);
992 chip->rear_opened++;
993 spin_unlock_irq(&chip->reg_lock);
994 return 0;
995}
996
Takashi Iwai208a1b42005-11-17 14:53:41 +0100997static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 u32 capture_bank_number)
999{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001000 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1001 struct snd_pcm_runtime *runtime = substream->runtime;
1002 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001004 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (ypcm == NULL)
1006 return -ENOMEM;
1007 ypcm->chip = chip;
1008 ypcm->type = capture_bank_number + CAPTURE_REC;
1009 ypcm->substream = substream;
1010 ypcm->capture_bank_number = capture_bank_number;
1011 chip->capture_substream[capture_bank_number] = substream;
1012 runtime->hw = snd_ymfpci_capture;
1013 /* FIXME? True value is 256/48 = 5.33333 ms */
1014 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
1015 runtime->private_data = ypcm;
1016 runtime->private_free = snd_ymfpci_pcm_free_substream;
1017 snd_ymfpci_hw_start(chip);
1018 return 0;
1019}
1020
Takashi Iwai208a1b42005-11-17 14:53:41 +01001021static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 return snd_ymfpci_capture_open(substream, 0);
1024}
1025
Takashi Iwai208a1b42005-11-17 14:53:41 +01001026static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 return snd_ymfpci_capture_open(substream, 1);
1029}
1030
Takashi Iwai208a1b42005-11-17 14:53:41 +01001031static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 return 0;
1034}
1035
Takashi Iwai208a1b42005-11-17 14:53:41 +01001036static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001038 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1039 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 spin_lock_irq(&chip->reg_lock);
1042 if (ypcm->output_rear && chip->rear_opened > 0) {
1043 chip->rear_opened--;
1044 ymfpci_close_extension(chip);
1045 }
1046 spin_unlock_irq(&chip->reg_lock);
1047 return snd_ymfpci_playback_close_1(substream);
1048}
1049
Takashi Iwai208a1b42005-11-17 14:53:41 +01001050static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001052 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 spin_lock_irq(&chip->reg_lock);
1055 chip->spdif_opened = 0;
1056 ymfpci_close_extension(chip);
1057 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1058 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1059 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1060 spin_unlock_irq(&chip->reg_lock);
1061 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1062 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1063 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1064 return snd_ymfpci_playback_close_1(substream);
1065}
1066
Takashi Iwai208a1b42005-11-17 14:53:41 +01001067static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001069 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 spin_lock_irq(&chip->reg_lock);
1072 if (chip->rear_opened > 0) {
1073 chip->rear_opened--;
1074 ymfpci_close_extension(chip);
1075 }
1076 spin_unlock_irq(&chip->reg_lock);
1077 return snd_ymfpci_playback_close_1(substream);
1078}
1079
Takashi Iwai208a1b42005-11-17 14:53:41 +01001080static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001082 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1083 struct snd_pcm_runtime *runtime = substream->runtime;
1084 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 if (ypcm != NULL) {
1087 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1088 snd_ymfpci_hw_stop(chip);
1089 }
1090 return 0;
1091}
1092
Takashi Iwai208a1b42005-11-17 14:53:41 +01001093static struct snd_pcm_ops snd_ymfpci_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 .open = snd_ymfpci_playback_open,
1095 .close = snd_ymfpci_playback_close,
1096 .ioctl = snd_pcm_lib_ioctl,
1097 .hw_params = snd_ymfpci_playback_hw_params,
1098 .hw_free = snd_ymfpci_playback_hw_free,
1099 .prepare = snd_ymfpci_playback_prepare,
1100 .trigger = snd_ymfpci_playback_trigger,
1101 .pointer = snd_ymfpci_playback_pointer,
1102};
1103
Takashi Iwai208a1b42005-11-17 14:53:41 +01001104static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 .open = snd_ymfpci_capture_rec_open,
1106 .close = snd_ymfpci_capture_close,
1107 .ioctl = snd_pcm_lib_ioctl,
1108 .hw_params = snd_ymfpci_capture_hw_params,
1109 .hw_free = snd_ymfpci_capture_hw_free,
1110 .prepare = snd_ymfpci_capture_prepare,
1111 .trigger = snd_ymfpci_capture_trigger,
1112 .pointer = snd_ymfpci_capture_pointer,
1113};
1114
Takashi Iwai208a1b42005-11-17 14:53:41 +01001115int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001117 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int err;
1119
1120 if (rpcm)
1121 *rpcm = NULL;
1122 if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1123 return err;
1124 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1127 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1128
1129 /* global setup */
1130 pcm->info_flags = 0;
1131 strcpy(pcm->name, "YMFPCI");
1132 chip->pcm = pcm;
1133
1134 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1135 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1136
1137 if (rpcm)
1138 *rpcm = pcm;
1139 return 0;
1140}
1141
Takashi Iwai208a1b42005-11-17 14:53:41 +01001142static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 .open = snd_ymfpci_capture_ac97_open,
1144 .close = snd_ymfpci_capture_close,
1145 .ioctl = snd_pcm_lib_ioctl,
1146 .hw_params = snd_ymfpci_capture_hw_params,
1147 .hw_free = snd_ymfpci_capture_hw_free,
1148 .prepare = snd_ymfpci_capture_prepare,
1149 .trigger = snd_ymfpci_capture_trigger,
1150 .pointer = snd_ymfpci_capture_pointer,
1151};
1152
Takashi Iwai208a1b42005-11-17 14:53:41 +01001153int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001155 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int err;
1157
1158 if (rpcm)
1159 *rpcm = NULL;
1160 if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1161 return err;
1162 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1165
1166 /* global setup */
1167 pcm->info_flags = 0;
1168 sprintf(pcm->name, "YMFPCI - %s",
1169 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1170 chip->pcm2 = pcm;
1171
1172 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1173 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1174
1175 if (rpcm)
1176 *rpcm = pcm;
1177 return 0;
1178}
1179
Takashi Iwai208a1b42005-11-17 14:53:41 +01001180static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 .open = snd_ymfpci_playback_spdif_open,
1182 .close = snd_ymfpci_playback_spdif_close,
1183 .ioctl = snd_pcm_lib_ioctl,
1184 .hw_params = snd_ymfpci_playback_hw_params,
1185 .hw_free = snd_ymfpci_playback_hw_free,
1186 .prepare = snd_ymfpci_playback_prepare,
1187 .trigger = snd_ymfpci_playback_trigger,
1188 .pointer = snd_ymfpci_playback_pointer,
1189};
1190
Takashi Iwai208a1b42005-11-17 14:53:41 +01001191int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001193 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int err;
1195
1196 if (rpcm)
1197 *rpcm = NULL;
1198 if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1199 return err;
1200 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1203
1204 /* global setup */
1205 pcm->info_flags = 0;
1206 strcpy(pcm->name, "YMFPCI - IEC958");
1207 chip->pcm_spdif = pcm;
1208
1209 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1210 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1211
1212 if (rpcm)
1213 *rpcm = pcm;
1214 return 0;
1215}
1216
Takashi Iwai208a1b42005-11-17 14:53:41 +01001217static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 .open = snd_ymfpci_playback_4ch_open,
1219 .close = snd_ymfpci_playback_4ch_close,
1220 .ioctl = snd_pcm_lib_ioctl,
1221 .hw_params = snd_ymfpci_playback_hw_params,
1222 .hw_free = snd_ymfpci_playback_hw_free,
1223 .prepare = snd_ymfpci_playback_prepare,
1224 .trigger = snd_ymfpci_playback_trigger,
1225 .pointer = snd_ymfpci_playback_pointer,
1226};
1227
Takashi Iwai208a1b42005-11-17 14:53:41 +01001228int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001230 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int err;
1232
1233 if (rpcm)
1234 *rpcm = NULL;
1235 if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1236 return err;
1237 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1240
1241 /* global setup */
1242 pcm->info_flags = 0;
1243 strcpy(pcm->name, "YMFPCI - Rear PCM");
1244 chip->pcm_4ch = pcm;
1245
1246 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1247 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1248
1249 if (rpcm)
1250 *rpcm = pcm;
1251 return 0;
1252}
1253
Takashi Iwai208a1b42005-11-17 14:53:41 +01001254static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1257 uinfo->count = 1;
1258 return 0;
1259}
1260
Takashi Iwai208a1b42005-11-17 14:53:41 +01001261static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1262 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001264 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 spin_lock_irq(&chip->reg_lock);
1267 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1268 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001269 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 spin_unlock_irq(&chip->reg_lock);
1271 return 0;
1272}
1273
Takashi Iwai208a1b42005-11-17 14:53:41 +01001274static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1275 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001277 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 unsigned int val;
1279 int change;
1280
1281 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1282 (ucontrol->value.iec958.status[1] << 8);
1283 spin_lock_irq(&chip->reg_lock);
1284 change = chip->spdif_bits != val;
1285 chip->spdif_bits = val;
1286 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1287 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1288 spin_unlock_irq(&chip->reg_lock);
1289 return change;
1290}
1291
Takashi Iwai208a1b42005-11-17 14:53:41 +01001292static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1295 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1296 .info = snd_ymfpci_spdif_default_info,
1297 .get = snd_ymfpci_spdif_default_get,
1298 .put = snd_ymfpci_spdif_default_put
1299};
1300
Takashi Iwai208a1b42005-11-17 14:53:41 +01001301static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1304 uinfo->count = 1;
1305 return 0;
1306}
1307
Takashi Iwai208a1b42005-11-17 14:53:41 +01001308static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1309 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001311 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 spin_lock_irq(&chip->reg_lock);
1314 ucontrol->value.iec958.status[0] = 0x3e;
1315 ucontrol->value.iec958.status[1] = 0xff;
1316 spin_unlock_irq(&chip->reg_lock);
1317 return 0;
1318}
1319
Takashi Iwai208a1b42005-11-17 14:53:41 +01001320static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1323 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1324 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1325 .info = snd_ymfpci_spdif_mask_info,
1326 .get = snd_ymfpci_spdif_mask_get,
1327};
1328
Takashi Iwai208a1b42005-11-17 14:53:41 +01001329static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1332 uinfo->count = 1;
1333 return 0;
1334}
1335
Takashi Iwai208a1b42005-11-17 14:53:41 +01001336static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1337 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001339 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 spin_lock_irq(&chip->reg_lock);
1342 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1343 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001344 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 spin_unlock_irq(&chip->reg_lock);
1346 return 0;
1347}
1348
Takashi Iwai208a1b42005-11-17 14:53:41 +01001349static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1350 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001352 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 unsigned int val;
1354 int change;
1355
1356 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1357 (ucontrol->value.iec958.status[1] << 8);
1358 spin_lock_irq(&chip->reg_lock);
1359 change = chip->spdif_pcm_bits != val;
1360 chip->spdif_pcm_bits = val;
1361 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1362 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1363 spin_unlock_irq(&chip->reg_lock);
1364 return change;
1365}
1366
Takashi Iwai208a1b42005-11-17 14:53:41 +01001367static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1370 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1371 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1372 .info = snd_ymfpci_spdif_stream_info,
1373 .get = snd_ymfpci_spdif_stream_get,
1374 .put = snd_ymfpci_spdif_stream_put
1375};
1376
Takashi Iwai208a1b42005-11-17 14:53:41 +01001377static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};
1380
1381 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1382 info->count = 1;
1383 info->value.enumerated.items = 3;
1384 if (info->value.enumerated.item > 2)
1385 info->value.enumerated.item = 2;
1386 strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
1387 return 0;
1388}
1389
Takashi Iwai208a1b42005-11-17 14:53:41 +01001390static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001392 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 u16 reg;
1394
1395 spin_lock_irq(&chip->reg_lock);
1396 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1397 spin_unlock_irq(&chip->reg_lock);
1398 if (!(reg & 0x100))
1399 value->value.enumerated.item[0] = 0;
1400 else
1401 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1402 return 0;
1403}
1404
Takashi Iwai208a1b42005-11-17 14:53:41 +01001405static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001407 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 u16 reg, old_reg;
1409
1410 spin_lock_irq(&chip->reg_lock);
1411 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1412 if (value->value.enumerated.item[0] == 0)
1413 reg = old_reg & ~0x100;
1414 else
1415 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1416 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1417 spin_unlock_irq(&chip->reg_lock);
1418 return reg != old_reg;
1419}
1420
Takashi Iwai208a1b42005-11-17 14:53:41 +01001421static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1423 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1424 .name = "Direct Recording Source",
1425 .info = snd_ymfpci_drec_source_info,
1426 .get = snd_ymfpci_drec_source_get,
1427 .put = snd_ymfpci_drec_source_put
1428};
1429
1430/*
1431 * Mixer controls
1432 */
1433
Glen Masgaid602c882005-09-28 08:19:05 +02001434#define YMFPCI_SINGLE(xname, xindex, reg, shift) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1436 .info = snd_ymfpci_info_single, \
1437 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
Glen Masgaid602c882005-09-28 08:19:05 +02001438 .private_value = ((reg) | ((shift) << 16)) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001440#define snd_ymfpci_info_single snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Takashi Iwai208a1b42005-11-17 14:53:41 +01001442static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1443 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001445 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001446 int reg = kcontrol->private_value & 0xffff;
1447 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1448 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Glen Masgaid602c882005-09-28 08:19:05 +02001450 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 case YDSXGR_SPDIFOUTCTRL: break;
1452 case YDSXGR_SPDIFINCTRL: break;
1453 default: return -EINVAL;
1454 }
Glen Masgaid602c882005-09-28 08:19:05 +02001455 ucontrol->value.integer.value[0] =
1456 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return 0;
1458}
1459
Takashi Iwai208a1b42005-11-17 14:53:41 +01001460static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1461 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001463 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001464 int reg = kcontrol->private_value & 0xffff;
1465 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1466 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 int change;
1468 unsigned int val, oval;
1469
Glen Masgaid602c882005-09-28 08:19:05 +02001470 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 case YDSXGR_SPDIFOUTCTRL: break;
1472 case YDSXGR_SPDIFINCTRL: break;
1473 default: return -EINVAL;
1474 }
1475 val = (ucontrol->value.integer.value[0] & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 val <<= shift;
1477 spin_lock_irq(&chip->reg_lock);
1478 oval = snd_ymfpci_readl(chip, reg);
1479 val = (oval & ~(mask << shift)) | val;
1480 change = val != oval;
1481 snd_ymfpci_writel(chip, reg, val);
1482 spin_unlock_irq(&chip->reg_lock);
1483 return change;
1484}
1485
Takashi Iwai0cb29ea2007-01-29 15:33:49 +01001486static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
Takashi Iwai33925182006-08-28 13:29:42 +02001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488#define YMFPCI_DOUBLE(xname, xindex, reg) \
1489{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Takashi Iwai33925182006-08-28 13:29:42 +02001490 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 .info = snd_ymfpci_info_double, \
1492 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
Takashi Iwai33925182006-08-28 13:29:42 +02001493 .private_value = reg, \
1494 .tlv = { .p = db_scale_native } }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Takashi Iwai208a1b42005-11-17 14:53:41 +01001496static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 unsigned int reg = kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 if (reg < 0x80 || reg >= 0xc0)
1501 return -EINVAL;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001502 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 uinfo->count = 2;
1504 uinfo->value.integer.min = 0;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001505 uinfo->value.integer.max = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return 0;
1507}
1508
Takashi Iwai208a1b42005-11-17 14:53:41 +01001509static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001511 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001513 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 unsigned int val;
1515
1516 if (reg < 0x80 || reg >= 0xc0)
1517 return -EINVAL;
1518 spin_lock_irq(&chip->reg_lock);
1519 val = snd_ymfpci_readl(chip, reg);
1520 spin_unlock_irq(&chip->reg_lock);
1521 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1522 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return 0;
1524}
1525
Takashi Iwai208a1b42005-11-17 14:53:41 +01001526static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001528 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001530 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 int change;
1532 unsigned int val1, val2, oval;
1533
1534 if (reg < 0x80 || reg >= 0xc0)
1535 return -EINVAL;
1536 val1 = ucontrol->value.integer.value[0] & mask;
1537 val2 = ucontrol->value.integer.value[1] & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 val1 <<= shift_left;
1539 val2 <<= shift_right;
1540 spin_lock_irq(&chip->reg_lock);
1541 oval = snd_ymfpci_readl(chip, reg);
1542 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1543 change = val1 != oval;
1544 snd_ymfpci_writel(chip, reg, val1);
1545 spin_unlock_irq(&chip->reg_lock);
1546 return change;
1547}
1548
Clemens Ladisch177a7cd2007-07-23 17:38:44 +02001549static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1550 struct snd_ctl_elem_value *ucontrol)
1551{
1552 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1553 unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1554 unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1555 int change;
1556 unsigned int value, oval;
1557
1558 value = ucontrol->value.integer.value[0] & 0x3fff;
1559 value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1560 spin_lock_irq(&chip->reg_lock);
1561 oval = snd_ymfpci_readl(chip, reg);
1562 change = value != oval;
1563 snd_ymfpci_writel(chip, reg, value);
1564 snd_ymfpci_writel(chip, reg2, value);
1565 spin_unlock_irq(&chip->reg_lock);
1566 return change;
1567}
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569/*
1570 * 4ch duplication
1571 */
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001572#define snd_ymfpci_info_dup4ch snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Takashi Iwai208a1b42005-11-17 14:53:41 +01001574static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001576 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1578 return 0;
1579}
1580
Takashi Iwai208a1b42005-11-17 14:53:41 +01001581static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001583 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int change;
1585 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1586 if (change)
1587 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1588 return change;
1589}
1590
1591
Takashi Iwai208a1b42005-11-17 14:53:41 +01001592static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
Clemens Ladisch177a7cd2007-07-23 17:38:44 +02001593{
1594 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1595 .name = "Wave Playback Volume",
1596 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1597 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1598 .info = snd_ymfpci_info_double,
1599 .get = snd_ymfpci_get_double,
1600 .put = snd_ymfpci_put_nativedacvol,
1601 .private_value = YDSXGR_NATIVEDACOUTVOL,
1602 .tlv = { .p = db_scale_native },
1603},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1605YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1606YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1607YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1608YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1609YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1610YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1611YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
1612YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1613YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1614YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1615YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
Glen Masgaid602c882005-09-28 08:19:05 +02001616YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1617YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1618YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1621 .name = "4ch Duplication",
1622 .info = snd_ymfpci_info_dup4ch,
1623 .get = snd_ymfpci_get_dup4ch,
1624 .put = snd_ymfpci_put_dup4ch,
1625},
1626};
1627
1628
1629/*
1630 * GPIO
1631 */
1632
Takashi Iwai208a1b42005-11-17 14:53:41 +01001633static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 u16 reg, mode;
1636 unsigned long flags;
1637
1638 spin_lock_irqsave(&chip->reg_lock, flags);
1639 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1640 reg &= ~(1 << (pin + 8));
1641 reg |= (1 << pin);
1642 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1643 /* set the level mode for input line */
1644 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1645 mode &= ~(3 << (pin * 2));
1646 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1647 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1648 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1649 spin_unlock_irqrestore(&chip->reg_lock, flags);
1650 return (mode >> pin) & 1;
1651}
1652
Takashi Iwai208a1b42005-11-17 14:53:41 +01001653static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655 u16 reg;
1656 unsigned long flags;
1657
1658 spin_lock_irqsave(&chip->reg_lock, flags);
1659 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1660 reg &= ~(1 << pin);
1661 reg &= ~(1 << (pin + 8));
1662 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1663 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1664 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1665 spin_unlock_irqrestore(&chip->reg_lock, flags);
1666
1667 return 0;
1668}
1669
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001670#define snd_ymfpci_gpio_sw_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Takashi Iwai208a1b42005-11-17 14:53:41 +01001672static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001674 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 int pin = (int)kcontrol->private_value;
1676 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1677 return 0;
1678}
1679
Takashi Iwai208a1b42005-11-17 14:53:41 +01001680static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001682 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 int pin = (int)kcontrol->private_value;
1684
1685 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1686 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1687 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1688 return 1;
1689 }
1690 return 0;
1691}
1692
Takashi Iwai208a1b42005-11-17 14:53:41 +01001693static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 .name = "Shared Rear/Line-In Switch",
1695 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1696 .info = snd_ymfpci_gpio_sw_info,
1697 .get = snd_ymfpci_gpio_sw_get,
1698 .put = snd_ymfpci_gpio_sw_put,
1699 .private_value = 2,
1700};
1701
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001702/*
1703 * PCM voice volume
1704 */
1705
Takashi Iwai208a1b42005-11-17 14:53:41 +01001706static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1707 struct snd_ctl_elem_info *uinfo)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001708{
1709 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1710 uinfo->count = 2;
1711 uinfo->value.integer.min = 0;
1712 uinfo->value.integer.max = 0x8000;
1713 return 0;
1714}
1715
Takashi Iwai208a1b42005-11-17 14:53:41 +01001716static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1717 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001718{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001719 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001720 unsigned int subs = kcontrol->id.subdevice;
1721
1722 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1723 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1724 return 0;
1725}
1726
Takashi Iwai208a1b42005-11-17 14:53:41 +01001727static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1728 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001729{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001730 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001731 unsigned int subs = kcontrol->id.subdevice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001732 struct snd_pcm_substream *substream;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001733 unsigned long flags;
1734
1735 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1736 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1737 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1738 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1739
Takashi Iwai208a1b42005-11-17 14:53:41 +01001740 substream = (struct snd_pcm_substream *)kcontrol->private_value;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001741 spin_lock_irqsave(&chip->voice_lock, flags);
1742 if (substream->runtime && substream->runtime->private_data) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01001743 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01001744 if (!ypcm->use_441_slot)
1745 ypcm->update_pcm_vol = 2;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001746 }
1747 spin_unlock_irqrestore(&chip->voice_lock, flags);
1748 return 1;
1749 }
1750 return 0;
1751}
1752
Takashi Iwai208a1b42005-11-17 14:53:41 +01001753static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001754 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1755 .name = "PCM Playback Volume",
1756 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1757 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1758 .info = snd_ymfpci_pcm_vol_info,
1759 .get = snd_ymfpci_pcm_vol_get,
1760 .put = snd_ymfpci_pcm_vol_put,
1761};
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764/*
1765 * Mixer routines
1766 */
1767
Takashi Iwai208a1b42005-11-17 14:53:41 +01001768static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001770 struct snd_ymfpci *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 chip->ac97_bus = NULL;
1772}
1773
Takashi Iwai208a1b42005-11-17 14:53:41 +01001774static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001776 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 chip->ac97 = NULL;
1778}
1779
Glen Masgaid9301262006-10-10 09:27:19 +02001780int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001782 struct snd_ac97_template ac97;
1783 struct snd_kcontrol *kctl;
1784 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 unsigned int idx;
1786 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001787 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 .write = snd_ymfpci_codec_write,
1789 .read = snd_ymfpci_codec_read,
1790 };
1791
1792 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1793 return err;
1794 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1795 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1796
1797 memset(&ac97, 0, sizeof(ac97));
1798 ac97.private_data = chip;
1799 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1800 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1801 return err;
1802
1803 /* to be sure */
1804 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1805 AC97_EA_VRA|AC97_EA_VRM, 0);
1806
1807 for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1808 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1809 return err;
1810 }
1811
1812 /* add S/PDIF control */
1813 snd_assert(chip->pcm_spdif != NULL, return -EIO);
1814 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1815 return err;
1816 kctl->id.device = chip->pcm_spdif->device;
1817 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1818 return err;
1819 kctl->id.device = chip->pcm_spdif->device;
1820 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1821 return err;
1822 kctl->id.device = chip->pcm_spdif->device;
1823 chip->spdif_pcm_ctl = kctl;
1824
1825 /* direct recording source */
1826 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1827 (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1828 return err;
1829
1830 /*
1831 * shared rear/line-in
1832 */
1833 if (rear_switch) {
1834 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1835 return err;
1836 }
1837
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001838 /* per-voice volume */
1839 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1840 for (idx = 0; idx < 32; ++idx) {
1841 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1842 if (!kctl)
1843 return -ENOMEM;
1844 kctl->id.device = chip->pcm->device;
1845 kctl->id.subdevice = idx;
1846 kctl->private_value = (unsigned long)substream;
1847 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1848 return err;
1849 chip->pcm_mixer[idx].left = 0x8000;
1850 chip->pcm_mixer[idx].right = 0x8000;
1851 chip->pcm_mixer[idx].ctl = kctl;
1852 substream = substream->next;
1853 }
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return 0;
1856}
1857
1858
1859/*
1860 * timer
1861 */
1862
Takashi Iwai208a1b42005-11-17 14:53:41 +01001863static int snd_ymfpci_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001865 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 unsigned long flags;
1867 unsigned int count;
1868
1869 chip = snd_timer_chip(timer);
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001870 count = (timer->sticks << 1) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 spin_lock_irqsave(&chip->reg_lock, flags);
1872 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1873 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1874 spin_unlock_irqrestore(&chip->reg_lock, flags);
1875 return 0;
1876}
1877
Takashi Iwai208a1b42005-11-17 14:53:41 +01001878static int snd_ymfpci_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001880 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 unsigned long flags;
1882
1883 chip = snd_timer_chip(timer);
1884 spin_lock_irqsave(&chip->reg_lock, flags);
1885 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1886 spin_unlock_irqrestore(&chip->reg_lock, flags);
1887 return 0;
1888}
1889
Takashi Iwai208a1b42005-11-17 14:53:41 +01001890static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 unsigned long *num, unsigned long *den)
1892{
1893 *num = 1;
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001894 *den = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return 0;
1896}
1897
Takashi Iwai208a1b42005-11-17 14:53:41 +01001898static struct snd_timer_hardware snd_ymfpci_timer_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 .flags = SNDRV_TIMER_HW_AUTO,
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001900 .resolution = 20833, /* 1/fs = 20.8333...us */
1901 .ticks = 0x8000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 .start = snd_ymfpci_timer_start,
1903 .stop = snd_ymfpci_timer_stop,
1904 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1905};
1906
Takashi Iwai208a1b42005-11-17 14:53:41 +01001907int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001909 struct snd_timer *timer = NULL;
1910 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 int err;
1912
1913 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1914 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1915 tid.card = chip->card->number;
1916 tid.device = device;
1917 tid.subdevice = 0;
1918 if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1919 strcpy(timer->name, "YMFPCI timer");
1920 timer->private_data = chip;
1921 timer->hw = snd_ymfpci_timer_hw;
1922 }
1923 chip->timer = timer;
1924 return err;
1925}
1926
1927
1928/*
1929 * proc interface
1930 */
1931
Takashi Iwai208a1b42005-11-17 14:53:41 +01001932static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1933 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001935 struct snd_ymfpci *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 int i;
1937
1938 snd_iprintf(buffer, "YMFPCI\n\n");
1939 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1940 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1941}
1942
Takashi Iwai208a1b42005-11-17 14:53:41 +01001943static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001945 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 if (! snd_card_proc_new(card, "ymfpci", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001948 snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return 0;
1950}
1951
1952/*
1953 * initialization routines
1954 */
1955
1956static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1957{
1958 u8 cmd;
1959
1960 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1961#if 0 // force to reset
1962 if (cmd & 0x03) {
1963#endif
1964 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1965 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1966 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1967 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1968 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1969#if 0
1970 }
1971#endif
1972}
1973
Takashi Iwai208a1b42005-11-17 14:53:41 +01001974static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
1976 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1977}
1978
Takashi Iwai208a1b42005-11-17 14:53:41 +01001979static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 u32 val;
1982 int timeout = 1000;
1983
1984 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1985 if (val)
1986 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
1987 while (timeout-- > 0) {
1988 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
1989 if ((val & 0x00000002) == 0)
1990 break;
1991 }
1992}
1993
Takashi Iwai8ad2da12007-02-26 15:55:43 +01001994#ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
Clemens Ladisch102fa902006-10-11 12:05:59 +02001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996#include "ymfpci_image.h"
1997
Clemens Ladisch102fa902006-10-11 12:05:59 +02001998static struct firmware snd_ymfpci_dsp_microcode = {
1999 .size = YDSXG_DSPLENGTH,
2000 .data = (u8 *)DspInst,
2001};
2002static struct firmware snd_ymfpci_controller_microcode = {
2003 .size = YDSXG_CTRLLENGTH,
2004 .data = (u8 *)CntrlInst,
2005};
2006static struct firmware snd_ymfpci_controller_1e_microcode = {
2007 .size = YDSXG_CTRLLENGTH,
2008 .data = (u8 *)CntrlInst1E,
2009};
2010#endif
2011
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002012#ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
2013static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2014{
2015 chip->dsp_microcode = &snd_ymfpci_dsp_microcode;
2016 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2017 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2018 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2019 chip->device_id == PCI_DEVICE_ID_YAMAHA_754)
2020 chip->controller_microcode =
2021 &snd_ymfpci_controller_1e_microcode;
2022 else
2023 chip->controller_microcode =
2024 &snd_ymfpci_controller_microcode;
2025 return 0;
2026}
2027
2028#else /* use fw_loader */
2029
Clemens Ladisch102fa902006-10-11 12:05:59 +02002030#ifdef __LITTLE_ENDIAN
2031static inline void snd_ymfpci_convert_from_le(const struct firmware *fw) { }
2032#else
2033static void snd_ymfpci_convert_from_le(const struct firmware *fw)
2034{
2035 int i;
2036 u32 *data = (u32 *)fw->data;
2037
2038 for (i = 0; i < fw->size / 4; ++i)
2039 le32_to_cpus(&data[i]);
2040}
2041#endif
2042
2043static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2044{
2045 int err, is_1e;
2046 const char *name;
2047
2048 err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2049 &chip->pci->dev);
2050 if (err >= 0) {
2051 if (chip->dsp_microcode->size == YDSXG_DSPLENGTH)
2052 snd_ymfpci_convert_from_le(chip->dsp_microcode);
2053 else {
2054 snd_printk(KERN_ERR "DSP microcode has wrong size\n");
2055 err = -EINVAL;
2056 }
2057 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002058 if (err < 0)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002059 return err;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002060 is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2061 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2062 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2063 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2064 name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2065 err = request_firmware(&chip->controller_microcode, name,
2066 &chip->pci->dev);
2067 if (err >= 0) {
2068 if (chip->controller_microcode->size == YDSXG_CTRLLENGTH)
2069 snd_ymfpci_convert_from_le(chip->controller_microcode);
2070 else {
2071 snd_printk(KERN_ERR "controller microcode"
2072 " has wrong size\n");
2073 err = -EINVAL;
2074 }
2075 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002076 if (err < 0)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002077 return err;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002078 return 0;
2079}
Clemens Ladisch7e0af292007-05-03 17:59:54 +02002080
2081MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
2082MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
2083MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
2084
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002085#endif
Clemens Ladisch102fa902006-10-11 12:05:59 +02002086
Takashi Iwai208a1b42005-11-17 14:53:41 +01002087static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 int i;
2090 u16 ctrl;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002091 u32 *inst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2094 snd_ymfpci_disable_dsp(chip);
2095 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2096 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2097 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2098 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2099 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2100 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2101 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2102 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2103 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2104
2105 /* setup DSP instruction code */
Clemens Ladisch102fa902006-10-11 12:05:59 +02002106 inst = (u32 *)chip->dsp_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002108 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), inst[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 /* setup control instruction code */
Clemens Ladisch102fa902006-10-11 12:05:59 +02002111 inst = (u32 *)chip->controller_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2113 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), inst[i]);
2114
2115 snd_ymfpci_enable_dsp(chip);
2116}
2117
Takashi Iwai208a1b42005-11-17 14:53:41 +01002118static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
2120 long size, playback_ctrl_size;
2121 int voice, bank, reg;
2122 u8 *ptr;
2123 dma_addr_t ptr_addr;
2124
2125 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2126 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2127 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2128 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2129 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2130
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002131 size = ALIGN(playback_ctrl_size, 0x100) +
2132 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2133 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2134 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 chip->work_size;
2136 /* work_ptr must be aligned to 256 bytes, but it's already
2137 covered with the kernel page allocation mechanism */
2138 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2139 size, &chip->work_ptr) < 0)
2140 return -ENOMEM;
2141 ptr = chip->work_ptr.area;
2142 ptr_addr = chip->work_ptr.addr;
2143 memset(ptr, 0, size); /* for sure */
2144
2145 chip->bank_base_playback = ptr;
2146 chip->bank_base_playback_addr = ptr_addr;
2147 chip->ctrl_playback = (u32 *)ptr;
2148 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002149 ptr += ALIGN(playback_ctrl_size, 0x100);
2150 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2152 chip->voices[voice].number = voice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002153 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 chip->voices[voice].bank_addr = ptr_addr;
2155 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002156 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 ptr += chip->bank_size_playback;
2158 ptr_addr += chip->bank_size_playback;
2159 }
2160 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002161 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2162 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 chip->bank_base_capture = ptr;
2164 chip->bank_base_capture_addr = ptr_addr;
2165 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2166 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002167 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 ptr += chip->bank_size_capture;
2169 ptr_addr += chip->bank_size_capture;
2170 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002171 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2172 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 chip->bank_base_effect = ptr;
2174 chip->bank_base_effect_addr = ptr_addr;
2175 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2176 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002177 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 ptr += chip->bank_size_effect;
2179 ptr_addr += chip->bank_size_effect;
2180 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002181 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2182 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 chip->work_base = ptr;
2184 chip->work_base_addr = ptr_addr;
2185
2186 snd_assert(ptr + chip->work_size == chip->work_ptr.area + chip->work_ptr.bytes, );
2187
2188 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2189 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2190 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2191 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2192 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2193
2194 /* S/PDIF output initialization */
2195 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2196 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2197 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2198
2199 /* S/PDIF input initialization */
2200 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2201
2202 /* digital mixer setup */
2203 for (reg = 0x80; reg < 0xc0; reg += 4)
2204 snd_ymfpci_writel(chip, reg, 0);
2205 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2206 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2207 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2208 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2209 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2210 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2211 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2212
2213 return 0;
2214}
2215
Takashi Iwai208a1b42005-11-17 14:53:41 +01002216static int snd_ymfpci_free(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 u16 ctrl;
2219
2220 snd_assert(chip != NULL, return -EINVAL);
2221
2222 if (chip->res_reg_area) { /* don't touch busy hardware */
2223 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2224 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2225 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2226 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2227 snd_ymfpci_disable_dsp(chip);
2228 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2229 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2230 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2231 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2232 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2233 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2234 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2235 }
2236
2237 snd_ymfpci_ac3_done(chip);
2238
2239 /* Set PCI device to D3 state */
2240#if 0
2241 /* FIXME: temporarily disabled, otherwise we cannot fire up
2242 * the chip again unless reboot. ACPI bug?
2243 */
2244 pci_set_power_state(chip->pci, 3);
2245#endif
2246
2247#ifdef CONFIG_PM
2248 vfree(chip->saved_regs);
2249#endif
Takashi Iwaib1d57762005-10-10 11:56:31 +02002250 release_and_free_resource(chip->mpu_res);
2251 release_and_free_resource(chip->fm_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 snd_ymfpci_free_gameport(chip);
2253 if (chip->reg_area_virt)
2254 iounmap(chip->reg_area_virt);
2255 if (chip->work_ptr.area)
2256 snd_dma_free_pages(&chip->work_ptr);
2257
2258 if (chip->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01002259 free_irq(chip->irq, chip);
Takashi Iwaib1d57762005-10-10 11:56:31 +02002260 release_and_free_resource(chip->res_reg_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2263
2264 pci_disable_device(chip->pci);
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002265#ifndef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
2266 release_firmware(chip->dsp_microcode);
2267 release_firmware(chip->controller_microcode);
Clemens Ladisch102fa902006-10-11 12:05:59 +02002268#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 kfree(chip);
2270 return 0;
2271}
2272
Takashi Iwai208a1b42005-11-17 14:53:41 +01002273static int snd_ymfpci_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002275 struct snd_ymfpci *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return snd_ymfpci_free(chip);
2277}
2278
2279#ifdef CONFIG_PM
2280static int saved_regs_index[] = {
2281 /* spdif */
2282 YDSXGR_SPDIFOUTCTRL,
2283 YDSXGR_SPDIFOUTSTATUS,
2284 YDSXGR_SPDIFINCTRL,
2285 /* volumes */
2286 YDSXGR_PRIADCLOOPVOL,
2287 YDSXGR_NATIVEDACINVOL,
2288 YDSXGR_NATIVEDACOUTVOL,
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01002289 YDSXGR_BUF441OUTVOL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 YDSXGR_NATIVEADCINVOL,
2291 YDSXGR_SPDIFLOOPVOL,
2292 YDSXGR_SPDIFOUTVOL,
2293 YDSXGR_ZVOUTVOL,
2294 YDSXGR_LEGACYOUTVOL,
2295 /* address bases */
2296 YDSXGR_PLAYCTRLBASE,
2297 YDSXGR_RECCTRLBASE,
2298 YDSXGR_EFFCTRLBASE,
2299 YDSXGR_WORKBASE,
2300 /* capture set up */
2301 YDSXGR_MAPOFREC,
2302 YDSXGR_RECFORMAT,
2303 YDSXGR_RECSLOTSR,
2304 YDSXGR_ADCFORMAT,
2305 YDSXGR_ADCSLOTSR,
2306};
2307#define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
2308
Takashi Iwaided46232005-11-17 16:09:43 +01002309int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
Takashi Iwaided46232005-11-17 16:09:43 +01002311 struct snd_card *card = pci_get_drvdata(pci);
2312 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 unsigned int i;
2314
Takashi Iwaided46232005-11-17 16:09:43 +01002315 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 snd_pcm_suspend_all(chip->pcm);
2317 snd_pcm_suspend_all(chip->pcm2);
2318 snd_pcm_suspend_all(chip->pcm_spdif);
2319 snd_pcm_suspend_all(chip->pcm_4ch);
2320 snd_ac97_suspend(chip->ac97);
2321 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2322 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2323 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2324 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2325 snd_ymfpci_disable_dsp(chip);
Takashi Iwaided46232005-11-17 16:09:43 +01002326 pci_disable_device(pci);
2327 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002328 pci_set_power_state(pci, pci_choose_state(pci, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return 0;
2330}
2331
Takashi Iwaided46232005-11-17 16:09:43 +01002332int snd_ymfpci_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
Takashi Iwaided46232005-11-17 16:09:43 +01002334 struct snd_card *card = pci_get_drvdata(pci);
2335 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 unsigned int i;
2337
Takashi Iwai30b35392006-10-11 18:52:53 +02002338 pci_set_power_state(pci, PCI_D0);
Takashi Iwaided46232005-11-17 16:09:43 +01002339 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002340 if (pci_enable_device(pci) < 0) {
2341 printk(KERN_ERR "ymfpci: pci_enable_device failed, "
2342 "disabling device\n");
2343 snd_card_disconnect(card);
2344 return -EIO;
2345 }
Takashi Iwaided46232005-11-17 16:09:43 +01002346 pci_set_master(pci);
2347 snd_ymfpci_aclink_reset(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 snd_ymfpci_codec_ready(chip, 0);
2349 snd_ymfpci_download_image(chip);
2350 udelay(100);
2351
2352 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2353 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2354
2355 snd_ac97_resume(chip->ac97);
2356
2357 /* start hw again */
2358 if (chip->start_count > 0) {
2359 spin_lock_irq(&chip->reg_lock);
2360 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2361 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2362 spin_unlock_irq(&chip->reg_lock);
2363 }
Takashi Iwaided46232005-11-17 16:09:43 +01002364 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 return 0;
2366}
2367#endif /* CONFIG_PM */
2368
Takashi Iwai208a1b42005-11-17 14:53:41 +01002369int __devinit snd_ymfpci_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 struct pci_dev * pci,
2371 unsigned short old_legacy_ctrl,
Takashi Iwai208a1b42005-11-17 14:53:41 +01002372 struct snd_ymfpci ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002374 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002376 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 .dev_free = snd_ymfpci_dev_free,
2378 };
2379
2380 *rchip = NULL;
2381
2382 /* enable PCI device */
2383 if ((err = pci_enable_device(pci)) < 0)
2384 return err;
2385
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002386 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (chip == NULL) {
2388 pci_disable_device(pci);
2389 return -ENOMEM;
2390 }
2391 chip->old_legacy_ctrl = old_legacy_ctrl;
2392 spin_lock_init(&chip->reg_lock);
2393 spin_lock_init(&chip->voice_lock);
2394 init_waitqueue_head(&chip->interrupt_sleep);
2395 atomic_set(&chip->interrupt_sleep_count, 0);
2396 chip->card = card;
2397 chip->pci = pci;
2398 chip->irq = -1;
2399 chip->device_id = pci->device;
Auke Kok44c10132007-06-08 15:46:36 -07002400 chip->rev = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 chip->reg_area_phys = pci_resource_start(pci, 0);
2402 chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2403 pci_set_master(pci);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01002404 chip->src441_used = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002407 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 -07002408 snd_ymfpci_free(chip);
2409 return -EBUSY;
2410 }
Takashi Iwai437a5a42006-11-21 12:14:23 +01002411 if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2412 "YMFPCI", chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002413 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 snd_ymfpci_free(chip);
2415 return -EBUSY;
2416 }
2417 chip->irq = pci->irq;
2418
2419 snd_ymfpci_aclink_reset(pci);
2420 if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2421 snd_ymfpci_free(chip);
2422 return -EIO;
2423 }
2424
Clemens Ladisch102fa902006-10-11 12:05:59 +02002425 err = snd_ymfpci_request_firmware(chip);
2426 if (err < 0) {
2427 snd_printk(KERN_ERR "firmware request failed: %d\n", err);
2428 snd_ymfpci_free(chip);
2429 return err;
2430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 snd_ymfpci_download_image(chip);
2432
2433 udelay(100); /* seems we need a delay after downloading image.. */
2434
2435 if (snd_ymfpci_memalloc(chip) < 0) {
2436 snd_ymfpci_free(chip);
2437 return -EIO;
2438 }
2439
2440 if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
2441 snd_ymfpci_free(chip);
2442 return err;
2443 }
2444
2445#ifdef CONFIG_PM
2446 chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
2447 if (chip->saved_regs == NULL) {
2448 snd_ymfpci_free(chip);
2449 return -ENOMEM;
2450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451#endif
2452
2453 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2454 snd_ymfpci_free(chip);
2455 return err;
2456 }
2457
2458 snd_ymfpci_proc_init(card, chip);
2459
2460 snd_card_set_dev(card, &pci->dev);
2461
2462 *rchip = chip;
2463 return 0;
2464}