blob: 16be1f39548477f83a411f2a64161ea9c5b6b1fd [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 *
5 * BUGS:
6 * --
7 *
8 * TODO:
9 * --
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#include <sound/driver.h>
28#include <linux/delay.h>
29#include <linux/init.h>
30#include <linux/interrupt.h>
31#include <linux/pci.h>
32#include <linux/sched.h>
33#include <linux/slab.h>
34#include <linux/vmalloc.h>
35
36#include <sound/core.h>
37#include <sound/control.h>
38#include <sound/info.h>
Takashi Iwai33925182006-08-28 13:29:42 +020039#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <sound/ymfpci.h>
41#include <sound/asoundef.h>
42#include <sound/mpu401.h>
43
44#include <asm/io.h>
45
46/*
47 * constants
48 */
49
50/*
51 * common I/O routines
52 */
53
Takashi Iwai208a1b42005-11-17 14:53:41 +010054static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Takashi Iwai208a1b42005-11-17 14:53:41 +010056static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 return readb(chip->reg_area_virt + offset);
59}
60
Takashi Iwai208a1b42005-11-17 14:53:41 +010061static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 writeb(val, chip->reg_area_virt + offset);
64}
65
Takashi Iwai208a1b42005-11-17 14:53:41 +010066static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 return readw(chip->reg_area_virt + offset);
69}
70
Takashi Iwai208a1b42005-11-17 14:53:41 +010071static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 writew(val, chip->reg_area_virt + offset);
74}
75
Takashi Iwai208a1b42005-11-17 14:53:41 +010076static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 return readl(chip->reg_area_virt + offset);
79}
80
Takashi Iwai208a1b42005-11-17 14:53:41 +010081static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 writel(val, chip->reg_area_virt + offset);
84}
85
Takashi Iwai208a1b42005-11-17 14:53:41 +010086static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020088 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
90
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020091 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 do {
93 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
94 return 0;
95 set_current_state(TASK_UNINTERRUPTIBLE);
Nishanth Aravamudan8433a502005-10-24 15:02:37 +020096 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020097 } while (time_before(jiffies, end_time));
Takashi Iwai99b359b2005-10-20 18:26:44 +020098 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 -070099 return -EBUSY;
100}
101
Takashi Iwai208a1b42005-11-17 14:53:41 +0100102static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100104 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 u32 cmd;
106
107 snd_ymfpci_codec_ready(chip, 0);
108 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
109 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
110}
111
Takashi Iwai208a1b42005-11-17 14:53:41 +0100112static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100114 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (snd_ymfpci_codec_ready(chip, 0))
117 return ~0;
118 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
119 if (snd_ymfpci_codec_ready(chip, 0))
120 return ~0;
121 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
122 int i;
123 for (i = 0; i < 600; i++)
124 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
125 }
126 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
127}
128
129/*
130 * Misc routines
131 */
132
133static u32 snd_ymfpci_calc_delta(u32 rate)
134{
135 switch (rate) {
136 case 8000: return 0x02aaab00;
137 case 11025: return 0x03accd00;
138 case 16000: return 0x05555500;
139 case 22050: return 0x07599a00;
140 case 32000: return 0x0aaaab00;
141 case 44100: return 0x0eb33300;
142 default: return ((rate << 16) / 375) << 5;
143 }
144}
145
146static u32 def_rate[8] = {
147 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
148};
149
150static u32 snd_ymfpci_calc_lpfK(u32 rate)
151{
152 u32 i;
153 static u32 val[8] = {
154 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
155 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
156 };
157
158 if (rate == 44100)
159 return 0x40000000; /* FIXME: What's the right value? */
160 for (i = 0; i < 8; i++)
161 if (rate <= def_rate[i])
162 return val[i];
163 return val[0];
164}
165
166static u32 snd_ymfpci_calc_lpfQ(u32 rate)
167{
168 u32 i;
169 static u32 val[8] = {
170 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
171 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
172 };
173
174 if (rate == 44100)
175 return 0x370A0000;
176 for (i = 0; i < 8; i++)
177 if (rate <= def_rate[i])
178 return val[i];
179 return val[0];
180}
181
182/*
183 * Hardware start management
184 */
185
Takashi Iwai208a1b42005-11-17 14:53:41 +0100186static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 unsigned long flags;
189
190 spin_lock_irqsave(&chip->reg_lock, flags);
191 if (chip->start_count++ > 0)
192 goto __end;
193 snd_ymfpci_writel(chip, YDSXGR_MODE,
194 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
195 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
196 __end:
197 spin_unlock_irqrestore(&chip->reg_lock, flags);
198}
199
Takashi Iwai208a1b42005-11-17 14:53:41 +0100200static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 unsigned long flags;
203 long timeout = 1000;
204
205 spin_lock_irqsave(&chip->reg_lock, flags);
206 if (--chip->start_count > 0)
207 goto __end;
208 snd_ymfpci_writel(chip, YDSXGR_MODE,
209 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
210 while (timeout-- > 0) {
211 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
212 break;
213 }
214 if (atomic_read(&chip->interrupt_sleep_count)) {
215 atomic_set(&chip->interrupt_sleep_count, 0);
216 wake_up(&chip->interrupt_sleep);
217 }
218 __end:
219 spin_unlock_irqrestore(&chip->reg_lock, flags);
220}
221
222/*
223 * Playback voice management
224 */
225
Takashi Iwai208a1b42005-11-17 14:53:41 +0100226static int voice_alloc(struct snd_ymfpci *chip,
227 enum snd_ymfpci_voice_type type, int pair,
228 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100230 struct snd_ymfpci_voice *voice, *voice2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int idx;
232
233 *rvoice = NULL;
234 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
235 voice = &chip->voices[idx];
236 voice2 = pair ? &chip->voices[idx+1] : NULL;
237 if (voice->use || (voice2 && voice2->use))
238 continue;
239 voice->use = 1;
240 if (voice2)
241 voice2->use = 1;
242 switch (type) {
243 case YMFPCI_PCM:
244 voice->pcm = 1;
245 if (voice2)
246 voice2->pcm = 1;
247 break;
248 case YMFPCI_SYNTH:
249 voice->synth = 1;
250 break;
251 case YMFPCI_MIDI:
252 voice->midi = 1;
253 break;
254 }
255 snd_ymfpci_hw_start(chip);
256 if (voice2)
257 snd_ymfpci_hw_start(chip);
258 *rvoice = voice;
259 return 0;
260 }
261 return -ENOMEM;
262}
263
Takashi Iwai208a1b42005-11-17 14:53:41 +0100264static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
265 enum snd_ymfpci_voice_type type, int pair,
266 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 unsigned long flags;
269 int result;
270
271 snd_assert(rvoice != NULL, return -EINVAL);
272 snd_assert(!pair || type == YMFPCI_PCM, return -EINVAL);
273
274 spin_lock_irqsave(&chip->voice_lock, flags);
275 for (;;) {
276 result = voice_alloc(chip, type, pair, rvoice);
277 if (result == 0 || type != YMFPCI_PCM)
278 break;
279 /* TODO: synth/midi voice deallocation */
280 break;
281 }
282 spin_unlock_irqrestore(&chip->voice_lock, flags);
283 return result;
284}
285
Takashi Iwai208a1b42005-11-17 14:53:41 +0100286static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 unsigned long flags;
289
290 snd_assert(pvoice != NULL, return -EINVAL);
291 snd_ymfpci_hw_stop(chip);
292 spin_lock_irqsave(&chip->voice_lock, flags);
293 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
294 pvoice->ypcm = NULL;
295 pvoice->interrupt = NULL;
296 spin_unlock_irqrestore(&chip->voice_lock, flags);
297 return 0;
298}
299
300/*
301 * PCM part
302 */
303
Takashi Iwai208a1b42005-11-17 14:53:41 +0100304static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100306 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 u32 pos, delta;
308
309 if ((ypcm = voice->ypcm) == NULL)
310 return;
311 if (ypcm->substream == NULL)
312 return;
313 spin_lock(&chip->reg_lock);
314 if (ypcm->running) {
315 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
316 if (pos < ypcm->last_pos)
317 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
318 else
319 delta = pos - ypcm->last_pos;
320 ypcm->period_pos += delta;
321 ypcm->last_pos = pos;
322 if (ypcm->period_pos >= ypcm->period_size) {
323 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
324 ypcm->period_pos %= ypcm->period_size;
325 spin_unlock(&chip->reg_lock);
326 snd_pcm_period_elapsed(ypcm->substream);
327 spin_lock(&chip->reg_lock);
328 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200329
330 if (unlikely(ypcm->update_pcm_vol)) {
331 unsigned int subs = ypcm->substream->number;
332 unsigned int next_bank = 1 - chip->active_bank;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100333 struct snd_ymfpci_playback_bank *bank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200334 u32 volume;
335
336 bank = &voice->bank[next_bank];
337 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
338 bank->left_gain_end = volume;
339 if (ypcm->output_rear)
340 bank->eff2_gain_end = volume;
341 if (ypcm->voices[1])
342 bank = &ypcm->voices[1]->bank[next_bank];
343 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
344 bank->right_gain_end = volume;
345 if (ypcm->output_rear)
346 bank->eff3_gain_end = volume;
347 ypcm->update_pcm_vol--;
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 spin_unlock(&chip->reg_lock);
351}
352
Takashi Iwai208a1b42005-11-17 14:53:41 +0100353static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100355 struct snd_pcm_runtime *runtime = substream->runtime;
356 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
357 struct snd_ymfpci *chip = ypcm->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 u32 pos, delta;
359
360 spin_lock(&chip->reg_lock);
361 if (ypcm->running) {
362 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
363 if (pos < ypcm->last_pos)
364 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
365 else
366 delta = pos - ypcm->last_pos;
367 ypcm->period_pos += delta;
368 ypcm->last_pos = pos;
369 if (ypcm->period_pos >= ypcm->period_size) {
370 ypcm->period_pos %= ypcm->period_size;
371 // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
372 spin_unlock(&chip->reg_lock);
373 snd_pcm_period_elapsed(substream);
374 spin_lock(&chip->reg_lock);
375 }
376 }
377 spin_unlock(&chip->reg_lock);
378}
379
Takashi Iwai208a1b42005-11-17 14:53:41 +0100380static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int cmd)
382{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100383 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
384 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 int result = 0;
386
387 spin_lock(&chip->reg_lock);
388 if (ypcm->voices[0] == NULL) {
389 result = -EINVAL;
390 goto __unlock;
391 }
392 switch (cmd) {
393 case SNDRV_PCM_TRIGGER_START:
394 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
395 case SNDRV_PCM_TRIGGER_RESUME:
396 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
397 if (ypcm->voices[1] != NULL)
398 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
399 ypcm->running = 1;
400 break;
401 case SNDRV_PCM_TRIGGER_STOP:
402 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
403 case SNDRV_PCM_TRIGGER_SUSPEND:
404 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
405 if (ypcm->voices[1] != NULL)
406 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
407 ypcm->running = 0;
408 break;
409 default:
410 result = -EINVAL;
411 break;
412 }
413 __unlock:
414 spin_unlock(&chip->reg_lock);
415 return result;
416}
Takashi Iwai208a1b42005-11-17 14:53:41 +0100417static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 int cmd)
419{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100420 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
421 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 int result = 0;
423 u32 tmp;
424
425 spin_lock(&chip->reg_lock);
426 switch (cmd) {
427 case SNDRV_PCM_TRIGGER_START:
428 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
429 case SNDRV_PCM_TRIGGER_RESUME:
430 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
431 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
432 ypcm->running = 1;
433 break;
434 case SNDRV_PCM_TRIGGER_STOP:
435 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
436 case SNDRV_PCM_TRIGGER_SUSPEND:
437 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
438 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
439 ypcm->running = 0;
440 break;
441 default:
442 result = -EINVAL;
443 break;
444 }
445 spin_unlock(&chip->reg_lock);
446 return result;
447}
448
Takashi Iwai208a1b42005-11-17 14:53:41 +0100449static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 int err;
452
453 if (ypcm->voices[1] != NULL && voices < 2) {
454 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
455 ypcm->voices[1] = NULL;
456 }
457 if (voices == 1 && ypcm->voices[0] != NULL)
458 return 0; /* already allocated */
459 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
460 return 0; /* already allocated */
461 if (voices > 1) {
462 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
463 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
464 ypcm->voices[0] = NULL;
465 }
466 }
467 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
468 if (err < 0)
469 return err;
470 ypcm->voices[0]->ypcm = ypcm;
471 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
472 if (voices > 1) {
473 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
474 ypcm->voices[1]->ypcm = ypcm;
475 }
476 return 0;
477}
478
Takashi Iwai208a1b42005-11-17 14:53:41 +0100479static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
480 struct snd_pcm_runtime *runtime,
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200481 int has_pcm_volume)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100483 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 u32 format;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200485 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
486 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
487 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
Takashi Iwai208a1b42005-11-17 14:53:41 +0100488 struct snd_ymfpci_playback_bank *bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 unsigned int nbank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200490 u32 vol_left, vol_right;
491 u8 use_left, use_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 snd_assert(voice != NULL, return);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200494 if (runtime->channels == 1) {
495 use_left = 1;
496 use_right = 1;
497 } else {
498 use_left = (voiceidx & 1) == 0;
499 use_right = !use_left;
500 }
501 if (has_pcm_volume) {
502 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
503 [ypcm->substream->number].left << 15);
504 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
505 [ypcm->substream->number].right << 15);
506 } else {
507 vol_left = cpu_to_le32(0x40000000);
508 vol_right = cpu_to_le32(0x40000000);
509 }
510 format = runtime->channels == 2 ? 0x00010000 : 0;
511 if (snd_pcm_format_width(runtime->format) == 8)
512 format |= 0x80000000;
513 if (runtime->channels == 2 && (voiceidx & 1) != 0)
514 format |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 for (nbank = 0; nbank < 2; nbank++) {
516 bank = &voice->bank[nbank];
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200517 memset(bank, 0, sizeof(*bank));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 bank->format = cpu_to_le32(format);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200519 bank->base = cpu_to_le32(runtime->dma_addr);
520 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 bank->lpfQ = cpu_to_le32(lpfQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 bank->delta =
523 bank->delta_end = cpu_to_le32(delta);
524 bank->lpfK =
525 bank->lpfK_end = cpu_to_le32(lpfK);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200526 bank->eg_gain =
527 bank->eg_gain_end = cpu_to_le32(0x40000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200529 if (ypcm->output_front) {
530 if (use_left) {
531 bank->left_gain =
532 bank->left_gain_end = vol_left;
533 }
534 if (use_right) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 bank->right_gain =
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200536 bank->right_gain_end = vol_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200538 }
539 if (ypcm->output_rear) {
Jaroslav Kysela5a25c5c2006-01-18 08:02:24 +0100540 if (!ypcm->swap_rear) {
541 if (use_left) {
542 bank->eff2_gain =
543 bank->eff2_gain_end = vol_left;
544 }
545 if (use_right) {
546 bank->eff3_gain =
547 bank->eff3_gain_end = vol_right;
548 }
549 } else {
550 /* The SPDIF out channels seem to be swapped, so we have
551 * to swap them here, too. The rear analog out channels
552 * will be wrong, but otherwise AC3 would not work.
553 */
554 if (use_left) {
555 bank->eff3_gain =
556 bank->eff3_gain_end = vol_left;
557 }
558 if (use_right) {
559 bank->eff2_gain =
560 bank->eff2_gain_end = vol_right;
561 }
562 }
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565}
566
Takashi Iwai208a1b42005-11-17 14:53:41 +0100567static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
570 4096, &chip->ac3_tmp_base) < 0)
571 return -ENOMEM;
572
573 chip->bank_effect[3][0]->base =
574 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
575 chip->bank_effect[3][0]->loop_end =
576 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
577 chip->bank_effect[4][0]->base =
578 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
579 chip->bank_effect[4][0]->loop_end =
580 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
581
582 spin_lock_irq(&chip->reg_lock);
583 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
584 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
585 spin_unlock_irq(&chip->reg_lock);
586 return 0;
587}
588
Takashi Iwai208a1b42005-11-17 14:53:41 +0100589static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 spin_lock_irq(&chip->reg_lock);
592 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
593 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
594 spin_unlock_irq(&chip->reg_lock);
595 // snd_ymfpci_irq_wait(chip);
596 if (chip->ac3_tmp_base.area) {
597 snd_dma_free_pages(&chip->ac3_tmp_base);
598 chip->ac3_tmp_base.area = NULL;
599 }
600 return 0;
601}
602
Takashi Iwai208a1b42005-11-17 14:53:41 +0100603static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
604 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100606 struct snd_pcm_runtime *runtime = substream->runtime;
607 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int err;
609
610 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
611 return err;
612 if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
613 return err;
614 return 0;
615}
616
Takashi Iwai208a1b42005-11-17 14:53:41 +0100617static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100619 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
620 struct snd_pcm_runtime *runtime = substream->runtime;
621 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (runtime->private_data == NULL)
624 return 0;
625 ypcm = runtime->private_data;
626
627 /* wait, until the PCI operations are not finished */
628 snd_ymfpci_irq_wait(chip);
629 snd_pcm_lib_free_pages(substream);
630 if (ypcm->voices[1]) {
631 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
632 ypcm->voices[1] = NULL;
633 }
634 if (ypcm->voices[0]) {
635 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
636 ypcm->voices[0] = NULL;
637 }
638 return 0;
639}
640
Takashi Iwai208a1b42005-11-17 14:53:41 +0100641static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100643 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
644 struct snd_pcm_runtime *runtime = substream->runtime;
645 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 unsigned int nvoice;
647
648 ypcm->period_size = runtime->period_size;
649 ypcm->buffer_size = runtime->buffer_size;
650 ypcm->period_pos = 0;
651 ypcm->last_pos = 0;
652 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200653 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
654 substream->pcm == chip->pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 0;
656}
657
Takashi Iwai208a1b42005-11-17 14:53:41 +0100658static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
659 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
662}
663
Takashi Iwai208a1b42005-11-17 14:53:41 +0100664static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100666 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /* wait, until the PCI operations are not finished */
669 snd_ymfpci_irq_wait(chip);
670 return snd_pcm_lib_free_pages(substream);
671}
672
Takashi Iwai208a1b42005-11-17 14:53:41 +0100673static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100675 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
676 struct snd_pcm_runtime *runtime = substream->runtime;
677 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
678 struct snd_ymfpci_capture_bank * bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int nbank;
680 u32 rate, format;
681
682 ypcm->period_size = runtime->period_size;
683 ypcm->buffer_size = runtime->buffer_size;
684 ypcm->period_pos = 0;
685 ypcm->last_pos = 0;
686 ypcm->shift = 0;
687 rate = ((48000 * 4096) / runtime->rate) - 1;
688 format = 0;
689 if (runtime->channels == 2) {
690 format |= 2;
691 ypcm->shift++;
692 }
693 if (snd_pcm_format_width(runtime->format) == 8)
694 format |= 1;
695 else
696 ypcm->shift++;
697 switch (ypcm->capture_bank_number) {
698 case 0:
699 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
700 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
701 break;
702 case 1:
703 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
704 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
705 break;
706 }
707 for (nbank = 0; nbank < 2; nbank++) {
708 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
709 bank->base = cpu_to_le32(runtime->dma_addr);
710 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
711 bank->start = 0;
712 bank->num_of_loops = 0;
713 }
714 return 0;
715}
716
Takashi Iwai208a1b42005-11-17 14:53:41 +0100717static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100719 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
720 struct snd_pcm_runtime *runtime = substream->runtime;
721 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
722 struct snd_ymfpci_voice *voice = ypcm->voices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if (!(ypcm->running && voice))
725 return 0;
726 return le32_to_cpu(voice->bank[chip->active_bank].start);
727}
728
Takashi Iwai208a1b42005-11-17 14:53:41 +0100729static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100731 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
732 struct snd_pcm_runtime *runtime = substream->runtime;
733 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 if (!ypcm->running)
736 return 0;
737 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
738}
739
Takashi Iwai208a1b42005-11-17 14:53:41 +0100740static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 wait_queue_t wait;
743 int loops = 4;
744
745 while (loops-- > 0) {
746 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
747 continue;
748 init_waitqueue_entry(&wait, current);
749 add_wait_queue(&chip->interrupt_sleep, &wait);
750 atomic_inc(&chip->interrupt_sleep_count);
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200751 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 remove_wait_queue(&chip->interrupt_sleep, &wait);
753 }
754}
755
David Howells7d12e782006-10-05 14:55:46 +0100756static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100758 struct snd_ymfpci *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 u32 status, nvoice, mode;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100760 struct snd_ymfpci_voice *voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
763 if (status & 0x80000000) {
764 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
765 spin_lock(&chip->voice_lock);
766 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
767 voice = &chip->voices[nvoice];
768 if (voice->interrupt)
769 voice->interrupt(chip, voice);
770 }
771 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
772 if (chip->capture_substream[nvoice])
773 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
774 }
775#if 0
776 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
777 if (chip->effect_substream[nvoice])
778 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
779 }
780#endif
781 spin_unlock(&chip->voice_lock);
782 spin_lock(&chip->reg_lock);
783 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
784 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
785 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
786 spin_unlock(&chip->reg_lock);
787
788 if (atomic_read(&chip->interrupt_sleep_count)) {
789 atomic_set(&chip->interrupt_sleep_count, 0);
790 wake_up(&chip->interrupt_sleep);
791 }
792 }
793
794 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
795 if (status & 1) {
796 if (chip->timer)
797 snd_timer_interrupt(chip->timer, chip->timer->sticks);
798 }
799 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
800
801 if (chip->rawmidi)
David Howells7d12e782006-10-05 14:55:46 +0100802 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return IRQ_HANDLED;
804}
805
Takashi Iwai208a1b42005-11-17 14:53:41 +0100806static struct snd_pcm_hardware snd_ymfpci_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 .info = (SNDRV_PCM_INFO_MMAP |
809 SNDRV_PCM_INFO_MMAP_VALID |
810 SNDRV_PCM_INFO_INTERLEAVED |
811 SNDRV_PCM_INFO_BLOCK_TRANSFER |
812 SNDRV_PCM_INFO_PAUSE |
813 SNDRV_PCM_INFO_RESUME),
814 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
815 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
816 .rate_min = 8000,
817 .rate_max = 48000,
818 .channels_min = 1,
819 .channels_max = 2,
820 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
821 .period_bytes_min = 64,
822 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
823 .periods_min = 3,
824 .periods_max = 1024,
825 .fifo_size = 0,
826};
827
Takashi Iwai208a1b42005-11-17 14:53:41 +0100828static struct snd_pcm_hardware snd_ymfpci_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 .info = (SNDRV_PCM_INFO_MMAP |
831 SNDRV_PCM_INFO_MMAP_VALID |
832 SNDRV_PCM_INFO_INTERLEAVED |
833 SNDRV_PCM_INFO_BLOCK_TRANSFER |
834 SNDRV_PCM_INFO_PAUSE |
835 SNDRV_PCM_INFO_RESUME),
836 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
837 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
838 .rate_min = 8000,
839 .rate_max = 48000,
840 .channels_min = 1,
841 .channels_max = 2,
842 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
843 .period_bytes_min = 64,
844 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
845 .periods_min = 3,
846 .periods_max = 1024,
847 .fifo_size = 0,
848};
849
Takashi Iwai208a1b42005-11-17 14:53:41 +0100850static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Jesper Juhl4d572772005-05-30 17:30:32 +0200852 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
Takashi Iwai208a1b42005-11-17 14:53:41 +0100855static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100857 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
858 struct snd_pcm_runtime *runtime = substream->runtime;
859 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200861 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (ypcm == NULL)
863 return -ENOMEM;
864 ypcm->chip = chip;
865 ypcm->type = PLAYBACK_VOICE;
866 ypcm->substream = substream;
867 runtime->hw = snd_ymfpci_playback;
868 runtime->private_data = ypcm;
869 runtime->private_free = snd_ymfpci_pcm_free_substream;
870 /* FIXME? True value is 256/48 = 5.33333 ms */
871 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
872 return 0;
873}
874
875/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100876static void ymfpci_open_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 if (! chip->rear_opened) {
879 if (! chip->spdif_opened) /* set AC3 */
880 snd_ymfpci_writel(chip, YDSXGR_MODE,
881 snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
882 /* enable second codec (4CHEN) */
883 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
884 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
885 }
886}
887
888/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100889static void ymfpci_close_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 if (! chip->rear_opened) {
892 if (! chip->spdif_opened)
893 snd_ymfpci_writel(chip, YDSXGR_MODE,
894 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
895 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
896 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
897 }
898}
899
Takashi Iwai208a1b42005-11-17 14:53:41 +0100900static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100902 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
903 struct snd_pcm_runtime *runtime = substream->runtime;
904 struct snd_ymfpci_pcm *ypcm;
905 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 int err;
907
908 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
909 return err;
910 ypcm = runtime->private_data;
911 ypcm->output_front = 1;
912 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
Glen Masgaid9301262006-10-10 09:27:19 +0200913 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 spin_lock_irq(&chip->reg_lock);
915 if (ypcm->output_rear) {
916 ymfpci_open_extension(chip);
917 chip->rear_opened++;
918 }
919 spin_unlock_irq(&chip->reg_lock);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200920
921 kctl = chip->pcm_mixer[substream->number].ctl;
922 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
923 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return 0;
925}
926
Takashi Iwai208a1b42005-11-17 14:53:41 +0100927static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100929 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
930 struct snd_pcm_runtime *runtime = substream->runtime;
931 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int err;
933
934 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
935 return err;
936 ypcm = runtime->private_data;
937 ypcm->output_front = 0;
938 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200939 ypcm->swap_rear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 spin_lock_irq(&chip->reg_lock);
941 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
942 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
943 ymfpci_open_extension(chip);
944 chip->spdif_pcm_bits = chip->spdif_bits;
945 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
946 chip->spdif_opened++;
947 spin_unlock_irq(&chip->reg_lock);
948
949 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
950 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
951 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
952 return 0;
953}
954
Takashi Iwai208a1b42005-11-17 14:53:41 +0100955static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100957 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
958 struct snd_pcm_runtime *runtime = substream->runtime;
959 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int err;
961
962 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
963 return err;
964 ypcm = runtime->private_data;
965 ypcm->output_front = 0;
966 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200967 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 spin_lock_irq(&chip->reg_lock);
969 ymfpci_open_extension(chip);
970 chip->rear_opened++;
971 spin_unlock_irq(&chip->reg_lock);
972 return 0;
973}
974
Takashi Iwai208a1b42005-11-17 14:53:41 +0100975static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 u32 capture_bank_number)
977{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100978 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
979 struct snd_pcm_runtime *runtime = substream->runtime;
980 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200982 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (ypcm == NULL)
984 return -ENOMEM;
985 ypcm->chip = chip;
986 ypcm->type = capture_bank_number + CAPTURE_REC;
987 ypcm->substream = substream;
988 ypcm->capture_bank_number = capture_bank_number;
989 chip->capture_substream[capture_bank_number] = substream;
990 runtime->hw = snd_ymfpci_capture;
991 /* FIXME? True value is 256/48 = 5.33333 ms */
992 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
993 runtime->private_data = ypcm;
994 runtime->private_free = snd_ymfpci_pcm_free_substream;
995 snd_ymfpci_hw_start(chip);
996 return 0;
997}
998
Takashi Iwai208a1b42005-11-17 14:53:41 +0100999static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 return snd_ymfpci_capture_open(substream, 0);
1002}
1003
Takashi Iwai208a1b42005-11-17 14:53:41 +01001004static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 return snd_ymfpci_capture_open(substream, 1);
1007}
1008
Takashi Iwai208a1b42005-11-17 14:53:41 +01001009static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 return 0;
1012}
1013
Takashi Iwai208a1b42005-11-17 14:53:41 +01001014static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001016 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1017 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1018 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 spin_lock_irq(&chip->reg_lock);
1021 if (ypcm->output_rear && chip->rear_opened > 0) {
1022 chip->rear_opened--;
1023 ymfpci_close_extension(chip);
1024 }
1025 spin_unlock_irq(&chip->reg_lock);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001026 kctl = chip->pcm_mixer[substream->number].ctl;
1027 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1028 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return snd_ymfpci_playback_close_1(substream);
1030}
1031
Takashi Iwai208a1b42005-11-17 14:53:41 +01001032static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001034 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 spin_lock_irq(&chip->reg_lock);
1037 chip->spdif_opened = 0;
1038 ymfpci_close_extension(chip);
1039 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1040 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1041 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1042 spin_unlock_irq(&chip->reg_lock);
1043 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1044 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1045 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1046 return snd_ymfpci_playback_close_1(substream);
1047}
1048
Takashi Iwai208a1b42005-11-17 14:53:41 +01001049static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001051 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 spin_lock_irq(&chip->reg_lock);
1054 if (chip->rear_opened > 0) {
1055 chip->rear_opened--;
1056 ymfpci_close_extension(chip);
1057 }
1058 spin_unlock_irq(&chip->reg_lock);
1059 return snd_ymfpci_playback_close_1(substream);
1060}
1061
Takashi Iwai208a1b42005-11-17 14:53:41 +01001062static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001064 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1065 struct snd_pcm_runtime *runtime = substream->runtime;
1066 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 if (ypcm != NULL) {
1069 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1070 snd_ymfpci_hw_stop(chip);
1071 }
1072 return 0;
1073}
1074
Takashi Iwai208a1b42005-11-17 14:53:41 +01001075static struct snd_pcm_ops snd_ymfpci_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 .open = snd_ymfpci_playback_open,
1077 .close = snd_ymfpci_playback_close,
1078 .ioctl = snd_pcm_lib_ioctl,
1079 .hw_params = snd_ymfpci_playback_hw_params,
1080 .hw_free = snd_ymfpci_playback_hw_free,
1081 .prepare = snd_ymfpci_playback_prepare,
1082 .trigger = snd_ymfpci_playback_trigger,
1083 .pointer = snd_ymfpci_playback_pointer,
1084};
1085
Takashi Iwai208a1b42005-11-17 14:53:41 +01001086static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 .open = snd_ymfpci_capture_rec_open,
1088 .close = snd_ymfpci_capture_close,
1089 .ioctl = snd_pcm_lib_ioctl,
1090 .hw_params = snd_ymfpci_capture_hw_params,
1091 .hw_free = snd_ymfpci_capture_hw_free,
1092 .prepare = snd_ymfpci_capture_prepare,
1093 .trigger = snd_ymfpci_capture_trigger,
1094 .pointer = snd_ymfpci_capture_pointer,
1095};
1096
Takashi Iwai208a1b42005-11-17 14:53:41 +01001097int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001099 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 int err;
1101
1102 if (rpcm)
1103 *rpcm = NULL;
1104 if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1105 return err;
1106 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1109 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1110
1111 /* global setup */
1112 pcm->info_flags = 0;
1113 strcpy(pcm->name, "YMFPCI");
1114 chip->pcm = pcm;
1115
1116 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1117 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1118
1119 if (rpcm)
1120 *rpcm = pcm;
1121 return 0;
1122}
1123
Takashi Iwai208a1b42005-11-17 14:53:41 +01001124static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 .open = snd_ymfpci_capture_ac97_open,
1126 .close = snd_ymfpci_capture_close,
1127 .ioctl = snd_pcm_lib_ioctl,
1128 .hw_params = snd_ymfpci_capture_hw_params,
1129 .hw_free = snd_ymfpci_capture_hw_free,
1130 .prepare = snd_ymfpci_capture_prepare,
1131 .trigger = snd_ymfpci_capture_trigger,
1132 .pointer = snd_ymfpci_capture_pointer,
1133};
1134
Takashi Iwai208a1b42005-11-17 14:53:41 +01001135int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001137 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 int err;
1139
1140 if (rpcm)
1141 *rpcm = NULL;
1142 if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1143 return err;
1144 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1147
1148 /* global setup */
1149 pcm->info_flags = 0;
1150 sprintf(pcm->name, "YMFPCI - %s",
1151 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1152 chip->pcm2 = pcm;
1153
1154 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1155 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1156
1157 if (rpcm)
1158 *rpcm = pcm;
1159 return 0;
1160}
1161
Takashi Iwai208a1b42005-11-17 14:53:41 +01001162static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 .open = snd_ymfpci_playback_spdif_open,
1164 .close = snd_ymfpci_playback_spdif_close,
1165 .ioctl = snd_pcm_lib_ioctl,
1166 .hw_params = snd_ymfpci_playback_hw_params,
1167 .hw_free = snd_ymfpci_playback_hw_free,
1168 .prepare = snd_ymfpci_playback_prepare,
1169 .trigger = snd_ymfpci_playback_trigger,
1170 .pointer = snd_ymfpci_playback_pointer,
1171};
1172
Takashi Iwai208a1b42005-11-17 14:53:41 +01001173int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001175 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int err;
1177
1178 if (rpcm)
1179 *rpcm = NULL;
1180 if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1181 return err;
1182 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1185
1186 /* global setup */
1187 pcm->info_flags = 0;
1188 strcpy(pcm->name, "YMFPCI - IEC958");
1189 chip->pcm_spdif = pcm;
1190
1191 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1192 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1193
1194 if (rpcm)
1195 *rpcm = pcm;
1196 return 0;
1197}
1198
Takashi Iwai208a1b42005-11-17 14:53:41 +01001199static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 .open = snd_ymfpci_playback_4ch_open,
1201 .close = snd_ymfpci_playback_4ch_close,
1202 .ioctl = snd_pcm_lib_ioctl,
1203 .hw_params = snd_ymfpci_playback_hw_params,
1204 .hw_free = snd_ymfpci_playback_hw_free,
1205 .prepare = snd_ymfpci_playback_prepare,
1206 .trigger = snd_ymfpci_playback_trigger,
1207 .pointer = snd_ymfpci_playback_pointer,
1208};
1209
Takashi Iwai208a1b42005-11-17 14:53:41 +01001210int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001212 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 int err;
1214
1215 if (rpcm)
1216 *rpcm = NULL;
1217 if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1218 return err;
1219 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1222
1223 /* global setup */
1224 pcm->info_flags = 0;
1225 strcpy(pcm->name, "YMFPCI - Rear PCM");
1226 chip->pcm_4ch = pcm;
1227
1228 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1229 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1230
1231 if (rpcm)
1232 *rpcm = pcm;
1233 return 0;
1234}
1235
Takashi Iwai208a1b42005-11-17 14:53:41 +01001236static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1239 uinfo->count = 1;
1240 return 0;
1241}
1242
Takashi Iwai208a1b42005-11-17 14:53:41 +01001243static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1244 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001246 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 spin_lock_irq(&chip->reg_lock);
1249 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1250 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001251 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 spin_unlock_irq(&chip->reg_lock);
1253 return 0;
1254}
1255
Takashi Iwai208a1b42005-11-17 14:53:41 +01001256static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1257 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001259 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 unsigned int val;
1261 int change;
1262
1263 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1264 (ucontrol->value.iec958.status[1] << 8);
1265 spin_lock_irq(&chip->reg_lock);
1266 change = chip->spdif_bits != val;
1267 chip->spdif_bits = val;
1268 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1269 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1270 spin_unlock_irq(&chip->reg_lock);
1271 return change;
1272}
1273
Takashi Iwai208a1b42005-11-17 14:53:41 +01001274static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1277 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1278 .info = snd_ymfpci_spdif_default_info,
1279 .get = snd_ymfpci_spdif_default_get,
1280 .put = snd_ymfpci_spdif_default_put
1281};
1282
Takashi Iwai208a1b42005-11-17 14:53:41 +01001283static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1286 uinfo->count = 1;
1287 return 0;
1288}
1289
Takashi Iwai208a1b42005-11-17 14:53:41 +01001290static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1291 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001293 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 spin_lock_irq(&chip->reg_lock);
1296 ucontrol->value.iec958.status[0] = 0x3e;
1297 ucontrol->value.iec958.status[1] = 0xff;
1298 spin_unlock_irq(&chip->reg_lock);
1299 return 0;
1300}
1301
Takashi Iwai208a1b42005-11-17 14:53:41 +01001302static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1305 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1306 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1307 .info = snd_ymfpci_spdif_mask_info,
1308 .get = snd_ymfpci_spdif_mask_get,
1309};
1310
Takashi Iwai208a1b42005-11-17 14:53:41 +01001311static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1314 uinfo->count = 1;
1315 return 0;
1316}
1317
Takashi Iwai208a1b42005-11-17 14:53:41 +01001318static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1319 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001321 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 spin_lock_irq(&chip->reg_lock);
1324 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1325 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001326 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 spin_unlock_irq(&chip->reg_lock);
1328 return 0;
1329}
1330
Takashi Iwai208a1b42005-11-17 14:53:41 +01001331static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1332 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001334 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 unsigned int val;
1336 int change;
1337
1338 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1339 (ucontrol->value.iec958.status[1] << 8);
1340 spin_lock_irq(&chip->reg_lock);
1341 change = chip->spdif_pcm_bits != val;
1342 chip->spdif_pcm_bits = val;
1343 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1344 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1345 spin_unlock_irq(&chip->reg_lock);
1346 return change;
1347}
1348
Takashi Iwai208a1b42005-11-17 14:53:41 +01001349static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1352 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1353 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1354 .info = snd_ymfpci_spdif_stream_info,
1355 .get = snd_ymfpci_spdif_stream_get,
1356 .put = snd_ymfpci_spdif_stream_put
1357};
1358
Takashi Iwai208a1b42005-11-17 14:53:41 +01001359static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};
1362
1363 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1364 info->count = 1;
1365 info->value.enumerated.items = 3;
1366 if (info->value.enumerated.item > 2)
1367 info->value.enumerated.item = 2;
1368 strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
1369 return 0;
1370}
1371
Takashi Iwai208a1b42005-11-17 14:53:41 +01001372static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001374 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 u16 reg;
1376
1377 spin_lock_irq(&chip->reg_lock);
1378 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1379 spin_unlock_irq(&chip->reg_lock);
1380 if (!(reg & 0x100))
1381 value->value.enumerated.item[0] = 0;
1382 else
1383 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1384 return 0;
1385}
1386
Takashi Iwai208a1b42005-11-17 14:53:41 +01001387static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001389 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 u16 reg, old_reg;
1391
1392 spin_lock_irq(&chip->reg_lock);
1393 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1394 if (value->value.enumerated.item[0] == 0)
1395 reg = old_reg & ~0x100;
1396 else
1397 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1398 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1399 spin_unlock_irq(&chip->reg_lock);
1400 return reg != old_reg;
1401}
1402
Takashi Iwai208a1b42005-11-17 14:53:41 +01001403static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1405 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1406 .name = "Direct Recording Source",
1407 .info = snd_ymfpci_drec_source_info,
1408 .get = snd_ymfpci_drec_source_get,
1409 .put = snd_ymfpci_drec_source_put
1410};
1411
1412/*
1413 * Mixer controls
1414 */
1415
Glen Masgaid602c882005-09-28 08:19:05 +02001416#define YMFPCI_SINGLE(xname, xindex, reg, shift) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1418 .info = snd_ymfpci_info_single, \
1419 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
Glen Masgaid602c882005-09-28 08:19:05 +02001420 .private_value = ((reg) | ((shift) << 16)) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Takashi Iwai208a1b42005-11-17 14:53:41 +01001422static int snd_ymfpci_info_single(struct snd_kcontrol *kcontrol,
1423 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Glen Masgaid602c882005-09-28 08:19:05 +02001425 int reg = kcontrol->private_value & 0xffff;
1426
1427 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 case YDSXGR_SPDIFOUTCTRL: break;
1429 case YDSXGR_SPDIFINCTRL: break;
1430 default: return -EINVAL;
1431 }
Adrian Bunk467a8c22005-04-11 14:11:00 +02001432 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 uinfo->count = 1;
1434 uinfo->value.integer.min = 0;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001435 uinfo->value.integer.max = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return 0;
1437}
1438
Takashi Iwai208a1b42005-11-17 14:53:41 +01001439static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1440 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001442 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001443 int reg = kcontrol->private_value & 0xffff;
1444 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1445 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Glen Masgaid602c882005-09-28 08:19:05 +02001447 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 case YDSXGR_SPDIFOUTCTRL: break;
1449 case YDSXGR_SPDIFINCTRL: break;
1450 default: return -EINVAL;
1451 }
Glen Masgaid602c882005-09-28 08:19:05 +02001452 ucontrol->value.integer.value[0] =
1453 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return 0;
1455}
1456
Takashi Iwai208a1b42005-11-17 14:53:41 +01001457static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1458 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001460 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001461 int reg = kcontrol->private_value & 0xffff;
1462 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1463 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 int change;
1465 unsigned int val, oval;
1466
Glen Masgaid602c882005-09-28 08:19:05 +02001467 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 case YDSXGR_SPDIFOUTCTRL: break;
1469 case YDSXGR_SPDIFINCTRL: break;
1470 default: return -EINVAL;
1471 }
1472 val = (ucontrol->value.integer.value[0] & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 val <<= shift;
1474 spin_lock_irq(&chip->reg_lock);
1475 oval = snd_ymfpci_readl(chip, reg);
1476 val = (oval & ~(mask << shift)) | val;
1477 change = val != oval;
1478 snd_ymfpci_writel(chip, reg, val);
1479 spin_unlock_irq(&chip->reg_lock);
1480 return change;
1481}
1482
Takashi Iwai33925182006-08-28 13:29:42 +02001483static DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485#define YMFPCI_DOUBLE(xname, xindex, reg) \
1486{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Takashi Iwai33925182006-08-28 13:29:42 +02001487 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 .info = snd_ymfpci_info_double, \
1489 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
Takashi Iwai33925182006-08-28 13:29:42 +02001490 .private_value = reg, \
1491 .tlv = { .p = db_scale_native } }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Takashi Iwai208a1b42005-11-17 14:53:41 +01001493static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 unsigned int reg = kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 if (reg < 0x80 || reg >= 0xc0)
1498 return -EINVAL;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001499 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 uinfo->count = 2;
1501 uinfo->value.integer.min = 0;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001502 uinfo->value.integer.max = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return 0;
1504}
1505
Takashi Iwai208a1b42005-11-17 14:53:41 +01001506static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001508 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001510 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 unsigned int val;
1512
1513 if (reg < 0x80 || reg >= 0xc0)
1514 return -EINVAL;
1515 spin_lock_irq(&chip->reg_lock);
1516 val = snd_ymfpci_readl(chip, reg);
1517 spin_unlock_irq(&chip->reg_lock);
1518 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1519 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return 0;
1521}
1522
Takashi Iwai208a1b42005-11-17 14:53:41 +01001523static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001525 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001527 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 int change;
1529 unsigned int val1, val2, oval;
1530
1531 if (reg < 0x80 || reg >= 0xc0)
1532 return -EINVAL;
1533 val1 = ucontrol->value.integer.value[0] & mask;
1534 val2 = ucontrol->value.integer.value[1] & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 val1 <<= shift_left;
1536 val2 <<= shift_right;
1537 spin_lock_irq(&chip->reg_lock);
1538 oval = snd_ymfpci_readl(chip, reg);
1539 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1540 change = val1 != oval;
1541 snd_ymfpci_writel(chip, reg, val1);
1542 spin_unlock_irq(&chip->reg_lock);
1543 return change;
1544}
1545
1546/*
1547 * 4ch duplication
1548 */
Takashi Iwai208a1b42005-11-17 14:53:41 +01001549static int snd_ymfpci_info_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1552 uinfo->count = 1;
1553 uinfo->value.integer.min = 0;
1554 uinfo->value.integer.max = 1;
1555 return 0;
1556}
1557
Takashi Iwai208a1b42005-11-17 14:53:41 +01001558static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001560 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1562 return 0;
1563}
1564
Takashi Iwai208a1b42005-11-17 14:53:41 +01001565static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001567 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 int change;
1569 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1570 if (change)
1571 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1572 return change;
1573}
1574
1575
Takashi Iwai208a1b42005-11-17 14:53:41 +01001576static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577YMFPCI_DOUBLE("Wave Playback Volume", 0, YDSXGR_NATIVEDACOUTVOL),
1578YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1579YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1580YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1581YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1582YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1583YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1584YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1585YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
1586YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1587YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1588YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1589YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
Glen Masgaid602c882005-09-28 08:19:05 +02001590YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1591YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1592YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1595 .name = "4ch Duplication",
1596 .info = snd_ymfpci_info_dup4ch,
1597 .get = snd_ymfpci_get_dup4ch,
1598 .put = snd_ymfpci_put_dup4ch,
1599},
1600};
1601
1602
1603/*
1604 * GPIO
1605 */
1606
Takashi Iwai208a1b42005-11-17 14:53:41 +01001607static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 u16 reg, mode;
1610 unsigned long flags;
1611
1612 spin_lock_irqsave(&chip->reg_lock, flags);
1613 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1614 reg &= ~(1 << (pin + 8));
1615 reg |= (1 << pin);
1616 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1617 /* set the level mode for input line */
1618 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1619 mode &= ~(3 << (pin * 2));
1620 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1621 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1622 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1623 spin_unlock_irqrestore(&chip->reg_lock, flags);
1624 return (mode >> pin) & 1;
1625}
1626
Takashi Iwai208a1b42005-11-17 14:53:41 +01001627static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 u16 reg;
1630 unsigned long flags;
1631
1632 spin_lock_irqsave(&chip->reg_lock, flags);
1633 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1634 reg &= ~(1 << pin);
1635 reg &= ~(1 << (pin + 8));
1636 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1637 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1638 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1639 spin_unlock_irqrestore(&chip->reg_lock, flags);
1640
1641 return 0;
1642}
1643
Takashi Iwai208a1b42005-11-17 14:53:41 +01001644static int snd_ymfpci_gpio_sw_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1647 uinfo->count = 1;
1648 uinfo->value.integer.min = 0;
1649 uinfo->value.integer.max = 1;
1650 return 0;
1651}
1652
Takashi Iwai208a1b42005-11-17 14:53:41 +01001653static int snd_ymfpci_gpio_sw_get(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 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1658 return 0;
1659}
1660
Takashi Iwai208a1b42005-11-17 14:53:41 +01001661static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001663 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 int pin = (int)kcontrol->private_value;
1665
1666 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1667 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1668 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1669 return 1;
1670 }
1671 return 0;
1672}
1673
Takashi Iwai208a1b42005-11-17 14:53:41 +01001674static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 .name = "Shared Rear/Line-In Switch",
1676 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1677 .info = snd_ymfpci_gpio_sw_info,
1678 .get = snd_ymfpci_gpio_sw_get,
1679 .put = snd_ymfpci_gpio_sw_put,
1680 .private_value = 2,
1681};
1682
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001683/*
1684 * PCM voice volume
1685 */
1686
Takashi Iwai208a1b42005-11-17 14:53:41 +01001687static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1688 struct snd_ctl_elem_info *uinfo)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001689{
1690 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1691 uinfo->count = 2;
1692 uinfo->value.integer.min = 0;
1693 uinfo->value.integer.max = 0x8000;
1694 return 0;
1695}
1696
Takashi Iwai208a1b42005-11-17 14:53:41 +01001697static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1698 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001699{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001700 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001701 unsigned int subs = kcontrol->id.subdevice;
1702
1703 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1704 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1705 return 0;
1706}
1707
Takashi Iwai208a1b42005-11-17 14:53:41 +01001708static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1709 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001710{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001711 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001712 unsigned int subs = kcontrol->id.subdevice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001713 struct snd_pcm_substream *substream;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001714 unsigned long flags;
1715
1716 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1717 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1718 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1719 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1720
Takashi Iwai208a1b42005-11-17 14:53:41 +01001721 substream = (struct snd_pcm_substream *)kcontrol->private_value;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001722 spin_lock_irqsave(&chip->voice_lock, flags);
1723 if (substream->runtime && substream->runtime->private_data) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01001724 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001725 ypcm->update_pcm_vol = 2;
1726 }
1727 spin_unlock_irqrestore(&chip->voice_lock, flags);
1728 return 1;
1729 }
1730 return 0;
1731}
1732
Takashi Iwai208a1b42005-11-17 14:53:41 +01001733static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001734 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1735 .name = "PCM Playback Volume",
1736 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1737 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1738 .info = snd_ymfpci_pcm_vol_info,
1739 .get = snd_ymfpci_pcm_vol_get,
1740 .put = snd_ymfpci_pcm_vol_put,
1741};
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744/*
1745 * Mixer routines
1746 */
1747
Takashi Iwai208a1b42005-11-17 14:53:41 +01001748static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001750 struct snd_ymfpci *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 chip->ac97_bus = NULL;
1752}
1753
Takashi Iwai208a1b42005-11-17 14:53:41 +01001754static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001756 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 chip->ac97 = NULL;
1758}
1759
Glen Masgaid9301262006-10-10 09:27:19 +02001760int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001762 struct snd_ac97_template ac97;
1763 struct snd_kcontrol *kctl;
1764 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 unsigned int idx;
1766 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001767 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 .write = snd_ymfpci_codec_write,
1769 .read = snd_ymfpci_codec_read,
1770 };
1771
1772 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1773 return err;
1774 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1775 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1776
1777 memset(&ac97, 0, sizeof(ac97));
1778 ac97.private_data = chip;
1779 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1780 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1781 return err;
1782
1783 /* to be sure */
1784 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1785 AC97_EA_VRA|AC97_EA_VRM, 0);
1786
1787 for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1788 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1789 return err;
1790 }
1791
1792 /* add S/PDIF control */
1793 snd_assert(chip->pcm_spdif != NULL, return -EIO);
1794 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1795 return err;
1796 kctl->id.device = chip->pcm_spdif->device;
1797 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1798 return err;
1799 kctl->id.device = chip->pcm_spdif->device;
1800 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1801 return err;
1802 kctl->id.device = chip->pcm_spdif->device;
1803 chip->spdif_pcm_ctl = kctl;
1804
1805 /* direct recording source */
1806 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1807 (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1808 return err;
1809
1810 /*
1811 * shared rear/line-in
1812 */
1813 if (rear_switch) {
1814 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1815 return err;
1816 }
1817
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001818 /* per-voice volume */
1819 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1820 for (idx = 0; idx < 32; ++idx) {
1821 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1822 if (!kctl)
1823 return -ENOMEM;
1824 kctl->id.device = chip->pcm->device;
1825 kctl->id.subdevice = idx;
1826 kctl->private_value = (unsigned long)substream;
1827 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1828 return err;
1829 chip->pcm_mixer[idx].left = 0x8000;
1830 chip->pcm_mixer[idx].right = 0x8000;
1831 chip->pcm_mixer[idx].ctl = kctl;
1832 substream = substream->next;
1833 }
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return 0;
1836}
1837
1838
1839/*
1840 * timer
1841 */
1842
Takashi Iwai208a1b42005-11-17 14:53:41 +01001843static int snd_ymfpci_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001845 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 unsigned long flags;
1847 unsigned int count;
1848
1849 chip = snd_timer_chip(timer);
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001850 count = (timer->sticks << 1) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 spin_lock_irqsave(&chip->reg_lock, flags);
1852 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1853 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1854 spin_unlock_irqrestore(&chip->reg_lock, flags);
1855 return 0;
1856}
1857
Takashi Iwai208a1b42005-11-17 14:53:41 +01001858static int snd_ymfpci_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001860 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 unsigned long flags;
1862
1863 chip = snd_timer_chip(timer);
1864 spin_lock_irqsave(&chip->reg_lock, flags);
1865 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1866 spin_unlock_irqrestore(&chip->reg_lock, flags);
1867 return 0;
1868}
1869
Takashi Iwai208a1b42005-11-17 14:53:41 +01001870static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 unsigned long *num, unsigned long *den)
1872{
1873 *num = 1;
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001874 *den = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return 0;
1876}
1877
Takashi Iwai208a1b42005-11-17 14:53:41 +01001878static struct snd_timer_hardware snd_ymfpci_timer_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 .flags = SNDRV_TIMER_HW_AUTO,
Clemens Ladischd44c39a2005-10-19 14:39:48 +02001880 .resolution = 20833, /* 1/fs = 20.8333...us */
1881 .ticks = 0x8000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 .start = snd_ymfpci_timer_start,
1883 .stop = snd_ymfpci_timer_stop,
1884 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1885};
1886
Takashi Iwai208a1b42005-11-17 14:53:41 +01001887int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001889 struct snd_timer *timer = NULL;
1890 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 int err;
1892
1893 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1894 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1895 tid.card = chip->card->number;
1896 tid.device = device;
1897 tid.subdevice = 0;
1898 if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1899 strcpy(timer->name, "YMFPCI timer");
1900 timer->private_data = chip;
1901 timer->hw = snd_ymfpci_timer_hw;
1902 }
1903 chip->timer = timer;
1904 return err;
1905}
1906
1907
1908/*
1909 * proc interface
1910 */
1911
Takashi Iwai208a1b42005-11-17 14:53:41 +01001912static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1913 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001915 struct snd_ymfpci *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 int i;
1917
1918 snd_iprintf(buffer, "YMFPCI\n\n");
1919 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1920 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1921}
1922
Takashi Iwai208a1b42005-11-17 14:53:41 +01001923static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001925 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 if (! snd_card_proc_new(card, "ymfpci", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001928 snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return 0;
1930}
1931
1932/*
1933 * initialization routines
1934 */
1935
1936static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1937{
1938 u8 cmd;
1939
1940 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1941#if 0 // force to reset
1942 if (cmd & 0x03) {
1943#endif
1944 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1945 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1946 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1947 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1948 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1949#if 0
1950 }
1951#endif
1952}
1953
Takashi Iwai208a1b42005-11-17 14:53:41 +01001954static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
1956 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1957}
1958
Takashi Iwai208a1b42005-11-17 14:53:41 +01001959static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 u32 val;
1962 int timeout = 1000;
1963
1964 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1965 if (val)
1966 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
1967 while (timeout-- > 0) {
1968 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
1969 if ((val & 0x00000002) == 0)
1970 break;
1971 }
1972}
1973
1974#include "ymfpci_image.h"
1975
Takashi Iwai208a1b42005-11-17 14:53:41 +01001976static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 int i;
1979 u16 ctrl;
1980 unsigned long *inst;
1981
1982 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
1983 snd_ymfpci_disable_dsp(chip);
1984 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
1985 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
1986 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
1987 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
1988 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
1989 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
1990 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
1991 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1992 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
1993
1994 /* setup DSP instruction code */
1995 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
1996 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), DspInst[i]);
1997
1998 /* setup control instruction code */
1999 switch (chip->device_id) {
2000 case PCI_DEVICE_ID_YAMAHA_724F:
2001 case PCI_DEVICE_ID_YAMAHA_740C:
2002 case PCI_DEVICE_ID_YAMAHA_744:
2003 case PCI_DEVICE_ID_YAMAHA_754:
2004 inst = CntrlInst1E;
2005 break;
2006 default:
2007 inst = CntrlInst;
2008 break;
2009 }
2010 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2011 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), inst[i]);
2012
2013 snd_ymfpci_enable_dsp(chip);
2014}
2015
Takashi Iwai208a1b42005-11-17 14:53:41 +01002016static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 long size, playback_ctrl_size;
2019 int voice, bank, reg;
2020 u8 *ptr;
2021 dma_addr_t ptr_addr;
2022
2023 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2024 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2025 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2026 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2027 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2028
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002029 size = ALIGN(playback_ctrl_size, 0x100) +
2030 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2031 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2032 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 chip->work_size;
2034 /* work_ptr must be aligned to 256 bytes, but it's already
2035 covered with the kernel page allocation mechanism */
2036 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2037 size, &chip->work_ptr) < 0)
2038 return -ENOMEM;
2039 ptr = chip->work_ptr.area;
2040 ptr_addr = chip->work_ptr.addr;
2041 memset(ptr, 0, size); /* for sure */
2042
2043 chip->bank_base_playback = ptr;
2044 chip->bank_base_playback_addr = ptr_addr;
2045 chip->ctrl_playback = (u32 *)ptr;
2046 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002047 ptr += ALIGN(playback_ctrl_size, 0x100);
2048 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2050 chip->voices[voice].number = voice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002051 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 chip->voices[voice].bank_addr = ptr_addr;
2053 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002054 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 ptr += chip->bank_size_playback;
2056 ptr_addr += chip->bank_size_playback;
2057 }
2058 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002059 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2060 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 chip->bank_base_capture = ptr;
2062 chip->bank_base_capture_addr = ptr_addr;
2063 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2064 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002065 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 ptr += chip->bank_size_capture;
2067 ptr_addr += chip->bank_size_capture;
2068 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002069 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2070 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 chip->bank_base_effect = ptr;
2072 chip->bank_base_effect_addr = ptr_addr;
2073 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2074 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002075 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 ptr += chip->bank_size_effect;
2077 ptr_addr += chip->bank_size_effect;
2078 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002079 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2080 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 chip->work_base = ptr;
2082 chip->work_base_addr = ptr_addr;
2083
2084 snd_assert(ptr + chip->work_size == chip->work_ptr.area + chip->work_ptr.bytes, );
2085
2086 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2087 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2088 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2089 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2090 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2091
2092 /* S/PDIF output initialization */
2093 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2094 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2095 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2096
2097 /* S/PDIF input initialization */
2098 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2099
2100 /* digital mixer setup */
2101 for (reg = 0x80; reg < 0xc0; reg += 4)
2102 snd_ymfpci_writel(chip, reg, 0);
2103 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2104 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2105 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2106 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2107 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2108 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2109 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2110
2111 return 0;
2112}
2113
Takashi Iwai208a1b42005-11-17 14:53:41 +01002114static int snd_ymfpci_free(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 u16 ctrl;
2117
2118 snd_assert(chip != NULL, return -EINVAL);
2119
2120 if (chip->res_reg_area) { /* don't touch busy hardware */
2121 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2122 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2123 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2124 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2125 snd_ymfpci_disable_dsp(chip);
2126 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2127 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2128 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2129 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2130 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2131 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2132 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2133 }
2134
2135 snd_ymfpci_ac3_done(chip);
2136
2137 /* Set PCI device to D3 state */
2138#if 0
2139 /* FIXME: temporarily disabled, otherwise we cannot fire up
2140 * the chip again unless reboot. ACPI bug?
2141 */
2142 pci_set_power_state(chip->pci, 3);
2143#endif
2144
2145#ifdef CONFIG_PM
2146 vfree(chip->saved_regs);
2147#endif
Takashi Iwaib1d57762005-10-10 11:56:31 +02002148 release_and_free_resource(chip->mpu_res);
2149 release_and_free_resource(chip->fm_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 snd_ymfpci_free_gameport(chip);
2151 if (chip->reg_area_virt)
2152 iounmap(chip->reg_area_virt);
2153 if (chip->work_ptr.area)
2154 snd_dma_free_pages(&chip->work_ptr);
2155
2156 if (chip->irq >= 0)
2157 free_irq(chip->irq, (void *)chip);
Takashi Iwaib1d57762005-10-10 11:56:31 +02002158 release_and_free_resource(chip->res_reg_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2161
2162 pci_disable_device(chip->pci);
2163 kfree(chip);
2164 return 0;
2165}
2166
Takashi Iwai208a1b42005-11-17 14:53:41 +01002167static int snd_ymfpci_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002169 struct snd_ymfpci *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return snd_ymfpci_free(chip);
2171}
2172
2173#ifdef CONFIG_PM
2174static int saved_regs_index[] = {
2175 /* spdif */
2176 YDSXGR_SPDIFOUTCTRL,
2177 YDSXGR_SPDIFOUTSTATUS,
2178 YDSXGR_SPDIFINCTRL,
2179 /* volumes */
2180 YDSXGR_PRIADCLOOPVOL,
2181 YDSXGR_NATIVEDACINVOL,
2182 YDSXGR_NATIVEDACOUTVOL,
2183 // YDSXGR_BUF441OUTVOL,
2184 YDSXGR_NATIVEADCINVOL,
2185 YDSXGR_SPDIFLOOPVOL,
2186 YDSXGR_SPDIFOUTVOL,
2187 YDSXGR_ZVOUTVOL,
2188 YDSXGR_LEGACYOUTVOL,
2189 /* address bases */
2190 YDSXGR_PLAYCTRLBASE,
2191 YDSXGR_RECCTRLBASE,
2192 YDSXGR_EFFCTRLBASE,
2193 YDSXGR_WORKBASE,
2194 /* capture set up */
2195 YDSXGR_MAPOFREC,
2196 YDSXGR_RECFORMAT,
2197 YDSXGR_RECSLOTSR,
2198 YDSXGR_ADCFORMAT,
2199 YDSXGR_ADCSLOTSR,
2200};
2201#define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
2202
Takashi Iwaided46232005-11-17 16:09:43 +01002203int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Takashi Iwaided46232005-11-17 16:09:43 +01002205 struct snd_card *card = pci_get_drvdata(pci);
2206 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 unsigned int i;
2208
Takashi Iwaided46232005-11-17 16:09:43 +01002209 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 snd_pcm_suspend_all(chip->pcm);
2211 snd_pcm_suspend_all(chip->pcm2);
2212 snd_pcm_suspend_all(chip->pcm_spdif);
2213 snd_pcm_suspend_all(chip->pcm_4ch);
2214 snd_ac97_suspend(chip->ac97);
2215 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2216 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2217 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2218 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2219 snd_ymfpci_disable_dsp(chip);
Takashi Iwaided46232005-11-17 16:09:43 +01002220 pci_disable_device(pci);
2221 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002222 pci_set_power_state(pci, pci_choose_state(pci, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return 0;
2224}
2225
Takashi Iwaided46232005-11-17 16:09:43 +01002226int snd_ymfpci_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Takashi Iwaided46232005-11-17 16:09:43 +01002228 struct snd_card *card = pci_get_drvdata(pci);
2229 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 unsigned int i;
2231
Takashi Iwai30b35392006-10-11 18:52:53 +02002232 pci_set_power_state(pci, PCI_D0);
Takashi Iwaided46232005-11-17 16:09:43 +01002233 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002234 if (pci_enable_device(pci) < 0) {
2235 printk(KERN_ERR "ymfpci: pci_enable_device failed, "
2236 "disabling device\n");
2237 snd_card_disconnect(card);
2238 return -EIO;
2239 }
Takashi Iwaided46232005-11-17 16:09:43 +01002240 pci_set_master(pci);
2241 snd_ymfpci_aclink_reset(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 snd_ymfpci_codec_ready(chip, 0);
2243 snd_ymfpci_download_image(chip);
2244 udelay(100);
2245
2246 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2247 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2248
2249 snd_ac97_resume(chip->ac97);
2250
2251 /* start hw again */
2252 if (chip->start_count > 0) {
2253 spin_lock_irq(&chip->reg_lock);
2254 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2255 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2256 spin_unlock_irq(&chip->reg_lock);
2257 }
Takashi Iwaided46232005-11-17 16:09:43 +01002258 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return 0;
2260}
2261#endif /* CONFIG_PM */
2262
Takashi Iwai208a1b42005-11-17 14:53:41 +01002263int __devinit snd_ymfpci_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 struct pci_dev * pci,
2265 unsigned short old_legacy_ctrl,
Takashi Iwai208a1b42005-11-17 14:53:41 +01002266 struct snd_ymfpci ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002268 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002270 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 .dev_free = snd_ymfpci_dev_free,
2272 };
2273
2274 *rchip = NULL;
2275
2276 /* enable PCI device */
2277 if ((err = pci_enable_device(pci)) < 0)
2278 return err;
2279
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002280 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (chip == NULL) {
2282 pci_disable_device(pci);
2283 return -ENOMEM;
2284 }
2285 chip->old_legacy_ctrl = old_legacy_ctrl;
2286 spin_lock_init(&chip->reg_lock);
2287 spin_lock_init(&chip->voice_lock);
2288 init_waitqueue_head(&chip->interrupt_sleep);
2289 atomic_set(&chip->interrupt_sleep_count, 0);
2290 chip->card = card;
2291 chip->pci = pci;
2292 chip->irq = -1;
2293 chip->device_id = pci->device;
2294 pci_read_config_byte(pci, PCI_REVISION_ID, (u8 *)&chip->rev);
2295 chip->reg_area_phys = pci_resource_start(pci, 0);
2296 chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2297 pci_set_master(pci);
2298
2299 if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002300 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 -07002301 snd_ymfpci_free(chip);
2302 return -EBUSY;
2303 }
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07002304 if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_DISABLED|IRQF_SHARED, "YMFPCI", (void *) chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002305 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 snd_ymfpci_free(chip);
2307 return -EBUSY;
2308 }
2309 chip->irq = pci->irq;
2310
2311 snd_ymfpci_aclink_reset(pci);
2312 if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2313 snd_ymfpci_free(chip);
2314 return -EIO;
2315 }
2316
2317 snd_ymfpci_download_image(chip);
2318
2319 udelay(100); /* seems we need a delay after downloading image.. */
2320
2321 if (snd_ymfpci_memalloc(chip) < 0) {
2322 snd_ymfpci_free(chip);
2323 return -EIO;
2324 }
2325
2326 if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
2327 snd_ymfpci_free(chip);
2328 return err;
2329 }
2330
2331#ifdef CONFIG_PM
2332 chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
2333 if (chip->saved_regs == NULL) {
2334 snd_ymfpci_free(chip);
2335 return -ENOMEM;
2336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337#endif
2338
2339 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2340 snd_ymfpci_free(chip);
2341 return err;
2342 }
2343
2344 snd_ymfpci_proc_init(card, chip);
2345
2346 snd_card_set_dev(card, &pci->dev);
2347
2348 *rchip = chip;
2349 return 0;
2350}