blob: 60e8cb24bd4459d36ec000db9edf56d6c04d5c40 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02002 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Routines for control of YMF724/740/744/754 chips
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/delay.h>
Clemens Ladisch102fa902006-10-11 12:05:59 +020022#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/interrupt.h>
25#include <linux/pci.h>
26#include <linux/sched.h>
27#include <linux/slab.h>
David Woodhouseb82a82d2008-05-29 14:40:00 +030028#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040029#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <sound/core.h>
32#include <sound/control.h>
33#include <sound/info.h>
Takashi Iwai33925182006-08-28 13:29:42 +020034#include <sound/tlv.h>
Takashi Iwai81fcb172012-07-02 16:37:05 +020035#include "ymfpci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <sound/asoundef.h>
37#include <sound/mpu401.h>
38
39#include <asm/io.h>
Clemens Ladisch102fa902006-10-11 12:05:59 +020040#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * common I/O routines
44 */
45
Takashi Iwai208a1b42005-11-17 14:53:41 +010046static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Takashi Iwai208a1b42005-11-17 14:53:41 +010048static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 return readb(chip->reg_area_virt + offset);
51}
52
Takashi Iwai208a1b42005-11-17 14:53:41 +010053static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 writeb(val, chip->reg_area_virt + offset);
56}
57
Takashi Iwai208a1b42005-11-17 14:53:41 +010058static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 return readw(chip->reg_area_virt + offset);
61}
62
Takashi Iwai208a1b42005-11-17 14:53:41 +010063static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 writew(val, chip->reg_area_virt + offset);
66}
67
Takashi Iwai208a1b42005-11-17 14:53:41 +010068static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 return readl(chip->reg_area_virt + offset);
71}
72
Takashi Iwai208a1b42005-11-17 14:53:41 +010073static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 writel(val, chip->reg_area_virt + offset);
76}
77
Takashi Iwai208a1b42005-11-17 14:53:41 +010078static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020080 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
82
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020083 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 do {
85 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
86 return 0;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +020087 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020088 } while (time_before(jiffies, end_time));
Takashi Iwai99b359b2005-10-20 18:26:44 +020089 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return -EBUSY;
91}
92
Takashi Iwai208a1b42005-11-17 14:53:41 +010093static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Takashi Iwai208a1b42005-11-17 14:53:41 +010095 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 u32 cmd;
97
98 snd_ymfpci_codec_ready(chip, 0);
99 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
100 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
101}
102
Takashi Iwai208a1b42005-11-17 14:53:41 +0100103static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100105 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 if (snd_ymfpci_codec_ready(chip, 0))
108 return ~0;
109 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
110 if (snd_ymfpci_codec_ready(chip, 0))
111 return ~0;
112 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
113 int i;
114 for (i = 0; i < 600; i++)
115 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
116 }
117 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
118}
119
120/*
121 * Misc routines
122 */
123
124static u32 snd_ymfpci_calc_delta(u32 rate)
125{
126 switch (rate) {
127 case 8000: return 0x02aaab00;
128 case 11025: return 0x03accd00;
129 case 16000: return 0x05555500;
130 case 22050: return 0x07599a00;
131 case 32000: return 0x0aaaab00;
132 case 44100: return 0x0eb33300;
133 default: return ((rate << 16) / 375) << 5;
134 }
135}
136
137static u32 def_rate[8] = {
138 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
139};
140
141static u32 snd_ymfpci_calc_lpfK(u32 rate)
142{
143 u32 i;
144 static u32 val[8] = {
145 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
146 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
147 };
148
149 if (rate == 44100)
150 return 0x40000000; /* FIXME: What's the right value? */
151 for (i = 0; i < 8; i++)
152 if (rate <= def_rate[i])
153 return val[i];
154 return val[0];
155}
156
157static u32 snd_ymfpci_calc_lpfQ(u32 rate)
158{
159 u32 i;
160 static u32 val[8] = {
161 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
162 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
163 };
164
165 if (rate == 44100)
166 return 0x370A0000;
167 for (i = 0; i < 8; i++)
168 if (rate <= def_rate[i])
169 return val[i];
170 return val[0];
171}
172
173/*
174 * Hardware start management
175 */
176
Takashi Iwai208a1b42005-11-17 14:53:41 +0100177static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 unsigned long flags;
180
181 spin_lock_irqsave(&chip->reg_lock, flags);
182 if (chip->start_count++ > 0)
183 goto __end;
184 snd_ymfpci_writel(chip, YDSXGR_MODE,
185 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
186 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
187 __end:
188 spin_unlock_irqrestore(&chip->reg_lock, flags);
189}
190
Takashi Iwai208a1b42005-11-17 14:53:41 +0100191static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 unsigned long flags;
194 long timeout = 1000;
195
196 spin_lock_irqsave(&chip->reg_lock, flags);
197 if (--chip->start_count > 0)
198 goto __end;
199 snd_ymfpci_writel(chip, YDSXGR_MODE,
200 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
201 while (timeout-- > 0) {
202 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
203 break;
204 }
205 if (atomic_read(&chip->interrupt_sleep_count)) {
206 atomic_set(&chip->interrupt_sleep_count, 0);
207 wake_up(&chip->interrupt_sleep);
208 }
209 __end:
210 spin_unlock_irqrestore(&chip->reg_lock, flags);
211}
212
213/*
214 * Playback voice management
215 */
216
Takashi Iwai208a1b42005-11-17 14:53:41 +0100217static int voice_alloc(struct snd_ymfpci *chip,
218 enum snd_ymfpci_voice_type type, int pair,
219 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100221 struct snd_ymfpci_voice *voice, *voice2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int idx;
223
224 *rvoice = NULL;
225 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
226 voice = &chip->voices[idx];
227 voice2 = pair ? &chip->voices[idx+1] : NULL;
228 if (voice->use || (voice2 && voice2->use))
229 continue;
230 voice->use = 1;
231 if (voice2)
232 voice2->use = 1;
233 switch (type) {
234 case YMFPCI_PCM:
235 voice->pcm = 1;
236 if (voice2)
237 voice2->pcm = 1;
238 break;
239 case YMFPCI_SYNTH:
240 voice->synth = 1;
241 break;
242 case YMFPCI_MIDI:
243 voice->midi = 1;
244 break;
245 }
246 snd_ymfpci_hw_start(chip);
247 if (voice2)
248 snd_ymfpci_hw_start(chip);
249 *rvoice = voice;
250 return 0;
251 }
252 return -ENOMEM;
253}
254
Takashi Iwai208a1b42005-11-17 14:53:41 +0100255static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
256 enum snd_ymfpci_voice_type type, int pair,
257 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 unsigned long flags;
260 int result;
261
Takashi Iwaida3cec32008-08-08 17:12:14 +0200262 if (snd_BUG_ON(!rvoice))
263 return -EINVAL;
264 if (snd_BUG_ON(pair && type != YMFPCI_PCM))
265 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 spin_lock_irqsave(&chip->voice_lock, flags);
268 for (;;) {
269 result = voice_alloc(chip, type, pair, rvoice);
270 if (result == 0 || type != YMFPCI_PCM)
271 break;
272 /* TODO: synth/midi voice deallocation */
273 break;
274 }
275 spin_unlock_irqrestore(&chip->voice_lock, flags);
276 return result;
277}
278
Takashi Iwai208a1b42005-11-17 14:53:41 +0100279static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 unsigned long flags;
282
Takashi Iwaida3cec32008-08-08 17:12:14 +0200283 if (snd_BUG_ON(!pvoice))
284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 snd_ymfpci_hw_stop(chip);
286 spin_lock_irqsave(&chip->voice_lock, flags);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100287 if (pvoice->number == chip->src441_used) {
288 chip->src441_used = -1;
289 pvoice->ypcm->use_441_slot = 0;
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
292 pvoice->ypcm = NULL;
293 pvoice->interrupt = NULL;
294 spin_unlock_irqrestore(&chip->voice_lock, flags);
295 return 0;
296}
297
298/*
299 * PCM part
300 */
301
Takashi Iwai208a1b42005-11-17 14:53:41 +0100302static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100304 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 u32 pos, delta;
306
307 if ((ypcm = voice->ypcm) == NULL)
308 return;
309 if (ypcm->substream == NULL)
310 return;
311 spin_lock(&chip->reg_lock);
312 if (ypcm->running) {
313 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
314 if (pos < ypcm->last_pos)
315 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
316 else
317 delta = pos - ypcm->last_pos;
318 ypcm->period_pos += delta;
319 ypcm->last_pos = pos;
320 if (ypcm->period_pos >= ypcm->period_size) {
Takashi Iwaiee419652009-02-05 16:11:31 +0100321 /*
322 printk(KERN_DEBUG
323 "done - active_bank = 0x%x, start = 0x%x\n",
324 chip->active_bank,
325 voice->bank[chip->active_bank].start);
326 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 ypcm->period_pos %= ypcm->period_size;
328 spin_unlock(&chip->reg_lock);
329 snd_pcm_period_elapsed(ypcm->substream);
330 spin_lock(&chip->reg_lock);
331 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200332
333 if (unlikely(ypcm->update_pcm_vol)) {
334 unsigned int subs = ypcm->substream->number;
335 unsigned int next_bank = 1 - chip->active_bank;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100336 struct snd_ymfpci_playback_bank *bank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200337 u32 volume;
338
339 bank = &voice->bank[next_bank];
340 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
341 bank->left_gain_end = volume;
342 if (ypcm->output_rear)
343 bank->eff2_gain_end = volume;
344 if (ypcm->voices[1])
345 bank = &ypcm->voices[1]->bank[next_bank];
346 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
347 bank->right_gain_end = volume;
348 if (ypcm->output_rear)
349 bank->eff3_gain_end = volume;
350 ypcm->update_pcm_vol--;
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 spin_unlock(&chip->reg_lock);
354}
355
Takashi Iwai208a1b42005-11-17 14:53:41 +0100356static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100358 struct snd_pcm_runtime *runtime = substream->runtime;
359 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
360 struct snd_ymfpci *chip = ypcm->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 u32 pos, delta;
362
363 spin_lock(&chip->reg_lock);
364 if (ypcm->running) {
365 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
366 if (pos < ypcm->last_pos)
367 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
368 else
369 delta = pos - ypcm->last_pos;
370 ypcm->period_pos += delta;
371 ypcm->last_pos = pos;
372 if (ypcm->period_pos >= ypcm->period_size) {
373 ypcm->period_pos %= ypcm->period_size;
Takashi Iwaiee419652009-02-05 16:11:31 +0100374 /*
375 printk(KERN_DEBUG
376 "done - active_bank = 0x%x, start = 0x%x\n",
377 chip->active_bank,
378 voice->bank[chip->active_bank].start);
379 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 spin_unlock(&chip->reg_lock);
381 snd_pcm_period_elapsed(substream);
382 spin_lock(&chip->reg_lock);
383 }
384 }
385 spin_unlock(&chip->reg_lock);
386}
387
Takashi Iwai208a1b42005-11-17 14:53:41 +0100388static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 int cmd)
390{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100391 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
392 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200393 struct snd_kcontrol *kctl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int result = 0;
395
396 spin_lock(&chip->reg_lock);
397 if (ypcm->voices[0] == NULL) {
398 result = -EINVAL;
399 goto __unlock;
400 }
401 switch (cmd) {
402 case SNDRV_PCM_TRIGGER_START:
403 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
404 case SNDRV_PCM_TRIGGER_RESUME:
405 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100406 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
408 ypcm->running = 1;
409 break;
410 case SNDRV_PCM_TRIGGER_STOP:
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200411 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
412 kctl = chip->pcm_mixer[substream->number].ctl;
413 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
414 }
415 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
417 case SNDRV_PCM_TRIGGER_SUSPEND:
418 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100419 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
421 ypcm->running = 0;
422 break;
423 default:
424 result = -EINVAL;
425 break;
426 }
427 __unlock:
428 spin_unlock(&chip->reg_lock);
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200429 if (kctl)
430 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return result;
432}
Takashi Iwai208a1b42005-11-17 14:53:41 +0100433static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 int cmd)
435{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100436 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
437 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 int result = 0;
439 u32 tmp;
440
441 spin_lock(&chip->reg_lock);
442 switch (cmd) {
443 case SNDRV_PCM_TRIGGER_START:
444 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
445 case SNDRV_PCM_TRIGGER_RESUME:
446 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
447 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
448 ypcm->running = 1;
449 break;
450 case SNDRV_PCM_TRIGGER_STOP:
451 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
452 case SNDRV_PCM_TRIGGER_SUSPEND:
453 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
454 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
455 ypcm->running = 0;
456 break;
457 default:
458 result = -EINVAL;
459 break;
460 }
461 spin_unlock(&chip->reg_lock);
462 return result;
463}
464
Takashi Iwai208a1b42005-11-17 14:53:41 +0100465static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 int err;
468
469 if (ypcm->voices[1] != NULL && voices < 2) {
470 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
471 ypcm->voices[1] = NULL;
472 }
473 if (voices == 1 && ypcm->voices[0] != NULL)
474 return 0; /* already allocated */
475 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
476 return 0; /* already allocated */
477 if (voices > 1) {
478 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
479 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
480 ypcm->voices[0] = NULL;
481 }
482 }
483 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
484 if (err < 0)
485 return err;
486 ypcm->voices[0]->ypcm = ypcm;
487 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
488 if (voices > 1) {
489 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
490 ypcm->voices[1]->ypcm = ypcm;
491 }
492 return 0;
493}
494
Takashi Iwai208a1b42005-11-17 14:53:41 +0100495static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
496 struct snd_pcm_runtime *runtime,
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200497 int has_pcm_volume)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100499 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 u32 format;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200501 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
502 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
503 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
Takashi Iwai208a1b42005-11-17 14:53:41 +0100504 struct snd_ymfpci_playback_bank *bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 unsigned int nbank;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200506 u32 vol_left, vol_right;
507 u8 use_left, use_right;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100508 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Takashi Iwaida3cec32008-08-08 17:12:14 +0200510 if (snd_BUG_ON(!voice))
511 return;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200512 if (runtime->channels == 1) {
513 use_left = 1;
514 use_right = 1;
515 } else {
516 use_left = (voiceidx & 1) == 0;
517 use_right = !use_left;
518 }
519 if (has_pcm_volume) {
520 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
521 [ypcm->substream->number].left << 15);
522 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
523 [ypcm->substream->number].right << 15);
524 } else {
525 vol_left = cpu_to_le32(0x40000000);
526 vol_right = cpu_to_le32(0x40000000);
527 }
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100528 spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200529 format = runtime->channels == 2 ? 0x00010000 : 0;
530 if (snd_pcm_format_width(runtime->format) == 8)
531 format |= 0x80000000;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100532 else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
533 runtime->rate == 44100 && runtime->channels == 2 &&
534 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
535 ypcm->chip->src441_used == voice->number)) {
536 ypcm->chip->src441_used = voice->number;
537 ypcm->use_441_slot = 1;
538 format |= 0x10000000;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100539 }
540 if (ypcm->chip->src441_used == voice->number &&
541 (format & 0x10000000) == 0) {
542 ypcm->chip->src441_used = -1;
543 ypcm->use_441_slot = 0;
544 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200545 if (runtime->channels == 2 && (voiceidx & 1) != 0)
546 format |= 1;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100547 spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 for (nbank = 0; nbank < 2; nbank++) {
549 bank = &voice->bank[nbank];
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200550 memset(bank, 0, sizeof(*bank));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 bank->format = cpu_to_le32(format);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200552 bank->base = cpu_to_le32(runtime->dma_addr);
553 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 bank->lpfQ = cpu_to_le32(lpfQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 bank->delta =
556 bank->delta_end = cpu_to_le32(delta);
557 bank->lpfK =
558 bank->lpfK_end = cpu_to_le32(lpfK);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200559 bank->eg_gain =
560 bank->eg_gain_end = cpu_to_le32(0x40000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200562 if (ypcm->output_front) {
563 if (use_left) {
564 bank->left_gain =
565 bank->left_gain_end = vol_left;
566 }
567 if (use_right) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 bank->right_gain =
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200569 bank->right_gain_end = vol_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200571 }
572 if (ypcm->output_rear) {
Jaroslav Kysela5a25c5c2006-01-18 08:02:24 +0100573 if (!ypcm->swap_rear) {
574 if (use_left) {
575 bank->eff2_gain =
576 bank->eff2_gain_end = vol_left;
577 }
578 if (use_right) {
579 bank->eff3_gain =
580 bank->eff3_gain_end = vol_right;
581 }
582 } else {
583 /* The SPDIF out channels seem to be swapped, so we have
584 * to swap them here, too. The rear analog out channels
585 * will be wrong, but otherwise AC3 would not work.
586 */
587 if (use_left) {
588 bank->eff3_gain =
589 bank->eff3_gain_end = vol_left;
590 }
591 if (use_right) {
592 bank->eff2_gain =
593 bank->eff2_gain_end = vol_right;
594 }
595 }
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598}
599
Takashi Iwai208a1b42005-11-17 14:53:41 +0100600static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
603 4096, &chip->ac3_tmp_base) < 0)
604 return -ENOMEM;
605
606 chip->bank_effect[3][0]->base =
607 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
608 chip->bank_effect[3][0]->loop_end =
609 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
610 chip->bank_effect[4][0]->base =
611 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
612 chip->bank_effect[4][0]->loop_end =
613 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
614
615 spin_lock_irq(&chip->reg_lock);
616 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
617 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
618 spin_unlock_irq(&chip->reg_lock);
619 return 0;
620}
621
Takashi Iwai208a1b42005-11-17 14:53:41 +0100622static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 spin_lock_irq(&chip->reg_lock);
625 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
626 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
627 spin_unlock_irq(&chip->reg_lock);
628 // snd_ymfpci_irq_wait(chip);
629 if (chip->ac3_tmp_base.area) {
630 snd_dma_free_pages(&chip->ac3_tmp_base);
631 chip->ac3_tmp_base.area = NULL;
632 }
633 return 0;
634}
635
Takashi Iwai208a1b42005-11-17 14:53:41 +0100636static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
637 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100639 struct snd_pcm_runtime *runtime = substream->runtime;
640 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 int err;
642
643 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
644 return err;
645 if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
646 return err;
647 return 0;
648}
649
Takashi Iwai208a1b42005-11-17 14:53:41 +0100650static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100652 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
653 struct snd_pcm_runtime *runtime = substream->runtime;
654 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 if (runtime->private_data == NULL)
657 return 0;
658 ypcm = runtime->private_data;
659
660 /* wait, until the PCI operations are not finished */
661 snd_ymfpci_irq_wait(chip);
662 snd_pcm_lib_free_pages(substream);
663 if (ypcm->voices[1]) {
664 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
665 ypcm->voices[1] = NULL;
666 }
667 if (ypcm->voices[0]) {
668 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
669 ypcm->voices[0] = NULL;
670 }
671 return 0;
672}
673
Takashi Iwai208a1b42005-11-17 14:53:41 +0100674static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100676 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
677 struct snd_pcm_runtime *runtime = substream->runtime;
678 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200679 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 unsigned int nvoice;
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 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200687 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
688 substream->pcm == chip->pcm);
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200689
690 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
691 kctl = chip->pcm_mixer[substream->number].ctl;
692 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
693 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return 0;
696}
697
Takashi Iwai208a1b42005-11-17 14:53:41 +0100698static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
699 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
702}
703
Takashi Iwai208a1b42005-11-17 14:53:41 +0100704static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100706 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 /* wait, until the PCI operations are not finished */
709 snd_ymfpci_irq_wait(chip);
710 return snd_pcm_lib_free_pages(substream);
711}
712
Takashi Iwai208a1b42005-11-17 14:53:41 +0100713static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100715 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
716 struct snd_pcm_runtime *runtime = substream->runtime;
717 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
718 struct snd_ymfpci_capture_bank * bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int nbank;
720 u32 rate, format;
721
722 ypcm->period_size = runtime->period_size;
723 ypcm->buffer_size = runtime->buffer_size;
724 ypcm->period_pos = 0;
725 ypcm->last_pos = 0;
726 ypcm->shift = 0;
727 rate = ((48000 * 4096) / runtime->rate) - 1;
728 format = 0;
729 if (runtime->channels == 2) {
730 format |= 2;
731 ypcm->shift++;
732 }
733 if (snd_pcm_format_width(runtime->format) == 8)
734 format |= 1;
735 else
736 ypcm->shift++;
737 switch (ypcm->capture_bank_number) {
738 case 0:
739 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
740 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
741 break;
742 case 1:
743 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
744 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
745 break;
746 }
747 for (nbank = 0; nbank < 2; nbank++) {
748 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
749 bank->base = cpu_to_le32(runtime->dma_addr);
750 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
751 bank->start = 0;
752 bank->num_of_loops = 0;
753 }
754 return 0;
755}
756
Takashi Iwai208a1b42005-11-17 14:53:41 +0100757static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100759 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
760 struct snd_pcm_runtime *runtime = substream->runtime;
761 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
762 struct snd_ymfpci_voice *voice = ypcm->voices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (!(ypcm->running && voice))
765 return 0;
766 return le32_to_cpu(voice->bank[chip->active_bank].start);
767}
768
Takashi Iwai208a1b42005-11-17 14:53:41 +0100769static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100771 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
772 struct snd_pcm_runtime *runtime = substream->runtime;
773 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 if (!ypcm->running)
776 return 0;
777 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
778}
779
Takashi Iwai208a1b42005-11-17 14:53:41 +0100780static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 wait_queue_t wait;
783 int loops = 4;
784
785 while (loops-- > 0) {
786 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
787 continue;
788 init_waitqueue_entry(&wait, current);
789 add_wait_queue(&chip->interrupt_sleep, &wait);
790 atomic_inc(&chip->interrupt_sleep_count);
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200791 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 remove_wait_queue(&chip->interrupt_sleep, &wait);
793 }
794}
795
David Howells7d12e782006-10-05 14:55:46 +0100796static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100798 struct snd_ymfpci *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 u32 status, nvoice, mode;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100800 struct snd_ymfpci_voice *voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
803 if (status & 0x80000000) {
804 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
805 spin_lock(&chip->voice_lock);
806 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
807 voice = &chip->voices[nvoice];
808 if (voice->interrupt)
809 voice->interrupt(chip, voice);
810 }
811 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
812 if (chip->capture_substream[nvoice])
813 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
814 }
815#if 0
816 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
817 if (chip->effect_substream[nvoice])
818 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
819 }
820#endif
821 spin_unlock(&chip->voice_lock);
822 spin_lock(&chip->reg_lock);
823 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
824 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
825 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
826 spin_unlock(&chip->reg_lock);
827
828 if (atomic_read(&chip->interrupt_sleep_count)) {
829 atomic_set(&chip->interrupt_sleep_count, 0);
830 wake_up(&chip->interrupt_sleep);
831 }
832 }
833
834 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
835 if (status & 1) {
836 if (chip->timer)
Clemens Ladisch6e2efaa2009-08-10 10:06:53 +0200837 snd_timer_interrupt(chip->timer, chip->timer_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
840
841 if (chip->rawmidi)
David Howells7d12e782006-10-05 14:55:46 +0100842 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return IRQ_HANDLED;
844}
845
Takashi Iwai208a1b42005-11-17 14:53:41 +0100846static struct snd_pcm_hardware snd_ymfpci_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 .info = (SNDRV_PCM_INFO_MMAP |
849 SNDRV_PCM_INFO_MMAP_VALID |
850 SNDRV_PCM_INFO_INTERLEAVED |
851 SNDRV_PCM_INFO_BLOCK_TRANSFER |
852 SNDRV_PCM_INFO_PAUSE |
853 SNDRV_PCM_INFO_RESUME),
854 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
855 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
856 .rate_min = 8000,
857 .rate_max = 48000,
858 .channels_min = 1,
859 .channels_max = 2,
860 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
861 .period_bytes_min = 64,
862 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
863 .periods_min = 3,
864 .periods_max = 1024,
865 .fifo_size = 0,
866};
867
Takashi Iwai208a1b42005-11-17 14:53:41 +0100868static struct snd_pcm_hardware snd_ymfpci_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 .info = (SNDRV_PCM_INFO_MMAP |
871 SNDRV_PCM_INFO_MMAP_VALID |
872 SNDRV_PCM_INFO_INTERLEAVED |
873 SNDRV_PCM_INFO_BLOCK_TRANSFER |
874 SNDRV_PCM_INFO_PAUSE |
875 SNDRV_PCM_INFO_RESUME),
876 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
877 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
878 .rate_min = 8000,
879 .rate_max = 48000,
880 .channels_min = 1,
881 .channels_max = 2,
882 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
883 .period_bytes_min = 64,
884 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
885 .periods_min = 3,
886 .periods_max = 1024,
887 .fifo_size = 0,
888};
889
Takashi Iwai208a1b42005-11-17 14:53:41 +0100890static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Jesper Juhl4d572772005-05-30 17:30:32 +0200892 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Takashi Iwai208a1b42005-11-17 14:53:41 +0100895static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100897 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
898 struct snd_pcm_runtime *runtime = substream->runtime;
899 struct snd_ymfpci_pcm *ypcm;
Clemens Ladisch84f9df12011-09-16 22:52:48 +0200900 int err;
901
902 runtime->hw = snd_ymfpci_playback;
903 /* FIXME? True value is 256/48 = 5.33333 ms */
904 err = snd_pcm_hw_constraint_minmax(runtime,
905 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
906 5334, UINT_MAX);
907 if (err < 0)
908 return err;
Clemens Ladisch5b0416a2011-09-16 23:08:28 +0200909 err = snd_pcm_hw_rule_noresample(runtime, 48000);
910 if (err < 0)
911 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200913 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (ypcm == NULL)
915 return -ENOMEM;
916 ypcm->chip = chip;
917 ypcm->type = PLAYBACK_VOICE;
918 ypcm->substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 runtime->private_data = ypcm;
920 runtime->private_free = snd_ymfpci_pcm_free_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
922}
923
924/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100925static void ymfpci_open_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 if (! chip->rear_opened) {
928 if (! chip->spdif_opened) /* set AC3 */
929 snd_ymfpci_writel(chip, YDSXGR_MODE,
930 snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
931 /* enable second codec (4CHEN) */
932 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
933 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
934 }
935}
936
937/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100938static void ymfpci_close_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 if (! chip->rear_opened) {
941 if (! chip->spdif_opened)
942 snd_ymfpci_writel(chip, YDSXGR_MODE,
943 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
944 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
945 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
946 }
947}
948
Takashi Iwai208a1b42005-11-17 14:53:41 +0100949static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100951 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
952 struct snd_pcm_runtime *runtime = substream->runtime;
953 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 int err;
955
956 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
957 return err;
958 ypcm = runtime->private_data;
959 ypcm->output_front = 1;
960 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
Glen Masgaid9301262006-10-10 09:27:19 +0200961 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 spin_lock_irq(&chip->reg_lock);
963 if (ypcm->output_rear) {
964 ymfpci_open_extension(chip);
965 chip->rear_opened++;
966 }
967 spin_unlock_irq(&chip->reg_lock);
968 return 0;
969}
970
Takashi Iwai208a1b42005-11-17 14:53:41 +0100971static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100973 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
974 struct snd_pcm_runtime *runtime = substream->runtime;
975 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 int err;
977
978 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
979 return err;
980 ypcm = runtime->private_data;
981 ypcm->output_front = 0;
982 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200983 ypcm->swap_rear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 spin_lock_irq(&chip->reg_lock);
985 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
986 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
987 ymfpci_open_extension(chip);
988 chip->spdif_pcm_bits = chip->spdif_bits;
989 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
990 chip->spdif_opened++;
991 spin_unlock_irq(&chip->reg_lock);
992
993 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
994 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
995 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
996 return 0;
997}
998
Takashi Iwai208a1b42005-11-17 14:53:41 +0100999static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001001 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1002 struct snd_pcm_runtime *runtime = substream->runtime;
1003 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 int err;
1005
1006 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
1007 return err;
1008 ypcm = runtime->private_data;
1009 ypcm->output_front = 0;
1010 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +02001011 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 spin_lock_irq(&chip->reg_lock);
1013 ymfpci_open_extension(chip);
1014 chip->rear_opened++;
1015 spin_unlock_irq(&chip->reg_lock);
1016 return 0;
1017}
1018
Takashi Iwai208a1b42005-11-17 14:53:41 +01001019static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 u32 capture_bank_number)
1021{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001022 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1023 struct snd_pcm_runtime *runtime = substream->runtime;
1024 struct snd_ymfpci_pcm *ypcm;
Clemens Ladisch84f9df12011-09-16 22:52:48 +02001025 int err;
1026
1027 runtime->hw = snd_ymfpci_capture;
1028 /* FIXME? True value is 256/48 = 5.33333 ms */
1029 err = snd_pcm_hw_constraint_minmax(runtime,
1030 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1031 5334, UINT_MAX);
1032 if (err < 0)
1033 return err;
Clemens Ladisch5b0416a2011-09-16 23:08:28 +02001034 err = snd_pcm_hw_rule_noresample(runtime, 48000);
1035 if (err < 0)
1036 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001038 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (ypcm == NULL)
1040 return -ENOMEM;
1041 ypcm->chip = chip;
1042 ypcm->type = capture_bank_number + CAPTURE_REC;
1043 ypcm->substream = substream;
1044 ypcm->capture_bank_number = capture_bank_number;
1045 chip->capture_substream[capture_bank_number] = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 runtime->private_data = ypcm;
1047 runtime->private_free = snd_ymfpci_pcm_free_substream;
1048 snd_ymfpci_hw_start(chip);
1049 return 0;
1050}
1051
Takashi Iwai208a1b42005-11-17 14:53:41 +01001052static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 return snd_ymfpci_capture_open(substream, 0);
1055}
1056
Takashi Iwai208a1b42005-11-17 14:53:41 +01001057static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 return snd_ymfpci_capture_open(substream, 1);
1060}
1061
Takashi Iwai208a1b42005-11-17 14:53:41 +01001062static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 return 0;
1065}
1066
Takashi Iwai208a1b42005-11-17 14:53:41 +01001067static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001069 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1070 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 spin_lock_irq(&chip->reg_lock);
1073 if (ypcm->output_rear && chip->rear_opened > 0) {
1074 chip->rear_opened--;
1075 ymfpci_close_extension(chip);
1076 }
1077 spin_unlock_irq(&chip->reg_lock);
1078 return snd_ymfpci_playback_close_1(substream);
1079}
1080
Takashi Iwai208a1b42005-11-17 14:53:41 +01001081static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001083 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 spin_lock_irq(&chip->reg_lock);
1086 chip->spdif_opened = 0;
1087 ymfpci_close_extension(chip);
1088 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1089 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1090 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1091 spin_unlock_irq(&chip->reg_lock);
1092 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1093 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1094 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1095 return snd_ymfpci_playback_close_1(substream);
1096}
1097
Takashi Iwai208a1b42005-11-17 14:53:41 +01001098static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001100 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 spin_lock_irq(&chip->reg_lock);
1103 if (chip->rear_opened > 0) {
1104 chip->rear_opened--;
1105 ymfpci_close_extension(chip);
1106 }
1107 spin_unlock_irq(&chip->reg_lock);
1108 return snd_ymfpci_playback_close_1(substream);
1109}
1110
Takashi Iwai208a1b42005-11-17 14:53:41 +01001111static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001113 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1114 struct snd_pcm_runtime *runtime = substream->runtime;
1115 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (ypcm != NULL) {
1118 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1119 snd_ymfpci_hw_stop(chip);
1120 }
1121 return 0;
1122}
1123
Takashi Iwai208a1b42005-11-17 14:53:41 +01001124static struct snd_pcm_ops snd_ymfpci_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 .open = snd_ymfpci_playback_open,
1126 .close = snd_ymfpci_playback_close,
1127 .ioctl = snd_pcm_lib_ioctl,
1128 .hw_params = snd_ymfpci_playback_hw_params,
1129 .hw_free = snd_ymfpci_playback_hw_free,
1130 .prepare = snd_ymfpci_playback_prepare,
1131 .trigger = snd_ymfpci_playback_trigger,
1132 .pointer = snd_ymfpci_playback_pointer,
1133};
1134
Takashi Iwai208a1b42005-11-17 14:53:41 +01001135static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 .open = snd_ymfpci_capture_rec_open,
1137 .close = snd_ymfpci_capture_close,
1138 .ioctl = snd_pcm_lib_ioctl,
1139 .hw_params = snd_ymfpci_capture_hw_params,
1140 .hw_free = snd_ymfpci_capture_hw_free,
1141 .prepare = snd_ymfpci_capture_prepare,
1142 .trigger = snd_ymfpci_capture_trigger,
1143 .pointer = snd_ymfpci_capture_pointer,
1144};
1145
Takashi Iwai208a1b42005-11-17 14:53:41 +01001146int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001148 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 int err;
1150
1151 if (rpcm)
1152 *rpcm = NULL;
1153 if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1154 return err;
1155 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1158 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1159
1160 /* global setup */
1161 pcm->info_flags = 0;
1162 strcpy(pcm->name, "YMFPCI");
1163 chip->pcm = pcm;
1164
1165 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1166 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1167
Takashi Iwai0afdb8f2012-09-12 16:14:37 +02001168 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1169 snd_pcm_std_chmaps, 2, 0, NULL);
1170 if (err < 0)
1171 return err;
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (rpcm)
1174 *rpcm = pcm;
1175 return 0;
1176}
1177
Takashi Iwai208a1b42005-11-17 14:53:41 +01001178static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 .open = snd_ymfpci_capture_ac97_open,
1180 .close = snd_ymfpci_capture_close,
1181 .ioctl = snd_pcm_lib_ioctl,
1182 .hw_params = snd_ymfpci_capture_hw_params,
1183 .hw_free = snd_ymfpci_capture_hw_free,
1184 .prepare = snd_ymfpci_capture_prepare,
1185 .trigger = snd_ymfpci_capture_trigger,
1186 .pointer = snd_ymfpci_capture_pointer,
1187};
1188
Takashi Iwai208a1b42005-11-17 14:53:41 +01001189int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001191 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int err;
1193
1194 if (rpcm)
1195 *rpcm = NULL;
1196 if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1197 return err;
1198 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1201
1202 /* global setup */
1203 pcm->info_flags = 0;
1204 sprintf(pcm->name, "YMFPCI - %s",
1205 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1206 chip->pcm2 = pcm;
1207
1208 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1209 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1210
1211 if (rpcm)
1212 *rpcm = pcm;
1213 return 0;
1214}
1215
Takashi Iwai208a1b42005-11-17 14:53:41 +01001216static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 .open = snd_ymfpci_playback_spdif_open,
1218 .close = snd_ymfpci_playback_spdif_close,
1219 .ioctl = snd_pcm_lib_ioctl,
1220 .hw_params = snd_ymfpci_playback_hw_params,
1221 .hw_free = snd_ymfpci_playback_hw_free,
1222 .prepare = snd_ymfpci_playback_prepare,
1223 .trigger = snd_ymfpci_playback_trigger,
1224 .pointer = snd_ymfpci_playback_pointer,
1225};
1226
Takashi Iwai208a1b42005-11-17 14:53:41 +01001227int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001229 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int err;
1231
1232 if (rpcm)
1233 *rpcm = NULL;
1234 if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1235 return err;
1236 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1239
1240 /* global setup */
1241 pcm->info_flags = 0;
1242 strcpy(pcm->name, "YMFPCI - IEC958");
1243 chip->pcm_spdif = pcm;
1244
1245 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1246 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1247
1248 if (rpcm)
1249 *rpcm = pcm;
1250 return 0;
1251}
1252
Takashi Iwai208a1b42005-11-17 14:53:41 +01001253static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 .open = snd_ymfpci_playback_4ch_open,
1255 .close = snd_ymfpci_playback_4ch_close,
1256 .ioctl = snd_pcm_lib_ioctl,
1257 .hw_params = snd_ymfpci_playback_hw_params,
1258 .hw_free = snd_ymfpci_playback_hw_free,
1259 .prepare = snd_ymfpci_playback_prepare,
1260 .trigger = snd_ymfpci_playback_trigger,
1261 .pointer = snd_ymfpci_playback_pointer,
1262};
1263
Takashi Iwai0afdb8f2012-09-12 16:14:37 +02001264static const struct snd_pcm_chmap_elem surround_map[] = {
1265 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02001266 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai0afdb8f2012-09-12 16:14:37 +02001267 { .channels = 2,
1268 .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
1269 { }
1270};
1271
Takashi Iwai208a1b42005-11-17 14:53:41 +01001272int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001274 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int err;
1276
1277 if (rpcm)
1278 *rpcm = NULL;
1279 if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1280 return err;
1281 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1284
1285 /* global setup */
1286 pcm->info_flags = 0;
1287 strcpy(pcm->name, "YMFPCI - Rear PCM");
1288 chip->pcm_4ch = pcm;
1289
1290 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1291 snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1292
Takashi Iwai0afdb8f2012-09-12 16:14:37 +02001293 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1294 surround_map, 2, 0, NULL);
1295 if (err < 0)
1296 return err;
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (rpcm)
1299 *rpcm = pcm;
1300 return 0;
1301}
1302
Takashi Iwai208a1b42005-11-17 14:53:41 +01001303static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1306 uinfo->count = 1;
1307 return 0;
1308}
1309
Takashi Iwai208a1b42005-11-17 14:53:41 +01001310static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1311 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001313 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 spin_lock_irq(&chip->reg_lock);
1316 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1317 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001318 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 spin_unlock_irq(&chip->reg_lock);
1320 return 0;
1321}
1322
Takashi Iwai208a1b42005-11-17 14:53:41 +01001323static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1324 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001326 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 unsigned int val;
1328 int change;
1329
1330 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1331 (ucontrol->value.iec958.status[1] << 8);
1332 spin_lock_irq(&chip->reg_lock);
1333 change = chip->spdif_bits != val;
1334 chip->spdif_bits = val;
1335 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1336 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1337 spin_unlock_irq(&chip->reg_lock);
1338 return change;
1339}
1340
Takashi Iwai208a1b42005-11-17 14:53:41 +01001341static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1344 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1345 .info = snd_ymfpci_spdif_default_info,
1346 .get = snd_ymfpci_spdif_default_get,
1347 .put = snd_ymfpci_spdif_default_put
1348};
1349
Takashi Iwai208a1b42005-11-17 14:53:41 +01001350static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1353 uinfo->count = 1;
1354 return 0;
1355}
1356
Takashi Iwai208a1b42005-11-17 14:53:41 +01001357static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1358 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001360 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 spin_lock_irq(&chip->reg_lock);
1363 ucontrol->value.iec958.status[0] = 0x3e;
1364 ucontrol->value.iec958.status[1] = 0xff;
1365 spin_unlock_irq(&chip->reg_lock);
1366 return 0;
1367}
1368
Takashi Iwai208a1b42005-11-17 14:53:41 +01001369static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1372 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1373 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1374 .info = snd_ymfpci_spdif_mask_info,
1375 .get = snd_ymfpci_spdif_mask_get,
1376};
1377
Takashi Iwai208a1b42005-11-17 14:53:41 +01001378static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1381 uinfo->count = 1;
1382 return 0;
1383}
1384
Takashi Iwai208a1b42005-11-17 14:53:41 +01001385static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1386 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001388 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 spin_lock_irq(&chip->reg_lock);
1391 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1392 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001393 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 spin_unlock_irq(&chip->reg_lock);
1395 return 0;
1396}
1397
Takashi Iwai208a1b42005-11-17 14:53:41 +01001398static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1399 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001401 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 unsigned int val;
1403 int change;
1404
1405 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1406 (ucontrol->value.iec958.status[1] << 8);
1407 spin_lock_irq(&chip->reg_lock);
1408 change = chip->spdif_pcm_bits != val;
1409 chip->spdif_pcm_bits = val;
1410 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1411 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1412 spin_unlock_irq(&chip->reg_lock);
1413 return change;
1414}
1415
Takashi Iwai208a1b42005-11-17 14:53:41 +01001416static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1419 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1420 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1421 .info = snd_ymfpci_spdif_stream_info,
1422 .get = snd_ymfpci_spdif_stream_get,
1423 .put = snd_ymfpci_spdif_stream_put
1424};
1425
Takashi Iwai208a1b42005-11-17 14:53:41 +01001426static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Clemens Ladischbed68962011-01-10 16:29:06 +01001428 static const char *const texts[3] = {"AC'97", "IEC958", "ZV Port"};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Clemens Ladischbed68962011-01-10 16:29:06 +01001430 return snd_ctl_enum_info(info, 1, 3, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Takashi Iwai208a1b42005-11-17 14:53:41 +01001433static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001435 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 u16 reg;
1437
1438 spin_lock_irq(&chip->reg_lock);
1439 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1440 spin_unlock_irq(&chip->reg_lock);
1441 if (!(reg & 0x100))
1442 value->value.enumerated.item[0] = 0;
1443 else
1444 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1445 return 0;
1446}
1447
Takashi Iwai208a1b42005-11-17 14:53:41 +01001448static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001450 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 u16 reg, old_reg;
1452
1453 spin_lock_irq(&chip->reg_lock);
1454 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1455 if (value->value.enumerated.item[0] == 0)
1456 reg = old_reg & ~0x100;
1457 else
1458 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1459 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1460 spin_unlock_irq(&chip->reg_lock);
1461 return reg != old_reg;
1462}
1463
Takashi Iwai208a1b42005-11-17 14:53:41 +01001464static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1466 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1467 .name = "Direct Recording Source",
1468 .info = snd_ymfpci_drec_source_info,
1469 .get = snd_ymfpci_drec_source_get,
1470 .put = snd_ymfpci_drec_source_put
1471};
1472
1473/*
1474 * Mixer controls
1475 */
1476
Glen Masgaid602c882005-09-28 08:19:05 +02001477#define YMFPCI_SINGLE(xname, xindex, reg, shift) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1479 .info = snd_ymfpci_info_single, \
1480 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
Glen Masgaid602c882005-09-28 08:19:05 +02001481 .private_value = ((reg) | ((shift) << 16)) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001483#define snd_ymfpci_info_single snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Takashi Iwai208a1b42005-11-17 14:53:41 +01001485static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1486 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001488 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001489 int reg = kcontrol->private_value & 0xffff;
1490 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1491 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Glen Masgaid602c882005-09-28 08:19:05 +02001493 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 case YDSXGR_SPDIFOUTCTRL: break;
1495 case YDSXGR_SPDIFINCTRL: break;
1496 default: return -EINVAL;
1497 }
Glen Masgaid602c882005-09-28 08:19:05 +02001498 ucontrol->value.integer.value[0] =
1499 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return 0;
1501}
1502
Takashi Iwai208a1b42005-11-17 14:53:41 +01001503static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1504 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001506 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c882005-09-28 08:19:05 +02001507 int reg = kcontrol->private_value & 0xffff;
1508 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1509 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int change;
1511 unsigned int val, oval;
1512
Glen Masgaid602c882005-09-28 08:19:05 +02001513 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 case YDSXGR_SPDIFOUTCTRL: break;
1515 case YDSXGR_SPDIFINCTRL: break;
1516 default: return -EINVAL;
1517 }
1518 val = (ucontrol->value.integer.value[0] & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 val <<= shift;
1520 spin_lock_irq(&chip->reg_lock);
1521 oval = snd_ymfpci_readl(chip, reg);
1522 val = (oval & ~(mask << shift)) | val;
1523 change = val != oval;
1524 snd_ymfpci_writel(chip, reg, val);
1525 spin_unlock_irq(&chip->reg_lock);
1526 return change;
1527}
1528
Takashi Iwai0cb29ea2007-01-29 15:33:49 +01001529static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
Takashi Iwai33925182006-08-28 13:29:42 +02001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531#define YMFPCI_DOUBLE(xname, xindex, reg) \
1532{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Takashi Iwai33925182006-08-28 13:29:42 +02001533 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 .info = snd_ymfpci_info_double, \
1535 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
Takashi Iwai33925182006-08-28 13:29:42 +02001536 .private_value = reg, \
1537 .tlv = { .p = db_scale_native } }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Takashi Iwai208a1b42005-11-17 14:53:41 +01001539static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 unsigned int reg = kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 if (reg < 0x80 || reg >= 0xc0)
1544 return -EINVAL;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001545 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 uinfo->count = 2;
1547 uinfo->value.integer.min = 0;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001548 uinfo->value.integer.max = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return 0;
1550}
1551
Takashi Iwai208a1b42005-11-17 14:53:41 +01001552static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001554 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001556 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 unsigned int val;
1558
1559 if (reg < 0x80 || reg >= 0xc0)
1560 return -EINVAL;
1561 spin_lock_irq(&chip->reg_lock);
1562 val = snd_ymfpci_readl(chip, reg);
1563 spin_unlock_irq(&chip->reg_lock);
1564 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1565 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return 0;
1567}
1568
Takashi Iwai208a1b42005-11-17 14:53:41 +01001569static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001571 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001573 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 int change;
1575 unsigned int val1, val2, oval;
1576
1577 if (reg < 0x80 || reg >= 0xc0)
1578 return -EINVAL;
1579 val1 = ucontrol->value.integer.value[0] & mask;
1580 val2 = ucontrol->value.integer.value[1] & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 val1 <<= shift_left;
1582 val2 <<= shift_right;
1583 spin_lock_irq(&chip->reg_lock);
1584 oval = snd_ymfpci_readl(chip, reg);
1585 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1586 change = val1 != oval;
1587 snd_ymfpci_writel(chip, reg, val1);
1588 spin_unlock_irq(&chip->reg_lock);
1589 return change;
1590}
1591
Clemens Ladisch177a7cd2007-07-23 17:38:44 +02001592static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1593 struct snd_ctl_elem_value *ucontrol)
1594{
1595 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1596 unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1597 unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1598 int change;
1599 unsigned int value, oval;
1600
1601 value = ucontrol->value.integer.value[0] & 0x3fff;
1602 value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1603 spin_lock_irq(&chip->reg_lock);
1604 oval = snd_ymfpci_readl(chip, reg);
1605 change = value != oval;
1606 snd_ymfpci_writel(chip, reg, value);
1607 snd_ymfpci_writel(chip, reg2, value);
1608 spin_unlock_irq(&chip->reg_lock);
1609 return change;
1610}
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612/*
1613 * 4ch duplication
1614 */
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001615#define snd_ymfpci_info_dup4ch snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Takashi Iwai208a1b42005-11-17 14:53:41 +01001617static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001619 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1621 return 0;
1622}
1623
Takashi Iwai208a1b42005-11-17 14:53:41 +01001624static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001626 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 int change;
1628 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1629 if (change)
1630 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1631 return change;
1632}
1633
Raymond Yau4d20bb12012-01-17 11:41:47 +08001634static struct snd_kcontrol_new snd_ymfpci_dup4ch __devinitdata = {
1635 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1636 .name = "4ch Duplication",
1637 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1638 .info = snd_ymfpci_info_dup4ch,
1639 .get = snd_ymfpci_get_dup4ch,
1640 .put = snd_ymfpci_put_dup4ch,
1641};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Takashi Iwai208a1b42005-11-17 14:53:41 +01001643static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
Clemens Ladisch177a7cd2007-07-23 17:38:44 +02001644{
1645 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1646 .name = "Wave Playback Volume",
1647 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1648 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1649 .info = snd_ymfpci_info_double,
1650 .get = snd_ymfpci_get_double,
1651 .put = snd_ymfpci_put_nativedacvol,
1652 .private_value = YDSXGR_NATIVEDACOUTVOL,
1653 .tlv = { .p = db_scale_native },
1654},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1656YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1657YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1658YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1659YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1660YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1661YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
Raymond Yau89f33252011-09-09 19:15:01 +08001662YMFPCI_DOUBLE("FM Legacy Playback Volume", 0, YDSXGR_LEGACYOUTVOL),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1664YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1665YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1666YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
Glen Masgaid602c882005-09-28 08:19:05 +02001667YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1668YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1669YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670};
1671
1672
1673/*
1674 * GPIO
1675 */
1676
Takashi Iwai208a1b42005-11-17 14:53:41 +01001677static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 u16 reg, mode;
1680 unsigned long flags;
1681
1682 spin_lock_irqsave(&chip->reg_lock, flags);
1683 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1684 reg &= ~(1 << (pin + 8));
1685 reg |= (1 << pin);
1686 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1687 /* set the level mode for input line */
1688 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1689 mode &= ~(3 << (pin * 2));
1690 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1691 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1692 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1693 spin_unlock_irqrestore(&chip->reg_lock, flags);
1694 return (mode >> pin) & 1;
1695}
1696
Takashi Iwai208a1b42005-11-17 14:53:41 +01001697static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 u16 reg;
1700 unsigned long flags;
1701
1702 spin_lock_irqsave(&chip->reg_lock, flags);
1703 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1704 reg &= ~(1 << pin);
1705 reg &= ~(1 << (pin + 8));
1706 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1707 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1708 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1709 spin_unlock_irqrestore(&chip->reg_lock, flags);
1710
1711 return 0;
1712}
1713
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001714#define snd_ymfpci_gpio_sw_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Takashi Iwai208a1b42005-11-17 14:53:41 +01001716static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001718 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 int pin = (int)kcontrol->private_value;
1720 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1721 return 0;
1722}
1723
Takashi Iwai208a1b42005-11-17 14:53:41 +01001724static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001726 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int pin = (int)kcontrol->private_value;
1728
1729 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1730 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1731 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1732 return 1;
1733 }
1734 return 0;
1735}
1736
Takashi Iwai208a1b42005-11-17 14:53:41 +01001737static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 .name = "Shared Rear/Line-In Switch",
1739 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1740 .info = snd_ymfpci_gpio_sw_info,
1741 .get = snd_ymfpci_gpio_sw_get,
1742 .put = snd_ymfpci_gpio_sw_put,
1743 .private_value = 2,
1744};
1745
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001746/*
1747 * PCM voice volume
1748 */
1749
Takashi Iwai208a1b42005-11-17 14:53:41 +01001750static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1751 struct snd_ctl_elem_info *uinfo)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001752{
1753 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1754 uinfo->count = 2;
1755 uinfo->value.integer.min = 0;
1756 uinfo->value.integer.max = 0x8000;
1757 return 0;
1758}
1759
Takashi Iwai208a1b42005-11-17 14:53:41 +01001760static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1761 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001762{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001763 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001764 unsigned int subs = kcontrol->id.subdevice;
1765
1766 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1767 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1768 return 0;
1769}
1770
Takashi Iwai208a1b42005-11-17 14:53:41 +01001771static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1772 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001773{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001774 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001775 unsigned int subs = kcontrol->id.subdevice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001776 struct snd_pcm_substream *substream;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001777 unsigned long flags;
1778
1779 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1780 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1781 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1782 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001783 if (chip->pcm_mixer[subs].left > 0x8000)
1784 chip->pcm_mixer[subs].left = 0x8000;
1785 if (chip->pcm_mixer[subs].right > 0x8000)
1786 chip->pcm_mixer[subs].right = 0x8000;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001787
Takashi Iwai208a1b42005-11-17 14:53:41 +01001788 substream = (struct snd_pcm_substream *)kcontrol->private_value;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001789 spin_lock_irqsave(&chip->voice_lock, flags);
1790 if (substream->runtime && substream->runtime->private_data) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01001791 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01001792 if (!ypcm->use_441_slot)
1793 ypcm->update_pcm_vol = 2;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001794 }
1795 spin_unlock_irqrestore(&chip->voice_lock, flags);
1796 return 1;
1797 }
1798 return 0;
1799}
1800
Takashi Iwai208a1b42005-11-17 14:53:41 +01001801static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001802 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1803 .name = "PCM Playback Volume",
1804 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1805 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1806 .info = snd_ymfpci_pcm_vol_info,
1807 .get = snd_ymfpci_pcm_vol_get,
1808 .put = snd_ymfpci_pcm_vol_put,
1809};
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812/*
1813 * Mixer routines
1814 */
1815
Takashi Iwai208a1b42005-11-17 14:53:41 +01001816static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001818 struct snd_ymfpci *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 chip->ac97_bus = NULL;
1820}
1821
Takashi Iwai208a1b42005-11-17 14:53:41 +01001822static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001824 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 chip->ac97 = NULL;
1826}
1827
Glen Masgaid9301262006-10-10 09:27:19 +02001828int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001830 struct snd_ac97_template ac97;
1831 struct snd_kcontrol *kctl;
1832 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 unsigned int idx;
1834 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001835 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 .write = snd_ymfpci_codec_write,
1837 .read = snd_ymfpci_codec_read,
1838 };
1839
1840 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1841 return err;
1842 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1843 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1844
1845 memset(&ac97, 0, sizeof(ac97));
1846 ac97.private_data = chip;
1847 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1848 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1849 return err;
1850
1851 /* to be sure */
1852 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1853 AC97_EA_VRA|AC97_EA_VRM, 0);
1854
1855 for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1856 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1857 return err;
1858 }
Raymond Yau4d20bb12012-01-17 11:41:47 +08001859 if (chip->ac97->ext_id & AC97_EI_SDAC) {
1860 kctl = snd_ctl_new1(&snd_ymfpci_dup4ch, chip);
1861 err = snd_ctl_add(chip->card, kctl);
1862 if (err < 0)
1863 return err;
1864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 /* add S/PDIF control */
Takashi Iwaida3cec32008-08-08 17:12:14 +02001867 if (snd_BUG_ON(!chip->pcm_spdif))
1868 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1870 return err;
1871 kctl->id.device = chip->pcm_spdif->device;
1872 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1873 return err;
1874 kctl->id.device = chip->pcm_spdif->device;
1875 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1876 return err;
1877 kctl->id.device = chip->pcm_spdif->device;
1878 chip->spdif_pcm_ctl = kctl;
1879
1880 /* direct recording source */
1881 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1882 (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1883 return err;
1884
1885 /*
1886 * shared rear/line-in
1887 */
1888 if (rear_switch) {
1889 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1890 return err;
1891 }
1892
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001893 /* per-voice volume */
1894 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1895 for (idx = 0; idx < 32; ++idx) {
1896 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1897 if (!kctl)
1898 return -ENOMEM;
1899 kctl->id.device = chip->pcm->device;
1900 kctl->id.subdevice = idx;
1901 kctl->private_value = (unsigned long)substream;
1902 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1903 return err;
1904 chip->pcm_mixer[idx].left = 0x8000;
1905 chip->pcm_mixer[idx].right = 0x8000;
1906 chip->pcm_mixer[idx].ctl = kctl;
1907 substream = substream->next;
1908 }
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return 0;
1911}
1912
1913
1914/*
1915 * timer
1916 */
1917
Takashi Iwai208a1b42005-11-17 14:53:41 +01001918static int snd_ymfpci_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001920 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 unsigned long flags;
1922 unsigned int count;
1923
1924 chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 spin_lock_irqsave(&chip->reg_lock, flags);
Clemens Ladisch6e2efaa2009-08-10 10:06:53 +02001926 if (timer->sticks > 1) {
1927 chip->timer_ticks = timer->sticks;
1928 count = timer->sticks - 1;
1929 } else {
1930 /*
1931 * Divisor 1 is not allowed; fake it by using divisor 2 and
1932 * counting two ticks for each interrupt.
1933 */
1934 chip->timer_ticks = 2;
1935 count = 2 - 1;
1936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1938 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1939 spin_unlock_irqrestore(&chip->reg_lock, flags);
1940 return 0;
1941}
1942
Takashi Iwai208a1b42005-11-17 14:53:41 +01001943static int snd_ymfpci_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001945 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 unsigned long flags;
1947
1948 chip = snd_timer_chip(timer);
1949 spin_lock_irqsave(&chip->reg_lock, flags);
1950 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1951 spin_unlock_irqrestore(&chip->reg_lock, flags);
1952 return 0;
1953}
1954
Takashi Iwai208a1b42005-11-17 14:53:41 +01001955static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 unsigned long *num, unsigned long *den)
1957{
1958 *num = 1;
Clemens Ladisch6e2efaa2009-08-10 10:06:53 +02001959 *den = 96000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return 0;
1961}
1962
Takashi Iwai208a1b42005-11-17 14:53:41 +01001963static struct snd_timer_hardware snd_ymfpci_timer_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 .flags = SNDRV_TIMER_HW_AUTO,
Clemens Ladisch6e2efaa2009-08-10 10:06:53 +02001965 .resolution = 10417, /* 1 / 96 kHz = 10.41666...us */
1966 .ticks = 0x10000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 .start = snd_ymfpci_timer_start,
1968 .stop = snd_ymfpci_timer_stop,
1969 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1970};
1971
Takashi Iwai208a1b42005-11-17 14:53:41 +01001972int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001974 struct snd_timer *timer = NULL;
1975 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 int err;
1977
1978 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1979 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1980 tid.card = chip->card->number;
1981 tid.device = device;
1982 tid.subdevice = 0;
1983 if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1984 strcpy(timer->name, "YMFPCI timer");
1985 timer->private_data = chip;
1986 timer->hw = snd_ymfpci_timer_hw;
1987 }
1988 chip->timer = timer;
1989 return err;
1990}
1991
1992
1993/*
1994 * proc interface
1995 */
1996
Takashi Iwai208a1b42005-11-17 14:53:41 +01001997static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1998 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002000 struct snd_ymfpci *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 int i;
2002
2003 snd_iprintf(buffer, "YMFPCI\n\n");
2004 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
2005 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
2006}
2007
Takashi Iwai208a1b42005-11-17 14:53:41 +01002008static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002010 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 if (! snd_card_proc_new(card, "ymfpci", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002013 snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return 0;
2015}
2016
2017/*
2018 * initialization routines
2019 */
2020
2021static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
2022{
2023 u8 cmd;
2024
2025 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
2026#if 0 // force to reset
2027 if (cmd & 0x03) {
2028#endif
2029 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
2030 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
2031 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
2032 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
2033 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
2034#if 0
2035 }
2036#endif
2037}
2038
Takashi Iwai208a1b42005-11-17 14:53:41 +01002039static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
2041 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
2042}
2043
Takashi Iwai208a1b42005-11-17 14:53:41 +01002044static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 u32 val;
2047 int timeout = 1000;
2048
2049 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
2050 if (val)
2051 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
2052 while (timeout-- > 0) {
2053 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
2054 if ((val & 0x00000002) == 0)
2055 break;
2056 }
2057}
2058
Clemens Ladisch102fa902006-10-11 12:05:59 +02002059static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2060{
2061 int err, is_1e;
2062 const char *name;
2063
2064 err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2065 &chip->pci->dev);
2066 if (err >= 0) {
David Woodhouseb82a82d2008-05-29 14:40:00 +03002067 if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
Clemens Ladisch102fa902006-10-11 12:05:59 +02002068 snd_printk(KERN_ERR "DSP microcode has wrong size\n");
2069 err = -EINVAL;
2070 }
2071 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002072 if (err < 0)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002073 return err;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002074 is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2075 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2076 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2077 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2078 name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2079 err = request_firmware(&chip->controller_microcode, name,
2080 &chip->pci->dev);
2081 if (err >= 0) {
David Woodhouseb82a82d2008-05-29 14:40:00 +03002082 if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
Clemens Ladisch102fa902006-10-11 12:05:59 +02002083 snd_printk(KERN_ERR "controller microcode"
2084 " has wrong size\n");
2085 err = -EINVAL;
2086 }
2087 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002088 if (err < 0)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002089 return err;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002090 return 0;
2091}
Clemens Ladisch7e0af292007-05-03 17:59:54 +02002092
2093MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
2094MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
2095MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
2096
Takashi Iwai208a1b42005-11-17 14:53:41 +01002097static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 int i;
2100 u16 ctrl;
David Woodhouseb82a82d2008-05-29 14:40:00 +03002101 const __le32 *inst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2104 snd_ymfpci_disable_dsp(chip);
2105 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2106 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2107 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2108 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2109 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2110 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2111 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2112 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2113 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2114
2115 /* setup DSP instruction code */
David Woodhouseb82a82d2008-05-29 14:40:00 +03002116 inst = (const __le32 *)chip->dsp_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
David Woodhouseb82a82d2008-05-29 14:40:00 +03002118 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2119 le32_to_cpu(inst[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 /* setup control instruction code */
David Woodhouseb82a82d2008-05-29 14:40:00 +03002122 inst = (const __le32 *)chip->controller_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
David Woodhouseb82a82d2008-05-29 14:40:00 +03002124 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2125 le32_to_cpu(inst[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 snd_ymfpci_enable_dsp(chip);
2128}
2129
Takashi Iwai208a1b42005-11-17 14:53:41 +01002130static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 long size, playback_ctrl_size;
2133 int voice, bank, reg;
2134 u8 *ptr;
2135 dma_addr_t ptr_addr;
2136
2137 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2138 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2139 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2140 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2141 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2142
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002143 size = ALIGN(playback_ctrl_size, 0x100) +
2144 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2145 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2146 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 chip->work_size;
2148 /* work_ptr must be aligned to 256 bytes, but it's already
2149 covered with the kernel page allocation mechanism */
2150 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2151 size, &chip->work_ptr) < 0)
2152 return -ENOMEM;
2153 ptr = chip->work_ptr.area;
2154 ptr_addr = chip->work_ptr.addr;
2155 memset(ptr, 0, size); /* for sure */
2156
2157 chip->bank_base_playback = ptr;
2158 chip->bank_base_playback_addr = ptr_addr;
2159 chip->ctrl_playback = (u32 *)ptr;
2160 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002161 ptr += ALIGN(playback_ctrl_size, 0x100);
2162 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2164 chip->voices[voice].number = voice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002165 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 chip->voices[voice].bank_addr = ptr_addr;
2167 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002168 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 ptr += chip->bank_size_playback;
2170 ptr_addr += chip->bank_size_playback;
2171 }
2172 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002173 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2174 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 chip->bank_base_capture = ptr;
2176 chip->bank_base_capture_addr = ptr_addr;
2177 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2178 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002179 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 ptr += chip->bank_size_capture;
2181 ptr_addr += chip->bank_size_capture;
2182 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002183 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2184 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 chip->bank_base_effect = ptr;
2186 chip->bank_base_effect_addr = ptr_addr;
2187 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2188 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002189 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 ptr += chip->bank_size_effect;
2191 ptr_addr += chip->bank_size_effect;
2192 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002193 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2194 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 chip->work_base = ptr;
2196 chip->work_base_addr = ptr_addr;
2197
Takashi Iwaida3cec32008-08-08 17:12:14 +02002198 snd_BUG_ON(ptr + chip->work_size !=
2199 chip->work_ptr.area + chip->work_ptr.bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2202 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2203 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2204 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2205 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2206
2207 /* S/PDIF output initialization */
2208 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2209 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2210 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2211
2212 /* S/PDIF input initialization */
2213 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2214
2215 /* digital mixer setup */
2216 for (reg = 0x80; reg < 0xc0; reg += 4)
2217 snd_ymfpci_writel(chip, reg, 0);
2218 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
Takashi Iwai4a3b6982008-06-25 17:17:00 +02002219 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2221 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2222 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2223 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2224 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2225 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2226
2227 return 0;
2228}
2229
Takashi Iwai208a1b42005-11-17 14:53:41 +01002230static int snd_ymfpci_free(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
2232 u16 ctrl;
2233
Takashi Iwaida3cec32008-08-08 17:12:14 +02002234 if (snd_BUG_ON(!chip))
2235 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 if (chip->res_reg_area) { /* don't touch busy hardware */
2238 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2239 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2240 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2241 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2242 snd_ymfpci_disable_dsp(chip);
2243 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2244 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2245 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2246 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2247 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2248 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2249 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2250 }
2251
2252 snd_ymfpci_ac3_done(chip);
2253
2254 /* Set PCI device to D3 state */
2255#if 0
2256 /* FIXME: temporarily disabled, otherwise we cannot fire up
2257 * the chip again unless reboot. ACPI bug?
2258 */
2259 pci_set_power_state(chip->pci, 3);
2260#endif
2261
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002262#ifdef CONFIG_PM_SLEEP
Takashi Iwai7009fa52012-11-22 16:23:22 +01002263 kfree(chip->saved_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264#endif
Takashi Iwai95866d382008-03-22 10:11:08 +01002265 if (chip->irq >= 0)
2266 free_irq(chip->irq, chip);
Takashi Iwaib1d57762005-10-10 11:56:31 +02002267 release_and_free_resource(chip->mpu_res);
2268 release_and_free_resource(chip->fm_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 snd_ymfpci_free_gameport(chip);
2270 if (chip->reg_area_virt)
2271 iounmap(chip->reg_area_virt);
2272 if (chip->work_ptr.area)
2273 snd_dma_free_pages(&chip->work_ptr);
2274
Takashi Iwaib1d57762005-10-10 11:56:31 +02002275 release_and_free_resource(chip->res_reg_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2278
2279 pci_disable_device(chip->pci);
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002280 release_firmware(chip->dsp_microcode);
2281 release_firmware(chip->controller_microcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 kfree(chip);
2283 return 0;
2284}
2285
Takashi Iwai208a1b42005-11-17 14:53:41 +01002286static int snd_ymfpci_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002288 struct snd_ymfpci *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return snd_ymfpci_free(chip);
2290}
2291
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002292#ifdef CONFIG_PM_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293static int saved_regs_index[] = {
2294 /* spdif */
2295 YDSXGR_SPDIFOUTCTRL,
2296 YDSXGR_SPDIFOUTSTATUS,
2297 YDSXGR_SPDIFINCTRL,
2298 /* volumes */
2299 YDSXGR_PRIADCLOOPVOL,
2300 YDSXGR_NATIVEDACINVOL,
2301 YDSXGR_NATIVEDACOUTVOL,
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01002302 YDSXGR_BUF441OUTVOL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 YDSXGR_NATIVEADCINVOL,
2304 YDSXGR_SPDIFLOOPVOL,
2305 YDSXGR_SPDIFOUTVOL,
2306 YDSXGR_ZVOUTVOL,
2307 YDSXGR_LEGACYOUTVOL,
2308 /* address bases */
2309 YDSXGR_PLAYCTRLBASE,
2310 YDSXGR_RECCTRLBASE,
2311 YDSXGR_EFFCTRLBASE,
2312 YDSXGR_WORKBASE,
2313 /* capture set up */
2314 YDSXGR_MAPOFREC,
2315 YDSXGR_RECFORMAT,
2316 YDSXGR_RECSLOTSR,
2317 YDSXGR_ADCFORMAT,
2318 YDSXGR_ADCSLOTSR,
2319};
2320#define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
2321
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002322static int snd_ymfpci_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002324 struct pci_dev *pci = to_pci_dev(dev);
2325 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwaided46232005-11-17 16:09:43 +01002326 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 unsigned int i;
2328
Takashi Iwaided46232005-11-17 16:09:43 +01002329 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 snd_pcm_suspend_all(chip->pcm);
2331 snd_pcm_suspend_all(chip->pcm2);
2332 snd_pcm_suspend_all(chip->pcm_spdif);
2333 snd_pcm_suspend_all(chip->pcm_4ch);
2334 snd_ac97_suspend(chip->ac97);
2335 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2336 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2337 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
Takashi Iwai28aa1652012-03-13 08:07:41 +01002338 pci_read_config_word(chip->pci, PCIR_DSXG_LEGACY,
2339 &chip->saved_dsxg_legacy);
2340 pci_read_config_word(chip->pci, PCIR_DSXG_ELEGACY,
2341 &chip->saved_dsxg_elegacy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
Takashi Iwai4a3b6982008-06-25 17:17:00 +02002343 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 snd_ymfpci_disable_dsp(chip);
Takashi Iwaided46232005-11-17 16:09:43 +01002345 pci_disable_device(pci);
2346 pci_save_state(pci);
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002347 pci_set_power_state(pci, PCI_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return 0;
2349}
2350
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002351static int snd_ymfpci_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002353 struct pci_dev *pci = to_pci_dev(dev);
2354 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwaided46232005-11-17 16:09:43 +01002355 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 unsigned int i;
2357
Takashi Iwai30b35392006-10-11 18:52:53 +02002358 pci_set_power_state(pci, PCI_D0);
Takashi Iwaided46232005-11-17 16:09:43 +01002359 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002360 if (pci_enable_device(pci) < 0) {
2361 printk(KERN_ERR "ymfpci: pci_enable_device failed, "
2362 "disabling device\n");
2363 snd_card_disconnect(card);
2364 return -EIO;
2365 }
Takashi Iwaided46232005-11-17 16:09:43 +01002366 pci_set_master(pci);
2367 snd_ymfpci_aclink_reset(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 snd_ymfpci_codec_ready(chip, 0);
2369 snd_ymfpci_download_image(chip);
2370 udelay(100);
2371
2372 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2373 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2374
2375 snd_ac97_resume(chip->ac97);
2376
Takashi Iwai28aa1652012-03-13 08:07:41 +01002377 pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY,
2378 chip->saved_dsxg_legacy);
2379 pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY,
2380 chip->saved_dsxg_elegacy);
2381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 /* start hw again */
2383 if (chip->start_count > 0) {
2384 spin_lock_irq(&chip->reg_lock);
2385 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2386 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2387 spin_unlock_irq(&chip->reg_lock);
2388 }
Takashi Iwaided46232005-11-17 16:09:43 +01002389 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return 0;
2391}
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002392
2393SIMPLE_DEV_PM_OPS(snd_ymfpci_pm, snd_ymfpci_suspend, snd_ymfpci_resume);
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002394#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Takashi Iwai208a1b42005-11-17 14:53:41 +01002396int __devinit snd_ymfpci_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 struct pci_dev * pci,
2398 unsigned short old_legacy_ctrl,
Takashi Iwai208a1b42005-11-17 14:53:41 +01002399 struct snd_ymfpci ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002401 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002403 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 .dev_free = snd_ymfpci_dev_free,
2405 };
2406
2407 *rchip = NULL;
2408
2409 /* enable PCI device */
2410 if ((err = pci_enable_device(pci)) < 0)
2411 return err;
2412
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002413 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if (chip == NULL) {
2415 pci_disable_device(pci);
2416 return -ENOMEM;
2417 }
2418 chip->old_legacy_ctrl = old_legacy_ctrl;
2419 spin_lock_init(&chip->reg_lock);
2420 spin_lock_init(&chip->voice_lock);
2421 init_waitqueue_head(&chip->interrupt_sleep);
2422 atomic_set(&chip->interrupt_sleep_count, 0);
2423 chip->card = card;
2424 chip->pci = pci;
2425 chip->irq = -1;
2426 chip->device_id = pci->device;
Auke Kok44c10132007-06-08 15:46:36 -07002427 chip->rev = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 chip->reg_area_phys = pci_resource_start(pci, 0);
2429 chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2430 pci_set_master(pci);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01002431 chip->src441_used = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002434 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 -07002435 snd_ymfpci_free(chip);
2436 return -EBUSY;
2437 }
Takashi Iwai437a5a42006-11-21 12:14:23 +01002438 if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002439 KBUILD_MODNAME, chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002440 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 snd_ymfpci_free(chip);
2442 return -EBUSY;
2443 }
2444 chip->irq = pci->irq;
2445
2446 snd_ymfpci_aclink_reset(pci);
2447 if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2448 snd_ymfpci_free(chip);
2449 return -EIO;
2450 }
2451
Clemens Ladisch102fa902006-10-11 12:05:59 +02002452 err = snd_ymfpci_request_firmware(chip);
2453 if (err < 0) {
2454 snd_printk(KERN_ERR "firmware request failed: %d\n", err);
2455 snd_ymfpci_free(chip);
2456 return err;
2457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 snd_ymfpci_download_image(chip);
2459
2460 udelay(100); /* seems we need a delay after downloading image.. */
2461
2462 if (snd_ymfpci_memalloc(chip) < 0) {
2463 snd_ymfpci_free(chip);
2464 return -EIO;
2465 }
2466
2467 if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
2468 snd_ymfpci_free(chip);
2469 return err;
2470 }
2471
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002472#ifdef CONFIG_PM_SLEEP
Takashi Iwai7009fa52012-11-22 16:23:22 +01002473 chip->saved_regs = kmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32),
2474 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (chip->saved_regs == NULL) {
2476 snd_ymfpci_free(chip);
2477 return -ENOMEM;
2478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479#endif
2480
2481 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2482 snd_ymfpci_free(chip);
2483 return err;
2484 }
2485
2486 snd_ymfpci_proc_init(card, chip);
2487
2488 snd_card_set_dev(card, &pci->dev);
2489
2490 *rchip = chip;
2491 return 0;
2492}