blob: 5bde816cd5c4eeccd3a7a2f8441ed7974744e8a8 [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);
285 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
286 pvoice->ypcm = NULL;
287 pvoice->interrupt = NULL;
288 spin_unlock_irqrestore(&chip->voice_lock, flags);
289 return 0;
290}
291
292/*
293 * PCM part
294 */
295
Takashi Iwai208a1b42005-11-17 14:53:41 +0100296static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100298 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 u32 pos, delta;
300
301 if ((ypcm = voice->ypcm) == NULL)
302 return;
303 if (ypcm->substream == NULL)
304 return;
305 spin_lock(&chip->reg_lock);
306 if (ypcm->running) {
307 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
308 if (pos < ypcm->last_pos)
309 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
310 else
311 delta = pos - ypcm->last_pos;
312 ypcm->period_pos += delta;
313 ypcm->last_pos = pos;
314 if (ypcm->period_pos >= ypcm->period_size) {
315 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
316 ypcm->period_pos %= ypcm->period_size;
317 spin_unlock(&chip->reg_lock);
318 snd_pcm_period_elapsed(ypcm->substream);
319 spin_lock(&chip->reg_lock);
320 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200321
322 if (unlikely(ypcm->update_pcm_vol)) {
323 unsigned int subs = ypcm->substream->number;
324 unsigned int next_bank = 1 - chip->active_bank;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100325 struct snd_ymfpci_playback_bank *bank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200326 u32 volume;
327
328 bank = &voice->bank[next_bank];
329 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
330 bank->left_gain_end = volume;
331 if (ypcm->output_rear)
332 bank->eff2_gain_end = volume;
333 if (ypcm->voices[1])
334 bank = &ypcm->voices[1]->bank[next_bank];
335 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
336 bank->right_gain_end = volume;
337 if (ypcm->output_rear)
338 bank->eff3_gain_end = volume;
339 ypcm->update_pcm_vol--;
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 spin_unlock(&chip->reg_lock);
343}
344
Takashi Iwai208a1b42005-11-17 14:53:41 +0100345static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100347 struct snd_pcm_runtime *runtime = substream->runtime;
348 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
349 struct snd_ymfpci *chip = ypcm->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 u32 pos, delta;
351
352 spin_lock(&chip->reg_lock);
353 if (ypcm->running) {
354 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
355 if (pos < ypcm->last_pos)
356 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
357 else
358 delta = pos - ypcm->last_pos;
359 ypcm->period_pos += delta;
360 ypcm->last_pos = pos;
361 if (ypcm->period_pos >= ypcm->period_size) {
362 ypcm->period_pos %= ypcm->period_size;
363 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
364 spin_unlock(&chip->reg_lock);
365 snd_pcm_period_elapsed(substream);
366 spin_lock(&chip->reg_lock);
367 }
368 }
369 spin_unlock(&chip->reg_lock);
370}
371
Takashi Iwai208a1b42005-11-17 14:53:41 +0100372static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int cmd)
374{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100375 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
376 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int result = 0;
378
379 spin_lock(&chip->reg_lock);
380 if (ypcm->voices[0] == NULL) {
381 result = -EINVAL;
382 goto __unlock;
383 }
384 switch (cmd) {
385 case SNDRV_PCM_TRIGGER_START:
386 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
387 case SNDRV_PCM_TRIGGER_RESUME:
388 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
389 if (ypcm->voices[1] != NULL)
390 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
391 ypcm->running = 1;
392 break;
393 case SNDRV_PCM_TRIGGER_STOP:
394 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
395 case SNDRV_PCM_TRIGGER_SUSPEND:
396 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
397 if (ypcm->voices[1] != NULL)
398 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
399 ypcm->running = 0;
400 break;
401 default:
402 result = -EINVAL;
403 break;
404 }
405 __unlock:
406 spin_unlock(&chip->reg_lock);
407 return result;
408}
Takashi Iwai208a1b42005-11-17 14:53:41 +0100409static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int cmd)
411{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100412 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
413 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 int result = 0;
415 u32 tmp;
416
417 spin_lock(&chip->reg_lock);
418 switch (cmd) {
419 case SNDRV_PCM_TRIGGER_START:
420 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
421 case SNDRV_PCM_TRIGGER_RESUME:
422 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
423 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
424 ypcm->running = 1;
425 break;
426 case SNDRV_PCM_TRIGGER_STOP:
427 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
428 case SNDRV_PCM_TRIGGER_SUSPEND:
429 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
430 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
431 ypcm->running = 0;
432 break;
433 default:
434 result = -EINVAL;
435 break;
436 }
437 spin_unlock(&chip->reg_lock);
438 return result;
439}
440
Takashi Iwai208a1b42005-11-17 14:53:41 +0100441static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 int err;
444
445 if (ypcm->voices[1] != NULL && voices < 2) {
446 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
447 ypcm->voices[1] = NULL;
448 }
449 if (voices == 1 && ypcm->voices[0] != NULL)
450 return 0; /* already allocated */
451 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
452 return 0; /* already allocated */
453 if (voices > 1) {
454 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
455 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
456 ypcm->voices[0] = NULL;
457 }
458 }
459 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
460 if (err < 0)
461 return err;
462 ypcm->voices[0]->ypcm = ypcm;
463 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
464 if (voices > 1) {
465 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
466 ypcm->voices[1]->ypcm = ypcm;
467 }
468 return 0;
469}
470
Takashi Iwai208a1b42005-11-17 14:53:41 +0100471static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
472 struct snd_pcm_runtime *runtime,
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200473 int has_pcm_volume)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100475 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 u32 format;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200477 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
478 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
479 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
Takashi Iwai208a1b42005-11-17 14:53:41 +0100480 struct snd_ymfpci_playback_bank *bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 unsigned int nbank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200482 u32 vol_left, vol_right;
483 u8 use_left, use_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 snd_assert(voice != NULL, return);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200486 if (runtime->channels == 1) {
487 use_left = 1;
488 use_right = 1;
489 } else {
490 use_left = (voiceidx & 1) == 0;
491 use_right = !use_left;
492 }
493 if (has_pcm_volume) {
494 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
495 [ypcm->substream->number].left << 15);
496 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
497 [ypcm->substream->number].right << 15);
498 } else {
499 vol_left = cpu_to_le32(0x40000000);
500 vol_right = cpu_to_le32(0x40000000);
501 }
502 format = runtime->channels == 2 ? 0x00010000 : 0;
503 if (snd_pcm_format_width(runtime->format) == 8)
504 format |= 0x80000000;
505 if (runtime->channels == 2 && (voiceidx & 1) != 0)
506 format |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 for (nbank = 0; nbank < 2; nbank++) {
508 bank = &voice->bank[nbank];
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200509 memset(bank, 0, sizeof(*bank));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 bank->format = cpu_to_le32(format);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200511 bank->base = cpu_to_le32(runtime->dma_addr);
512 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 bank->lpfQ = cpu_to_le32(lpfQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 bank->delta =
515 bank->delta_end = cpu_to_le32(delta);
516 bank->lpfK =
517 bank->lpfK_end = cpu_to_le32(lpfK);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200518 bank->eg_gain =
519 bank->eg_gain_end = cpu_to_le32(0x40000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200521 if (ypcm->output_front) {
522 if (use_left) {
523 bank->left_gain =
524 bank->left_gain_end = vol_left;
525 }
526 if (use_right) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 bank->right_gain =
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200528 bank->right_gain_end = vol_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200530 }
531 if (ypcm->output_rear) {
Jaroslav Kysela5a25c5c2006-01-18 08:02:24 +0100532 if (!ypcm->swap_rear) {
533 if (use_left) {
534 bank->eff2_gain =
535 bank->eff2_gain_end = vol_left;
536 }
537 if (use_right) {
538 bank->eff3_gain =
539 bank->eff3_gain_end = vol_right;
540 }
541 } else {
542 /* The SPDIF out channels seem to be swapped, so we have
543 * to swap them here, too. The rear analog out channels
544 * will be wrong, but otherwise AC3 would not work.
545 */
546 if (use_left) {
547 bank->eff3_gain =
548 bank->eff3_gain_end = vol_left;
549 }
550 if (use_right) {
551 bank->eff2_gain =
552 bank->eff2_gain_end = vol_right;
553 }
554 }
555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557}
558
Takashi Iwai208a1b42005-11-17 14:53:41 +0100559static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
562 4096, &chip->ac3_tmp_base) < 0)
563 return -ENOMEM;
564
565 chip->bank_effect[3][0]->base =
566 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
567 chip->bank_effect[3][0]->loop_end =
568 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
569 chip->bank_effect[4][0]->base =
570 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
571 chip->bank_effect[4][0]->loop_end =
572 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
573
574 spin_lock_irq(&chip->reg_lock);
575 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
576 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
577 spin_unlock_irq(&chip->reg_lock);
578 return 0;
579}
580
Takashi Iwai208a1b42005-11-17 14:53:41 +0100581static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 spin_lock_irq(&chip->reg_lock);
584 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
585 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
586 spin_unlock_irq(&chip->reg_lock);
587 // snd_ymfpci_irq_wait(chip);
588 if (chip->ac3_tmp_base.area) {
589 snd_dma_free_pages(&chip->ac3_tmp_base);
590 chip->ac3_tmp_base.area = NULL;
591 }
592 return 0;
593}
594
Takashi Iwai208a1b42005-11-17 14:53:41 +0100595static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
596 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100598 struct snd_pcm_runtime *runtime = substream->runtime;
599 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int err;
601
602 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
603 return err;
604 if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
605 return err;
606 return 0;
607}
608
Takashi Iwai208a1b42005-11-17 14:53:41 +0100609static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100611 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
612 struct snd_pcm_runtime *runtime = substream->runtime;
613 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (runtime->private_data == NULL)
616 return 0;
617 ypcm = runtime->private_data;
618
619 /* wait, until the PCI operations are not finished */
620 snd_ymfpci_irq_wait(chip);
621 snd_pcm_lib_free_pages(substream);
622 if (ypcm->voices[1]) {
623 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
624 ypcm->voices[1] = NULL;
625 }
626 if (ypcm->voices[0]) {
627 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
628 ypcm->voices[0] = NULL;
629 }
630 return 0;
631}
632
Takashi Iwai208a1b42005-11-17 14:53:41 +0100633static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100635 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
636 struct snd_pcm_runtime *runtime = substream->runtime;
637 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 unsigned int nvoice;
639
640 ypcm->period_size = runtime->period_size;
641 ypcm->buffer_size = runtime->buffer_size;
642 ypcm->period_pos = 0;
643 ypcm->last_pos = 0;
644 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200645 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
646 substream->pcm == chip->pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return 0;
648}
649
Takashi Iwai208a1b42005-11-17 14:53:41 +0100650static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
651 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
654}
655
Takashi Iwai208a1b42005-11-17 14:53:41 +0100656static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100658 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* wait, until the PCI operations are not finished */
661 snd_ymfpci_irq_wait(chip);
662 return snd_pcm_lib_free_pages(substream);
663}
664
Takashi Iwai208a1b42005-11-17 14:53:41 +0100665static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100667 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
668 struct snd_pcm_runtime *runtime = substream->runtime;
669 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
670 struct snd_ymfpci_capture_bank * bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 int nbank;
672 u32 rate, format;
673
674 ypcm->period_size = runtime->period_size;
675 ypcm->buffer_size = runtime->buffer_size;
676 ypcm->period_pos = 0;
677 ypcm->last_pos = 0;
678 ypcm->shift = 0;
679 rate = ((48000 * 4096) / runtime->rate) - 1;
680 format = 0;
681 if (runtime->channels == 2) {
682 format |= 2;
683 ypcm->shift++;
684 }
685 if (snd_pcm_format_width(runtime->format) == 8)
686 format |= 1;
687 else
688 ypcm->shift++;
689 switch (ypcm->capture_bank_number) {
690 case 0:
691 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
692 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
693 break;
694 case 1:
695 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
696 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
697 break;
698 }
699 for (nbank = 0; nbank < 2; nbank++) {
700 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
701 bank->base = cpu_to_le32(runtime->dma_addr);
702 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
703 bank->start = 0;
704 bank->num_of_loops = 0;
705 }
706 return 0;
707}
708
Takashi Iwai208a1b42005-11-17 14:53:41 +0100709static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100711 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
712 struct snd_pcm_runtime *runtime = substream->runtime;
713 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
714 struct snd_ymfpci_voice *voice = ypcm->voices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (!(ypcm->running && voice))
717 return 0;
718 return le32_to_cpu(voice->bank[chip->active_bank].start);
719}
720
Takashi Iwai208a1b42005-11-17 14:53:41 +0100721static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100723 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
724 struct snd_pcm_runtime *runtime = substream->runtime;
725 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (!ypcm->running)
728 return 0;
729 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
730}
731
Takashi Iwai208a1b42005-11-17 14:53:41 +0100732static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 wait_queue_t wait;
735 int loops = 4;
736
737 while (loops-- > 0) {
738 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
739 continue;
740 init_waitqueue_entry(&wait, current);
741 add_wait_queue(&chip->interrupt_sleep, &wait);
742 atomic_inc(&chip->interrupt_sleep_count);
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200743 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 remove_wait_queue(&chip->interrupt_sleep, &wait);
745 }
746}
747
David Howells7d12e782006-10-05 14:55:46 +0100748static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100750 struct snd_ymfpci *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 u32 status, nvoice, mode;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100752 struct snd_ymfpci_voice *voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
755 if (status & 0x80000000) {
756 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
757 spin_lock(&chip->voice_lock);
758 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
759 voice = &chip->voices[nvoice];
760 if (voice->interrupt)
761 voice->interrupt(chip, voice);
762 }
763 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
764 if (chip->capture_substream[nvoice])
765 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
766 }
767#if 0
768 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
769 if (chip->effect_substream[nvoice])
770 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
771 }
772#endif
773 spin_unlock(&chip->voice_lock);
774 spin_lock(&chip->reg_lock);
775 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
776 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
777 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
778 spin_unlock(&chip->reg_lock);
779
780 if (atomic_read(&chip->interrupt_sleep_count)) {
781 atomic_set(&chip->interrupt_sleep_count, 0);
782 wake_up(&chip->interrupt_sleep);
783 }
784 }
785
786 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
787 if (status & 1) {
788 if (chip->timer)
789 snd_timer_interrupt(chip->timer, chip->timer->sticks);
790 }
791 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
792
793 if (chip->rawmidi)
David Howells7d12e782006-10-05 14:55:46 +0100794 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return IRQ_HANDLED;
796}
797
Takashi Iwai208a1b42005-11-17 14:53:41 +0100798static struct snd_pcm_hardware snd_ymfpci_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 .info = (SNDRV_PCM_INFO_MMAP |
801 SNDRV_PCM_INFO_MMAP_VALID |
802 SNDRV_PCM_INFO_INTERLEAVED |
803 SNDRV_PCM_INFO_BLOCK_TRANSFER |
804 SNDRV_PCM_INFO_PAUSE |
805 SNDRV_PCM_INFO_RESUME),
806 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
807 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
808 .rate_min = 8000,
809 .rate_max = 48000,
810 .channels_min = 1,
811 .channels_max = 2,
812 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
813 .period_bytes_min = 64,
814 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
815 .periods_min = 3,
816 .periods_max = 1024,
817 .fifo_size = 0,
818};
819
Takashi Iwai208a1b42005-11-17 14:53:41 +0100820static struct snd_pcm_hardware snd_ymfpci_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 .info = (SNDRV_PCM_INFO_MMAP |
823 SNDRV_PCM_INFO_MMAP_VALID |
824 SNDRV_PCM_INFO_INTERLEAVED |
825 SNDRV_PCM_INFO_BLOCK_TRANSFER |
826 SNDRV_PCM_INFO_PAUSE |
827 SNDRV_PCM_INFO_RESUME),
828 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
829 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
830 .rate_min = 8000,
831 .rate_max = 48000,
832 .channels_min = 1,
833 .channels_max = 2,
834 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
835 .period_bytes_min = 64,
836 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
837 .periods_min = 3,
838 .periods_max = 1024,
839 .fifo_size = 0,
840};
841
Takashi Iwai208a1b42005-11-17 14:53:41 +0100842static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Jesper Juhl4d572772005-05-30 17:30:32 +0200844 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Takashi Iwai208a1b42005-11-17 14:53:41 +0100847static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100849 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
850 struct snd_pcm_runtime *runtime = substream->runtime;
851 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200853 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (ypcm == NULL)
855 return -ENOMEM;
856 ypcm->chip = chip;
857 ypcm->type = PLAYBACK_VOICE;
858 ypcm->substream = substream;
859 runtime->hw = snd_ymfpci_playback;
860 runtime->private_data = ypcm;
861 runtime->private_free = snd_ymfpci_pcm_free_substream;
862 /* FIXME? True value is 256/48 = 5.33333 ms */
863 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
864 return 0;
865}
866
867/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100868static void ymfpci_open_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 if (! chip->rear_opened) {
871 if (! chip->spdif_opened) /* set AC3 */
872 snd_ymfpci_writel(chip, YDSXGR_MODE,
873 snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
874 /* enable second codec (4CHEN) */
875 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
876 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
877 }
878}
879
880/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100881static void ymfpci_close_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 if (! chip->rear_opened) {
884 if (! chip->spdif_opened)
885 snd_ymfpci_writel(chip, YDSXGR_MODE,
886 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
887 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
888 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
889 }
890}
891
Takashi Iwai208a1b42005-11-17 14:53:41 +0100892static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100894 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
895 struct snd_pcm_runtime *runtime = substream->runtime;
896 struct snd_ymfpci_pcm *ypcm;
897 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 int err;
899
900 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
901 return err;
902 ypcm = runtime->private_data;
903 ypcm->output_front = 1;
904 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
Glen Masgaid9301262006-10-10 09:27:19 +0200905 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 spin_lock_irq(&chip->reg_lock);
907 if (ypcm->output_rear) {
908 ymfpci_open_extension(chip);
909 chip->rear_opened++;
910 }
911 spin_unlock_irq(&chip->reg_lock);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200912
913 kctl = chip->pcm_mixer[substream->number].ctl;
914 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
915 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return 0;
917}
918
Takashi Iwai208a1b42005-11-17 14:53:41 +0100919static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100921 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
922 struct snd_pcm_runtime *runtime = substream->runtime;
923 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 int err;
925
926 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
927 return err;
928 ypcm = runtime->private_data;
929 ypcm->output_front = 0;
930 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200931 ypcm->swap_rear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 spin_lock_irq(&chip->reg_lock);
933 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
934 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
935 ymfpci_open_extension(chip);
936 chip->spdif_pcm_bits = chip->spdif_bits;
937 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
938 chip->spdif_opened++;
939 spin_unlock_irq(&chip->reg_lock);
940
941 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
942 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
943 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
944 return 0;
945}
946
Takashi Iwai208a1b42005-11-17 14:53:41 +0100947static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100949 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
950 struct snd_pcm_runtime *runtime = substream->runtime;
951 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 int err;
953
954 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
955 return err;
956 ypcm = runtime->private_data;
957 ypcm->output_front = 0;
958 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200959 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 spin_lock_irq(&chip->reg_lock);
961 ymfpci_open_extension(chip);
962 chip->rear_opened++;
963 spin_unlock_irq(&chip->reg_lock);
964 return 0;
965}
966
Takashi Iwai208a1b42005-11-17 14:53:41 +0100967static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 u32 capture_bank_number)
969{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100970 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
971 struct snd_pcm_runtime *runtime = substream->runtime;
972 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200974 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (ypcm == NULL)
976 return -ENOMEM;
977 ypcm->chip = chip;
978 ypcm->type = capture_bank_number + CAPTURE_REC;
979 ypcm->substream = substream;
980 ypcm->capture_bank_number = capture_bank_number;
981 chip->capture_substream[capture_bank_number] = substream;
982 runtime->hw = snd_ymfpci_capture;
983 /* FIXME? True value is 256/48 = 5.33333 ms */
984 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
985 runtime->private_data = ypcm;
986 runtime->private_free = snd_ymfpci_pcm_free_substream;
987 snd_ymfpci_hw_start(chip);
988 return 0;
989}
990
Takashi Iwai208a1b42005-11-17 14:53:41 +0100991static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 return snd_ymfpci_capture_open(substream, 0);
994}
995
Takashi Iwai208a1b42005-11-17 14:53:41 +0100996static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 return snd_ymfpci_capture_open(substream, 1);
999}
1000
Takashi Iwai208a1b42005-11-17 14:53:41 +01001001static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 return 0;
1004}
1005
Takashi Iwai208a1b42005-11-17 14:53:41 +01001006static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001008 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1009 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1010 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 spin_lock_irq(&chip->reg_lock);
1013 if (ypcm->output_rear && chip->rear_opened > 0) {
1014 chip->rear_opened--;
1015 ymfpci_close_extension(chip);
1016 }
1017 spin_unlock_irq(&chip->reg_lock);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001018 kctl = chip->pcm_mixer[substream->number].ctl;
1019 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1020 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return snd_ymfpci_playback_close_1(substream);
1022}
1023
Takashi Iwai208a1b42005-11-17 14:53:41 +01001024static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001026 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 spin_lock_irq(&chip->reg_lock);
1029 chip->spdif_opened = 0;
1030 ymfpci_close_extension(chip);
1031 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1032 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1033 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1034 spin_unlock_irq(&chip->reg_lock);
1035 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1036 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1037 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1038 return snd_ymfpci_playback_close_1(substream);
1039}
1040
Takashi Iwai208a1b42005-11-17 14:53:41 +01001041static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001043 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 spin_lock_irq(&chip->reg_lock);
1046 if (chip->rear_opened > 0) {
1047 chip->rear_opened--;
1048 ymfpci_close_extension(chip);
1049 }
1050 spin_unlock_irq(&chip->reg_lock);
1051 return snd_ymfpci_playback_close_1(substream);
1052}
1053
Takashi Iwai208a1b42005-11-17 14:53:41 +01001054static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001056 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1057 struct snd_pcm_runtime *runtime = substream->runtime;
1058 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 if (ypcm != NULL) {
1061 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1062 snd_ymfpci_hw_stop(chip);
1063 }
1064 return 0;
1065}
1066
Takashi Iwai208a1b42005-11-17 14:53:41 +01001067static struct snd_pcm_ops snd_ymfpci_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 .open = snd_ymfpci_playback_open,
1069 .close = snd_ymfpci_playback_close,
1070 .ioctl = snd_pcm_lib_ioctl,
1071 .hw_params = snd_ymfpci_playback_hw_params,
1072 .hw_free = snd_ymfpci_playback_hw_free,
1073 .prepare = snd_ymfpci_playback_prepare,
1074 .trigger = snd_ymfpci_playback_trigger,
1075 .pointer = snd_ymfpci_playback_pointer,
1076};
1077
Takashi Iwai208a1b42005-11-17 14:53:41 +01001078static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 .open = snd_ymfpci_capture_rec_open,
1080 .close = snd_ymfpci_capture_close,
1081 .ioctl = snd_pcm_lib_ioctl,
1082 .hw_params = snd_ymfpci_capture_hw_params,
1083 .hw_free = snd_ymfpci_capture_hw_free,
1084 .prepare = snd_ymfpci_capture_prepare,
1085 .trigger = snd_ymfpci_capture_trigger,
1086 .pointer = snd_ymfpci_capture_pointer,
1087};
1088
Takashi Iwai208a1b42005-11-17 14:53:41 +01001089int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001091 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 int err;
1093
1094 if (rpcm)
1095 *rpcm = NULL;
1096 if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1097 return err;
1098 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1101 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1102
1103 /* global setup */
1104 pcm->info_flags = 0;
1105 strcpy(pcm->name, "YMFPCI");
1106 chip->pcm = pcm;
1107
1108 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1109 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1110
1111 if (rpcm)
1112 *rpcm = pcm;
1113 return 0;
1114}
1115
Takashi Iwai208a1b42005-11-17 14:53:41 +01001116static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 .open = snd_ymfpci_capture_ac97_open,
1118 .close = snd_ymfpci_capture_close,
1119 .ioctl = snd_pcm_lib_ioctl,
1120 .hw_params = snd_ymfpci_capture_hw_params,
1121 .hw_free = snd_ymfpci_capture_hw_free,
1122 .prepare = snd_ymfpci_capture_prepare,
1123 .trigger = snd_ymfpci_capture_trigger,
1124 .pointer = snd_ymfpci_capture_pointer,
1125};
1126
Takashi Iwai208a1b42005-11-17 14:53:41 +01001127int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001129 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 int err;
1131
1132 if (rpcm)
1133 *rpcm = NULL;
1134 if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1135 return err;
1136 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1139
1140 /* global setup */
1141 pcm->info_flags = 0;
1142 sprintf(pcm->name, "YMFPCI - %s",
1143 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1144 chip->pcm2 = pcm;
1145
1146 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1147 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1148
1149 if (rpcm)
1150 *rpcm = pcm;
1151 return 0;
1152}
1153
Takashi Iwai208a1b42005-11-17 14:53:41 +01001154static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 .open = snd_ymfpci_playback_spdif_open,
1156 .close = snd_ymfpci_playback_spdif_close,
1157 .ioctl = snd_pcm_lib_ioctl,
1158 .hw_params = snd_ymfpci_playback_hw_params,
1159 .hw_free = snd_ymfpci_playback_hw_free,
1160 .prepare = snd_ymfpci_playback_prepare,
1161 .trigger = snd_ymfpci_playback_trigger,
1162 .pointer = snd_ymfpci_playback_pointer,
1163};
1164
Takashi Iwai208a1b42005-11-17 14:53:41 +01001165int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001167 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 int err;
1169
1170 if (rpcm)
1171 *rpcm = NULL;
1172 if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1173 return err;
1174 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1177
1178 /* global setup */
1179 pcm->info_flags = 0;
1180 strcpy(pcm->name, "YMFPCI - IEC958");
1181 chip->pcm_spdif = pcm;
1182
1183 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1184 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1185
1186 if (rpcm)
1187 *rpcm = pcm;
1188 return 0;
1189}
1190
Takashi Iwai208a1b42005-11-17 14:53:41 +01001191static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 .open = snd_ymfpci_playback_4ch_open,
1193 .close = snd_ymfpci_playback_4ch_close,
1194 .ioctl = snd_pcm_lib_ioctl,
1195 .hw_params = snd_ymfpci_playback_hw_params,
1196 .hw_free = snd_ymfpci_playback_hw_free,
1197 .prepare = snd_ymfpci_playback_prepare,
1198 .trigger = snd_ymfpci_playback_trigger,
1199 .pointer = snd_ymfpci_playback_pointer,
1200};
1201
Takashi Iwai208a1b42005-11-17 14:53:41 +01001202int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001204 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int err;
1206
1207 if (rpcm)
1208 *rpcm = NULL;
1209 if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1210 return err;
1211 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1214
1215 /* global setup */
1216 pcm->info_flags = 0;
1217 strcpy(pcm->name, "YMFPCI - Rear PCM");
1218 chip->pcm_4ch = pcm;
1219
1220 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1221 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1222
1223 if (rpcm)
1224 *rpcm = pcm;
1225 return 0;
1226}
1227
Takashi Iwai208a1b42005-11-17 14:53:41 +01001228static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1231 uinfo->count = 1;
1232 return 0;
1233}
1234
Takashi Iwai208a1b42005-11-17 14:53:41 +01001235static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1236 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001238 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 spin_lock_irq(&chip->reg_lock);
1241 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1242 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001243 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 spin_unlock_irq(&chip->reg_lock);
1245 return 0;
1246}
1247
Takashi Iwai208a1b42005-11-17 14:53:41 +01001248static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1249 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001251 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 unsigned int val;
1253 int change;
1254
1255 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1256 (ucontrol->value.iec958.status[1] << 8);
1257 spin_lock_irq(&chip->reg_lock);
1258 change = chip->spdif_bits != val;
1259 chip->spdif_bits = val;
1260 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1261 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1262 spin_unlock_irq(&chip->reg_lock);
1263 return change;
1264}
1265
Takashi Iwai208a1b42005-11-17 14:53:41 +01001266static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1269 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1270 .info = snd_ymfpci_spdif_default_info,
1271 .get = snd_ymfpci_spdif_default_get,
1272 .put = snd_ymfpci_spdif_default_put
1273};
1274
Takashi Iwai208a1b42005-11-17 14:53:41 +01001275static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1278 uinfo->count = 1;
1279 return 0;
1280}
1281
Takashi Iwai208a1b42005-11-17 14:53:41 +01001282static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1283 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001285 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 spin_lock_irq(&chip->reg_lock);
1288 ucontrol->value.iec958.status[0] = 0x3e;
1289 ucontrol->value.iec958.status[1] = 0xff;
1290 spin_unlock_irq(&chip->reg_lock);
1291 return 0;
1292}
1293
Takashi Iwai208a1b42005-11-17 14:53:41 +01001294static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1297 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1298 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1299 .info = snd_ymfpci_spdif_mask_info,
1300 .get = snd_ymfpci_spdif_mask_get,
1301};
1302
Takashi Iwai208a1b42005-11-17 14:53:41 +01001303static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1306 uinfo->count = 1;
1307 return 0;
1308}
1309
Takashi Iwai208a1b42005-11-17 14:53:41 +01001310static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1311 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001313 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 spin_lock_irq(&chip->reg_lock);
1316 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1317 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001318 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 spin_unlock_irq(&chip->reg_lock);
1320 return 0;
1321}
1322
Takashi Iwai208a1b42005-11-17 14:53:41 +01001323static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1324 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001326 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 unsigned int val;
1328 int change;
1329
1330 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1331 (ucontrol->value.iec958.status[1] << 8);
1332 spin_lock_irq(&chip->reg_lock);
1333 change = chip->spdif_pcm_bits != val;
1334 chip->spdif_pcm_bits = val;
1335 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1336 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1337 spin_unlock_irq(&chip->reg_lock);
1338 return change;
1339}
1340
Takashi Iwai208a1b42005-11-17 14:53:41 +01001341static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1344 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1345 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1346 .info = snd_ymfpci_spdif_stream_info,
1347 .get = snd_ymfpci_spdif_stream_get,
1348 .put = snd_ymfpci_spdif_stream_put
1349};
1350
Takashi Iwai208a1b42005-11-17 14:53:41 +01001351static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};
1354
1355 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1356 info->count = 1;
1357 info->value.enumerated.items = 3;
1358 if (info->value.enumerated.item > 2)
1359 info->value.enumerated.item = 2;
1360 strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
1361 return 0;
1362}
1363
Takashi Iwai208a1b42005-11-17 14:53:41 +01001364static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001366 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 u16 reg;
1368
1369 spin_lock_irq(&chip->reg_lock);
1370 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1371 spin_unlock_irq(&chip->reg_lock);
1372 if (!(reg & 0x100))
1373 value->value.enumerated.item[0] = 0;
1374 else
1375 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1376 return 0;
1377}
1378
Takashi Iwai208a1b42005-11-17 14:53:41 +01001379static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001381 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 u16 reg, old_reg;
1383
1384 spin_lock_irq(&chip->reg_lock);
1385 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1386 if (value->value.enumerated.item[0] == 0)
1387 reg = old_reg & ~0x100;
1388 else
1389 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1390 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1391 spin_unlock_irq(&chip->reg_lock);
1392 return reg != old_reg;
1393}
1394
Takashi Iwai208a1b42005-11-17 14:53:41 +01001395static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1397 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1398 .name = "Direct Recording Source",
1399 .info = snd_ymfpci_drec_source_info,
1400 .get = snd_ymfpci_drec_source_get,
1401 .put = snd_ymfpci_drec_source_put
1402};
1403
1404/*
1405 * Mixer controls
1406 */
1407
Glen Masgaid602c882005-09-28 08:19:05 +02001408#define YMFPCI_SINGLE(xname, xindex, reg, shift) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1410 .info = snd_ymfpci_info_single, \
1411 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
Glen Masgaid602c882005-09-28 08:19:05 +02001412 .private_value = ((reg) | ((shift) << 16)) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Takashi Iwai208a1b42005-11-17 14:53:41 +01001414static int snd_ymfpci_info_single(struct snd_kcontrol *kcontrol,
1415 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Glen Masgaid602c882005-09-28 08:19:05 +02001417 int reg = kcontrol->private_value & 0xffff;
1418
1419 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 case YDSXGR_SPDIFOUTCTRL: break;
1421 case YDSXGR_SPDIFINCTRL: break;
1422 default: return -EINVAL;
1423 }
Adrian Bunk467a8c22005-04-11 14:11:00 +02001424 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 uinfo->count = 1;
1426 uinfo->value.integer.min = 0;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001427 uinfo->value.integer.max = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return 0;
1429}
1430
Takashi Iwai208a1b42005-11-17 14:53:41 +01001431static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1432 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001434 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001435 int reg = kcontrol->private_value & 0xffff;
1436 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1437 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Glen Masgaid602c882005-09-28 08:19:05 +02001439 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 case YDSXGR_SPDIFOUTCTRL: break;
1441 case YDSXGR_SPDIFINCTRL: break;
1442 default: return -EINVAL;
1443 }
Glen Masgaid602c882005-09-28 08:19:05 +02001444 ucontrol->value.integer.value[0] =
1445 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return 0;
1447}
1448
Takashi Iwai208a1b42005-11-17 14:53:41 +01001449static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1450 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001452 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001453 int reg = kcontrol->private_value & 0xffff;
1454 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1455 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 int change;
1457 unsigned int val, oval;
1458
Glen Masgaid602c882005-09-28 08:19:05 +02001459 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 case YDSXGR_SPDIFOUTCTRL: break;
1461 case YDSXGR_SPDIFINCTRL: break;
1462 default: return -EINVAL;
1463 }
1464 val = (ucontrol->value.integer.value[0] & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 val <<= shift;
1466 spin_lock_irq(&chip->reg_lock);
1467 oval = snd_ymfpci_readl(chip, reg);
1468 val = (oval & ~(mask << shift)) | val;
1469 change = val != oval;
1470 snd_ymfpci_writel(chip, reg, val);
1471 spin_unlock_irq(&chip->reg_lock);
1472 return change;
1473}
1474
Takashi Iwai33925182006-08-28 13:29:42 +02001475static DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477#define YMFPCI_DOUBLE(xname, xindex, reg) \
1478{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Takashi Iwai33925182006-08-28 13:29:42 +02001479 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 .info = snd_ymfpci_info_double, \
1481 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
Takashi Iwai33925182006-08-28 13:29:42 +02001482 .private_value = reg, \
1483 .tlv = { .p = db_scale_native } }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Takashi Iwai208a1b42005-11-17 14:53:41 +01001485static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 unsigned int reg = kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (reg < 0x80 || reg >= 0xc0)
1490 return -EINVAL;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001491 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 uinfo->count = 2;
1493 uinfo->value.integer.min = 0;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001494 uinfo->value.integer.max = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return 0;
1496}
1497
Takashi Iwai208a1b42005-11-17 14:53:41 +01001498static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001500 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001502 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 unsigned int val;
1504
1505 if (reg < 0x80 || reg >= 0xc0)
1506 return -EINVAL;
1507 spin_lock_irq(&chip->reg_lock);
1508 val = snd_ymfpci_readl(chip, reg);
1509 spin_unlock_irq(&chip->reg_lock);
1510 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1511 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return 0;
1513}
1514
Takashi Iwai208a1b42005-11-17 14:53:41 +01001515static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001517 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001519 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int change;
1521 unsigned int val1, val2, oval;
1522
1523 if (reg < 0x80 || reg >= 0xc0)
1524 return -EINVAL;
1525 val1 = ucontrol->value.integer.value[0] & mask;
1526 val2 = ucontrol->value.integer.value[1] & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 val1 <<= shift_left;
1528 val2 <<= shift_right;
1529 spin_lock_irq(&chip->reg_lock);
1530 oval = snd_ymfpci_readl(chip, reg);
1531 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1532 change = val1 != oval;
1533 snd_ymfpci_writel(chip, reg, val1);
1534 spin_unlock_irq(&chip->reg_lock);
1535 return change;
1536}
1537
1538/*
1539 * 4ch duplication
1540 */
Takashi Iwai208a1b42005-11-17 14:53:41 +01001541static int snd_ymfpci_info_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1544 uinfo->count = 1;
1545 uinfo->value.integer.min = 0;
1546 uinfo->value.integer.max = 1;
1547 return 0;
1548}
1549
Takashi Iwai208a1b42005-11-17 14:53:41 +01001550static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001552 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1554 return 0;
1555}
1556
Takashi Iwai208a1b42005-11-17 14:53:41 +01001557static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001559 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 int change;
1561 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1562 if (change)
1563 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1564 return change;
1565}
1566
1567
Takashi Iwai208a1b42005-11-17 14:53:41 +01001568static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569YMFPCI_DOUBLE("Wave Playback Volume", 0, YDSXGR_NATIVEDACOUTVOL),
1570YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1571YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1572YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1573YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1574YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1575YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1576YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1577YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
1578YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1579YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1580YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1581YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
Glen Masgaid602c882005-09-28 08:19:05 +02001582YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1583YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1584YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1587 .name = "4ch Duplication",
1588 .info = snd_ymfpci_info_dup4ch,
1589 .get = snd_ymfpci_get_dup4ch,
1590 .put = snd_ymfpci_put_dup4ch,
1591},
1592};
1593
1594
1595/*
1596 * GPIO
1597 */
1598
Takashi Iwai208a1b42005-11-17 14:53:41 +01001599static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 u16 reg, mode;
1602 unsigned long flags;
1603
1604 spin_lock_irqsave(&chip->reg_lock, flags);
1605 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1606 reg &= ~(1 << (pin + 8));
1607 reg |= (1 << pin);
1608 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1609 /* set the level mode for input line */
1610 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1611 mode &= ~(3 << (pin * 2));
1612 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1613 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1614 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1615 spin_unlock_irqrestore(&chip->reg_lock, flags);
1616 return (mode >> pin) & 1;
1617}
1618
Takashi Iwai208a1b42005-11-17 14:53:41 +01001619static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 u16 reg;
1622 unsigned long flags;
1623
1624 spin_lock_irqsave(&chip->reg_lock, flags);
1625 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1626 reg &= ~(1 << pin);
1627 reg &= ~(1 << (pin + 8));
1628 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1629 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1630 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1631 spin_unlock_irqrestore(&chip->reg_lock, flags);
1632
1633 return 0;
1634}
1635
Takashi Iwai208a1b42005-11-17 14:53:41 +01001636static int snd_ymfpci_gpio_sw_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1639 uinfo->count = 1;
1640 uinfo->value.integer.min = 0;
1641 uinfo->value.integer.max = 1;
1642 return 0;
1643}
1644
Takashi Iwai208a1b42005-11-17 14:53:41 +01001645static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001647 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 int pin = (int)kcontrol->private_value;
1649 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1650 return 0;
1651}
1652
Takashi Iwai208a1b42005-11-17 14:53:41 +01001653static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001655 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 int pin = (int)kcontrol->private_value;
1657
1658 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1659 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1660 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1661 return 1;
1662 }
1663 return 0;
1664}
1665
Takashi Iwai208a1b42005-11-17 14:53:41 +01001666static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 .name = "Shared Rear/Line-In Switch",
1668 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1669 .info = snd_ymfpci_gpio_sw_info,
1670 .get = snd_ymfpci_gpio_sw_get,
1671 .put = snd_ymfpci_gpio_sw_put,
1672 .private_value = 2,
1673};
1674
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001675/*
1676 * PCM voice volume
1677 */
1678
Takashi Iwai208a1b42005-11-17 14:53:41 +01001679static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1680 struct snd_ctl_elem_info *uinfo)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001681{
1682 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1683 uinfo->count = 2;
1684 uinfo->value.integer.min = 0;
1685 uinfo->value.integer.max = 0x8000;
1686 return 0;
1687}
1688
Takashi Iwai208a1b42005-11-17 14:53:41 +01001689static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1690 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001691{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001692 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001693 unsigned int subs = kcontrol->id.subdevice;
1694
1695 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1696 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1697 return 0;
1698}
1699
Takashi Iwai208a1b42005-11-17 14:53:41 +01001700static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1701 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001702{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001703 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001704 unsigned int subs = kcontrol->id.subdevice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001705 struct snd_pcm_substream *substream;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001706 unsigned long flags;
1707
1708 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1709 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1710 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1711 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1712
Takashi Iwai208a1b42005-11-17 14:53:41 +01001713 substream = (struct snd_pcm_substream *)kcontrol->private_value;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001714 spin_lock_irqsave(&chip->voice_lock, flags);
1715 if (substream->runtime && substream->runtime->private_data) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01001716 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001717 ypcm->update_pcm_vol = 2;
1718 }
1719 spin_unlock_irqrestore(&chip->voice_lock, flags);
1720 return 1;
1721 }
1722 return 0;
1723}
1724
Takashi Iwai208a1b42005-11-17 14:53:41 +01001725static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001726 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1727 .name = "PCM Playback Volume",
1728 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1729 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1730 .info = snd_ymfpci_pcm_vol_info,
1731 .get = snd_ymfpci_pcm_vol_get,
1732 .put = snd_ymfpci_pcm_vol_put,
1733};
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736/*
1737 * Mixer routines
1738 */
1739
Takashi Iwai208a1b42005-11-17 14:53:41 +01001740static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001742 struct snd_ymfpci *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 chip->ac97_bus = NULL;
1744}
1745
Takashi Iwai208a1b42005-11-17 14:53:41 +01001746static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001748 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 chip->ac97 = NULL;
1750}
1751
Glen Masgaid9301262006-10-10 09:27:19 +02001752int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001754 struct snd_ac97_template ac97;
1755 struct snd_kcontrol *kctl;
1756 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 unsigned int idx;
1758 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001759 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 .write = snd_ymfpci_codec_write,
1761 .read = snd_ymfpci_codec_read,
1762 };
1763
1764 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1765 return err;
1766 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1767 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1768
1769 memset(&ac97, 0, sizeof(ac97));
1770 ac97.private_data = chip;
1771 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1772 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1773 return err;
1774
1775 /* to be sure */
1776 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1777 AC97_EA_VRA|AC97_EA_VRM, 0);
1778
1779 for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1780 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1781 return err;
1782 }
1783
1784 /* add S/PDIF control */
1785 snd_assert(chip->pcm_spdif != NULL, return -EIO);
1786 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1787 return err;
1788 kctl->id.device = chip->pcm_spdif->device;
1789 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1790 return err;
1791 kctl->id.device = chip->pcm_spdif->device;
1792 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1793 return err;
1794 kctl->id.device = chip->pcm_spdif->device;
1795 chip->spdif_pcm_ctl = kctl;
1796
1797 /* direct recording source */
1798 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1799 (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1800 return err;
1801
1802 /*
1803 * shared rear/line-in
1804 */
1805 if (rear_switch) {
1806 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1807 return err;
1808 }
1809
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001810 /* per-voice volume */
1811 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1812 for (idx = 0; idx < 32; ++idx) {
1813 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1814 if (!kctl)
1815 return -ENOMEM;
1816 kctl->id.device = chip->pcm->device;
1817 kctl->id.subdevice = idx;
1818 kctl->private_value = (unsigned long)substream;
1819 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1820 return err;
1821 chip->pcm_mixer[idx].left = 0x8000;
1822 chip->pcm_mixer[idx].right = 0x8000;
1823 chip->pcm_mixer[idx].ctl = kctl;
1824 substream = substream->next;
1825 }
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return 0;
1828}
1829
1830
1831/*
1832 * timer
1833 */
1834
Takashi Iwai208a1b42005-11-17 14:53:41 +01001835static int snd_ymfpci_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001837 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 unsigned long flags;
1839 unsigned int count;
1840
1841 chip = snd_timer_chip(timer);
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001842 count = (timer->sticks << 1) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 spin_lock_irqsave(&chip->reg_lock, flags);
1844 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1845 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1846 spin_unlock_irqrestore(&chip->reg_lock, flags);
1847 return 0;
1848}
1849
Takashi Iwai208a1b42005-11-17 14:53:41 +01001850static int snd_ymfpci_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001852 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 unsigned long flags;
1854
1855 chip = snd_timer_chip(timer);
1856 spin_lock_irqsave(&chip->reg_lock, flags);
1857 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1858 spin_unlock_irqrestore(&chip->reg_lock, flags);
1859 return 0;
1860}
1861
Takashi Iwai208a1b42005-11-17 14:53:41 +01001862static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 unsigned long *num, unsigned long *den)
1864{
1865 *num = 1;
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001866 *den = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return 0;
1868}
1869
Takashi Iwai208a1b42005-11-17 14:53:41 +01001870static struct snd_timer_hardware snd_ymfpci_timer_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 .flags = SNDRV_TIMER_HW_AUTO,
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001872 .resolution = 20833, /* 1/fs = 20.8333...us */
1873 .ticks = 0x8000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 .start = snd_ymfpci_timer_start,
1875 .stop = snd_ymfpci_timer_stop,
1876 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1877};
1878
Takashi Iwai208a1b42005-11-17 14:53:41 +01001879int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001881 struct snd_timer *timer = NULL;
1882 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 int err;
1884
1885 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1886 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1887 tid.card = chip->card->number;
1888 tid.device = device;
1889 tid.subdevice = 0;
1890 if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1891 strcpy(timer->name, "YMFPCI timer");
1892 timer->private_data = chip;
1893 timer->hw = snd_ymfpci_timer_hw;
1894 }
1895 chip->timer = timer;
1896 return err;
1897}
1898
1899
1900/*
1901 * proc interface
1902 */
1903
Takashi Iwai208a1b42005-11-17 14:53:41 +01001904static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1905 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001907 struct snd_ymfpci *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 int i;
1909
1910 snd_iprintf(buffer, "YMFPCI\n\n");
1911 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1912 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1913}
1914
Takashi Iwai208a1b42005-11-17 14:53:41 +01001915static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001917 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 if (! snd_card_proc_new(card, "ymfpci", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001920 snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return 0;
1922}
1923
1924/*
1925 * initialization routines
1926 */
1927
1928static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1929{
1930 u8 cmd;
1931
1932 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1933#if 0 // force to reset
1934 if (cmd & 0x03) {
1935#endif
1936 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1937 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1938 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1939 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1940 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1941#if 0
1942 }
1943#endif
1944}
1945
Takashi Iwai208a1b42005-11-17 14:53:41 +01001946static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1949}
1950
Takashi Iwai208a1b42005-11-17 14:53:41 +01001951static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
1953 u32 val;
1954 int timeout = 1000;
1955
1956 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1957 if (val)
1958 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
1959 while (timeout-- > 0) {
1960 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
1961 if ((val & 0x00000002) == 0)
1962 break;
1963 }
1964}
1965
Clemens Ladisch102fa902006-10-11 12:05:59 +02001966#define FIRMWARE_IN_THE_KERNEL
1967
1968#ifdef FIRMWARE_IN_THE_KERNEL
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970#include "ymfpci_image.h"
1971
Clemens Ladisch102fa902006-10-11 12:05:59 +02001972static struct firmware snd_ymfpci_dsp_microcode = {
1973 .size = YDSXG_DSPLENGTH,
1974 .data = (u8 *)DspInst,
1975};
1976static struct firmware snd_ymfpci_controller_microcode = {
1977 .size = YDSXG_CTRLLENGTH,
1978 .data = (u8 *)CntrlInst,
1979};
1980static struct firmware snd_ymfpci_controller_1e_microcode = {
1981 .size = YDSXG_CTRLLENGTH,
1982 .data = (u8 *)CntrlInst1E,
1983};
1984#endif
1985
1986#ifdef __LITTLE_ENDIAN
1987static inline void snd_ymfpci_convert_from_le(const struct firmware *fw) { }
1988#else
1989static void snd_ymfpci_convert_from_le(const struct firmware *fw)
1990{
1991 int i;
1992 u32 *data = (u32 *)fw->data;
1993
1994 for (i = 0; i < fw->size / 4; ++i)
1995 le32_to_cpus(&data[i]);
1996}
1997#endif
1998
1999static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2000{
2001 int err, is_1e;
2002 const char *name;
2003
2004 err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2005 &chip->pci->dev);
2006 if (err >= 0) {
2007 if (chip->dsp_microcode->size == YDSXG_DSPLENGTH)
2008 snd_ymfpci_convert_from_le(chip->dsp_microcode);
2009 else {
2010 snd_printk(KERN_ERR "DSP microcode has wrong size\n");
2011 err = -EINVAL;
2012 }
2013 }
2014 if (err < 0) {
2015#ifdef FIRMWARE_IN_THE_KERNEL
2016 chip->dsp_microcode = &snd_ymfpci_dsp_microcode;
2017#else
2018 return err;
2019#endif
2020 }
2021 is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2022 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2023 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2024 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2025 name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2026 err = request_firmware(&chip->controller_microcode, name,
2027 &chip->pci->dev);
2028 if (err >= 0) {
2029 if (chip->controller_microcode->size == YDSXG_CTRLLENGTH)
2030 snd_ymfpci_convert_from_le(chip->controller_microcode);
2031 else {
2032 snd_printk(KERN_ERR "controller microcode"
2033 " has wrong size\n");
2034 err = -EINVAL;
2035 }
2036 }
2037 if (err < 0) {
2038#ifdef FIRMWARE_IN_THE_KERNEL
2039 chip->controller_microcode =
2040 is_1e ? &snd_ymfpci_controller_1e_microcode
2041 : &snd_ymfpci_controller_microcode;
2042#else
2043 return err;
2044#endif
2045 }
2046 return 0;
2047}
2048
Takashi Iwai208a1b42005-11-17 14:53:41 +01002049static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 int i;
2052 u16 ctrl;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002053 u32 *inst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2056 snd_ymfpci_disable_dsp(chip);
2057 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2058 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2059 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2060 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2061 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2062 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2063 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2064 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2065 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2066
2067 /* setup DSP instruction code */
Clemens Ladisch102fa902006-10-11 12:05:59 +02002068 inst = (u32 *)chip->dsp_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002070 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), inst[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 /* setup control instruction code */
Clemens Ladisch102fa902006-10-11 12:05:59 +02002073 inst = (u32 *)chip->controller_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2075 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), inst[i]);
2076
2077 snd_ymfpci_enable_dsp(chip);
2078}
2079
Takashi Iwai208a1b42005-11-17 14:53:41 +01002080static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 long size, playback_ctrl_size;
2083 int voice, bank, reg;
2084 u8 *ptr;
2085 dma_addr_t ptr_addr;
2086
2087 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2088 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2089 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2090 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2091 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2092
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002093 size = ALIGN(playback_ctrl_size, 0x100) +
2094 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2095 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2096 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 chip->work_size;
2098 /* work_ptr must be aligned to 256 bytes, but it's already
2099 covered with the kernel page allocation mechanism */
2100 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2101 size, &chip->work_ptr) < 0)
2102 return -ENOMEM;
2103 ptr = chip->work_ptr.area;
2104 ptr_addr = chip->work_ptr.addr;
2105 memset(ptr, 0, size); /* for sure */
2106
2107 chip->bank_base_playback = ptr;
2108 chip->bank_base_playback_addr = ptr_addr;
2109 chip->ctrl_playback = (u32 *)ptr;
2110 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002111 ptr += ALIGN(playback_ctrl_size, 0x100);
2112 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2114 chip->voices[voice].number = voice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002115 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 chip->voices[voice].bank_addr = ptr_addr;
2117 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002118 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 ptr += chip->bank_size_playback;
2120 ptr_addr += chip->bank_size_playback;
2121 }
2122 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002123 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2124 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 chip->bank_base_capture = ptr;
2126 chip->bank_base_capture_addr = ptr_addr;
2127 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2128 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002129 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 ptr += chip->bank_size_capture;
2131 ptr_addr += chip->bank_size_capture;
2132 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002133 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2134 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 chip->bank_base_effect = ptr;
2136 chip->bank_base_effect_addr = ptr_addr;
2137 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2138 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002139 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 ptr += chip->bank_size_effect;
2141 ptr_addr += chip->bank_size_effect;
2142 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002143 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2144 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 chip->work_base = ptr;
2146 chip->work_base_addr = ptr_addr;
2147
2148 snd_assert(ptr + chip->work_size == chip->work_ptr.area + chip->work_ptr.bytes, );
2149
2150 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2151 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2152 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2153 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2154 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2155
2156 /* S/PDIF output initialization */
2157 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2158 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2159 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2160
2161 /* S/PDIF input initialization */
2162 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2163
2164 /* digital mixer setup */
2165 for (reg = 0x80; reg < 0xc0; reg += 4)
2166 snd_ymfpci_writel(chip, reg, 0);
2167 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2168 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2169 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2170 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2171 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2172 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2173 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2174
2175 return 0;
2176}
2177
Takashi Iwai208a1b42005-11-17 14:53:41 +01002178static int snd_ymfpci_free(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 u16 ctrl;
2181
2182 snd_assert(chip != NULL, return -EINVAL);
2183
2184 if (chip->res_reg_area) { /* don't touch busy hardware */
2185 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2186 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2187 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2188 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2189 snd_ymfpci_disable_dsp(chip);
2190 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2191 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2192 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2193 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2194 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2195 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2196 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2197 }
2198
2199 snd_ymfpci_ac3_done(chip);
2200
2201 /* Set PCI device to D3 state */
2202#if 0
2203 /* FIXME: temporarily disabled, otherwise we cannot fire up
2204 * the chip again unless reboot. ACPI bug?
2205 */
2206 pci_set_power_state(chip->pci, 3);
2207#endif
2208
2209#ifdef CONFIG_PM
2210 vfree(chip->saved_regs);
2211#endif
Takashi Iwaib1d57762005-10-10 11:56:31 +02002212 release_and_free_resource(chip->mpu_res);
2213 release_and_free_resource(chip->fm_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 snd_ymfpci_free_gameport(chip);
2215 if (chip->reg_area_virt)
2216 iounmap(chip->reg_area_virt);
2217 if (chip->work_ptr.area)
2218 snd_dma_free_pages(&chip->work_ptr);
2219
2220 if (chip->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01002221 free_irq(chip->irq, chip);
Takashi Iwaib1d57762005-10-10 11:56:31 +02002222 release_and_free_resource(chip->res_reg_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2225
2226 pci_disable_device(chip->pci);
Clemens Ladisch102fa902006-10-11 12:05:59 +02002227#ifdef FIRMWARE_IN_THE_KERNEL
2228 if (chip->dsp_microcode != &snd_ymfpci_dsp_microcode)
2229#endif
2230 release_firmware(chip->dsp_microcode);
2231#ifdef FIRMWARE_IN_THE_KERNEL
2232 if (chip->controller_microcode != &snd_ymfpci_controller_microcode &&
2233 chip->controller_microcode != &snd_ymfpci_controller_1e_microcode)
2234#endif
2235 release_firmware(chip->controller_microcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 kfree(chip);
2237 return 0;
2238}
2239
Takashi Iwai208a1b42005-11-17 14:53:41 +01002240static int snd_ymfpci_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002242 struct snd_ymfpci *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return snd_ymfpci_free(chip);
2244}
2245
2246#ifdef CONFIG_PM
2247static int saved_regs_index[] = {
2248 /* spdif */
2249 YDSXGR_SPDIFOUTCTRL,
2250 YDSXGR_SPDIFOUTSTATUS,
2251 YDSXGR_SPDIFINCTRL,
2252 /* volumes */
2253 YDSXGR_PRIADCLOOPVOL,
2254 YDSXGR_NATIVEDACINVOL,
2255 YDSXGR_NATIVEDACOUTVOL,
2256 // YDSXGR_BUF441OUTVOL,
2257 YDSXGR_NATIVEADCINVOL,
2258 YDSXGR_SPDIFLOOPVOL,
2259 YDSXGR_SPDIFOUTVOL,
2260 YDSXGR_ZVOUTVOL,
2261 YDSXGR_LEGACYOUTVOL,
2262 /* address bases */
2263 YDSXGR_PLAYCTRLBASE,
2264 YDSXGR_RECCTRLBASE,
2265 YDSXGR_EFFCTRLBASE,
2266 YDSXGR_WORKBASE,
2267 /* capture set up */
2268 YDSXGR_MAPOFREC,
2269 YDSXGR_RECFORMAT,
2270 YDSXGR_RECSLOTSR,
2271 YDSXGR_ADCFORMAT,
2272 YDSXGR_ADCSLOTSR,
2273};
2274#define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
2275
Takashi Iwaided46232005-11-17 16:09:43 +01002276int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
Takashi Iwaided46232005-11-17 16:09:43 +01002278 struct snd_card *card = pci_get_drvdata(pci);
2279 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 unsigned int i;
2281
Takashi Iwaided46232005-11-17 16:09:43 +01002282 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 snd_pcm_suspend_all(chip->pcm);
2284 snd_pcm_suspend_all(chip->pcm2);
2285 snd_pcm_suspend_all(chip->pcm_spdif);
2286 snd_pcm_suspend_all(chip->pcm_4ch);
2287 snd_ac97_suspend(chip->ac97);
2288 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2289 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2290 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2291 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2292 snd_ymfpci_disable_dsp(chip);
Takashi Iwaided46232005-11-17 16:09:43 +01002293 pci_disable_device(pci);
2294 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002295 pci_set_power_state(pci, pci_choose_state(pci, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return 0;
2297}
2298
Takashi Iwaided46232005-11-17 16:09:43 +01002299int snd_ymfpci_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Takashi Iwaided46232005-11-17 16:09:43 +01002301 struct snd_card *card = pci_get_drvdata(pci);
2302 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 unsigned int i;
2304
Takashi Iwai30b35392006-10-11 18:52:53 +02002305 pci_set_power_state(pci, PCI_D0);
Takashi Iwaided46232005-11-17 16:09:43 +01002306 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002307 if (pci_enable_device(pci) < 0) {
2308 printk(KERN_ERR "ymfpci: pci_enable_device failed, "
2309 "disabling device\n");
2310 snd_card_disconnect(card);
2311 return -EIO;
2312 }
Takashi Iwaided46232005-11-17 16:09:43 +01002313 pci_set_master(pci);
2314 snd_ymfpci_aclink_reset(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 snd_ymfpci_codec_ready(chip, 0);
2316 snd_ymfpci_download_image(chip);
2317 udelay(100);
2318
2319 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2320 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2321
2322 snd_ac97_resume(chip->ac97);
2323
2324 /* start hw again */
2325 if (chip->start_count > 0) {
2326 spin_lock_irq(&chip->reg_lock);
2327 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2328 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2329 spin_unlock_irq(&chip->reg_lock);
2330 }
Takashi Iwaided46232005-11-17 16:09:43 +01002331 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return 0;
2333}
2334#endif /* CONFIG_PM */
2335
Takashi Iwai208a1b42005-11-17 14:53:41 +01002336int __devinit snd_ymfpci_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 struct pci_dev * pci,
2338 unsigned short old_legacy_ctrl,
Takashi Iwai208a1b42005-11-17 14:53:41 +01002339 struct snd_ymfpci ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002341 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002343 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 .dev_free = snd_ymfpci_dev_free,
2345 };
2346
2347 *rchip = NULL;
2348
2349 /* enable PCI device */
2350 if ((err = pci_enable_device(pci)) < 0)
2351 return err;
2352
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002353 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (chip == NULL) {
2355 pci_disable_device(pci);
2356 return -ENOMEM;
2357 }
2358 chip->old_legacy_ctrl = old_legacy_ctrl;
2359 spin_lock_init(&chip->reg_lock);
2360 spin_lock_init(&chip->voice_lock);
2361 init_waitqueue_head(&chip->interrupt_sleep);
2362 atomic_set(&chip->interrupt_sleep_count, 0);
2363 chip->card = card;
2364 chip->pci = pci;
2365 chip->irq = -1;
2366 chip->device_id = pci->device;
Takashi Iwai01f681d2006-11-16 15:39:07 +01002367 pci_read_config_byte(pci, PCI_REVISION_ID, &chip->rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 chip->reg_area_phys = pci_resource_start(pci, 0);
2369 chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2370 pci_set_master(pci);
2371
2372 if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002373 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 -07002374 snd_ymfpci_free(chip);
2375 return -EBUSY;
2376 }
Takashi Iwai437a5a42006-11-21 12:14:23 +01002377 if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2378 "YMFPCI", chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002379 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 snd_ymfpci_free(chip);
2381 return -EBUSY;
2382 }
2383 chip->irq = pci->irq;
2384
2385 snd_ymfpci_aclink_reset(pci);
2386 if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2387 snd_ymfpci_free(chip);
2388 return -EIO;
2389 }
2390
Clemens Ladisch102fa902006-10-11 12:05:59 +02002391 err = snd_ymfpci_request_firmware(chip);
2392 if (err < 0) {
2393 snd_printk(KERN_ERR "firmware request failed: %d\n", err);
2394 snd_ymfpci_free(chip);
2395 return err;
2396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 snd_ymfpci_download_image(chip);
2398
2399 udelay(100); /* seems we need a delay after downloading image.. */
2400
2401 if (snd_ymfpci_memalloc(chip) < 0) {
2402 snd_ymfpci_free(chip);
2403 return -EIO;
2404 }
2405
2406 if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
2407 snd_ymfpci_free(chip);
2408 return err;
2409 }
2410
2411#ifdef CONFIG_PM
2412 chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
2413 if (chip->saved_regs == NULL) {
2414 snd_ymfpci_free(chip);
2415 return -ENOMEM;
2416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417#endif
2418
2419 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2420 snd_ymfpci_free(chip);
2421 return err;
2422 }
2423
2424 snd_ymfpci_proc_init(card, chip);
2425
2426 snd_card_set_dev(card, &pci->dev);
2427
2428 *rchip = chip;
2429 return 0;
2430}