blob: 748bbdcb2c528296414cd07763fbbc6dc3d3a13d [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Routines for control of YMF724/740/744/754 chips
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/delay.h>
Clemens Ladisch102fa902006-10-11 12:05:59 +02008#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/init.h>
10#include <linux/interrupt.h>
11#include <linux/pci.h>
12#include <linux/sched.h>
13#include <linux/slab.h>
David Woodhouseb82a82d2008-05-29 14:40:00 +030014#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040015#include <linux/module.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010016#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <sound/core.h>
19#include <sound/control.h>
20#include <sound/info.h>
Takashi Iwai33925182006-08-28 13:29:42 +020021#include <sound/tlv.h>
Takashi Iwai81fcb172012-07-02 16:37:05 +020022#include "ymfpci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <sound/asoundef.h>
24#include <sound/mpu401.h>
25
Clemens Ladisch102fa902006-10-11 12:05:59 +020026#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * common I/O routines
30 */
31
Takashi Iwai208a1b42005-11-17 14:53:41 +010032static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Takashi Iwai208a1b42005-11-17 14:53:41 +010034static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 return readb(chip->reg_area_virt + offset);
37}
38
Takashi Iwai208a1b42005-11-17 14:53:41 +010039static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 writeb(val, chip->reg_area_virt + offset);
42}
43
Takashi Iwai208a1b42005-11-17 14:53:41 +010044static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 return readw(chip->reg_area_virt + offset);
47}
48
Takashi Iwai208a1b42005-11-17 14:53:41 +010049static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 writew(val, chip->reg_area_virt + offset);
52}
53
Takashi Iwai208a1b42005-11-17 14:53:41 +010054static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 return readl(chip->reg_area_virt + offset);
57}
58
Takashi Iwai208a1b42005-11-17 14:53:41 +010059static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 writel(val, chip->reg_area_virt + offset);
62}
63
Takashi Iwai208a1b42005-11-17 14:53:41 +010064static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020066 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
68
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020069 end_time = jiffies + msecs_to_jiffies(750);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 do {
71 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
72 return 0;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +020073 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +020074 } while (time_before(jiffies, end_time));
Takashi Iwai6436bcf2014-02-26 12:18:27 +010075 dev_err(chip->card->dev,
76 "codec_ready: codec %i is not ready [0x%x]\n",
77 secondary, snd_ymfpci_readw(chip, reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -EBUSY;
79}
80
Takashi Iwai208a1b42005-11-17 14:53:41 +010081static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Takashi Iwai208a1b42005-11-17 14:53:41 +010083 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 u32 cmd;
85
86 snd_ymfpci_codec_ready(chip, 0);
87 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
88 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
89}
90
Takashi Iwai208a1b42005-11-17 14:53:41 +010091static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Takashi Iwai208a1b42005-11-17 14:53:41 +010093 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 if (snd_ymfpci_codec_ready(chip, 0))
96 return ~0;
97 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
98 if (snd_ymfpci_codec_ready(chip, 0))
99 return ~0;
100 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
101 int i;
102 for (i = 0; i < 600; i++)
103 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
104 }
105 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
106}
107
108/*
109 * Misc routines
110 */
111
112static u32 snd_ymfpci_calc_delta(u32 rate)
113{
114 switch (rate) {
115 case 8000: return 0x02aaab00;
116 case 11025: return 0x03accd00;
117 case 16000: return 0x05555500;
118 case 22050: return 0x07599a00;
119 case 32000: return 0x0aaaab00;
120 case 44100: return 0x0eb33300;
121 default: return ((rate << 16) / 375) << 5;
122 }
123}
124
125static u32 def_rate[8] = {
126 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
127};
128
129static u32 snd_ymfpci_calc_lpfK(u32 rate)
130{
131 u32 i;
132 static u32 val[8] = {
133 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
134 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
135 };
136
137 if (rate == 44100)
138 return 0x40000000; /* FIXME: What's the right value? */
139 for (i = 0; i < 8; i++)
140 if (rate <= def_rate[i])
141 return val[i];
142 return val[0];
143}
144
145static u32 snd_ymfpci_calc_lpfQ(u32 rate)
146{
147 u32 i;
148 static u32 val[8] = {
149 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
150 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
151 };
152
153 if (rate == 44100)
154 return 0x370A0000;
155 for (i = 0; i < 8; i++)
156 if (rate <= def_rate[i])
157 return val[i];
158 return val[0];
159}
160
161/*
162 * Hardware start management
163 */
164
Takashi Iwai208a1b42005-11-17 14:53:41 +0100165static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 unsigned long flags;
168
169 spin_lock_irqsave(&chip->reg_lock, flags);
170 if (chip->start_count++ > 0)
171 goto __end;
172 snd_ymfpci_writel(chip, YDSXGR_MODE,
173 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
174 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
175 __end:
176 spin_unlock_irqrestore(&chip->reg_lock, flags);
177}
178
Takashi Iwai208a1b42005-11-17 14:53:41 +0100179static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 unsigned long flags;
182 long timeout = 1000;
183
184 spin_lock_irqsave(&chip->reg_lock, flags);
185 if (--chip->start_count > 0)
186 goto __end;
187 snd_ymfpci_writel(chip, YDSXGR_MODE,
188 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
189 while (timeout-- > 0) {
190 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
191 break;
192 }
193 if (atomic_read(&chip->interrupt_sleep_count)) {
194 atomic_set(&chip->interrupt_sleep_count, 0);
195 wake_up(&chip->interrupt_sleep);
196 }
197 __end:
198 spin_unlock_irqrestore(&chip->reg_lock, flags);
199}
200
201/*
202 * Playback voice management
203 */
204
Takashi Iwai208a1b42005-11-17 14:53:41 +0100205static int voice_alloc(struct snd_ymfpci *chip,
206 enum snd_ymfpci_voice_type type, int pair,
207 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100209 struct snd_ymfpci_voice *voice, *voice2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 int idx;
211
212 *rvoice = NULL;
213 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
214 voice = &chip->voices[idx];
215 voice2 = pair ? &chip->voices[idx+1] : NULL;
216 if (voice->use || (voice2 && voice2->use))
217 continue;
218 voice->use = 1;
219 if (voice2)
220 voice2->use = 1;
221 switch (type) {
222 case YMFPCI_PCM:
223 voice->pcm = 1;
224 if (voice2)
225 voice2->pcm = 1;
226 break;
227 case YMFPCI_SYNTH:
228 voice->synth = 1;
229 break;
230 case YMFPCI_MIDI:
231 voice->midi = 1;
232 break;
233 }
234 snd_ymfpci_hw_start(chip);
235 if (voice2)
236 snd_ymfpci_hw_start(chip);
237 *rvoice = voice;
238 return 0;
239 }
240 return -ENOMEM;
241}
242
Takashi Iwai208a1b42005-11-17 14:53:41 +0100243static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
244 enum snd_ymfpci_voice_type type, int pair,
245 struct snd_ymfpci_voice **rvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 unsigned long flags;
248 int result;
249
Takashi Iwaida3cec32008-08-08 17:12:14 +0200250 if (snd_BUG_ON(!rvoice))
251 return -EINVAL;
252 if (snd_BUG_ON(pair && type != YMFPCI_PCM))
253 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 spin_lock_irqsave(&chip->voice_lock, flags);
256 for (;;) {
257 result = voice_alloc(chip, type, pair, rvoice);
258 if (result == 0 || type != YMFPCI_PCM)
259 break;
260 /* TODO: synth/midi voice deallocation */
261 break;
262 }
263 spin_unlock_irqrestore(&chip->voice_lock, flags);
264 return result;
265}
266
Takashi Iwai208a1b42005-11-17 14:53:41 +0100267static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 unsigned long flags;
270
Takashi Iwaida3cec32008-08-08 17:12:14 +0200271 if (snd_BUG_ON(!pvoice))
272 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 snd_ymfpci_hw_stop(chip);
274 spin_lock_irqsave(&chip->voice_lock, flags);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100275 if (pvoice->number == chip->src441_used) {
276 chip->src441_used = -1;
277 pvoice->ypcm->use_441_slot = 0;
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
280 pvoice->ypcm = NULL;
281 pvoice->interrupt = NULL;
282 spin_unlock_irqrestore(&chip->voice_lock, flags);
283 return 0;
284}
285
286/*
287 * PCM part
288 */
289
Takashi Iwai208a1b42005-11-17 14:53:41 +0100290static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100292 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 u32 pos, delta;
294
295 if ((ypcm = voice->ypcm) == NULL)
296 return;
297 if (ypcm->substream == NULL)
298 return;
299 spin_lock(&chip->reg_lock);
300 if (ypcm->running) {
301 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
302 if (pos < ypcm->last_pos)
303 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
304 else
305 delta = pos - ypcm->last_pos;
306 ypcm->period_pos += delta;
307 ypcm->last_pos = pos;
308 if (ypcm->period_pos >= ypcm->period_size) {
Takashi Iwaiee419652009-02-05 16:11:31 +0100309 /*
Takashi Iwai6436bcf2014-02-26 12:18:27 +0100310 dev_dbg(chip->card->dev,
Takashi Iwaiee419652009-02-05 16:11:31 +0100311 "done - active_bank = 0x%x, start = 0x%x\n",
312 chip->active_bank,
313 voice->bank[chip->active_bank].start);
314 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 ypcm->period_pos %= ypcm->period_size;
316 spin_unlock(&chip->reg_lock);
317 snd_pcm_period_elapsed(ypcm->substream);
318 spin_lock(&chip->reg_lock);
319 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200320
321 if (unlikely(ypcm->update_pcm_vol)) {
322 unsigned int subs = ypcm->substream->number;
323 unsigned int next_bank = 1 - chip->active_bank;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100324 struct snd_ymfpci_playback_bank *bank;
Takashi Iwaid3c63762018-07-25 23:24:01 +0200325 __le32 volume;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200326
327 bank = &voice->bank[next_bank];
328 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
329 bank->left_gain_end = volume;
330 if (ypcm->output_rear)
331 bank->eff2_gain_end = volume;
332 if (ypcm->voices[1])
333 bank = &ypcm->voices[1]->bank[next_bank];
334 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
335 bank->right_gain_end = volume;
336 if (ypcm->output_rear)
337 bank->eff3_gain_end = volume;
338 ypcm->update_pcm_vol--;
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 spin_unlock(&chip->reg_lock);
342}
343
Takashi Iwai208a1b42005-11-17 14:53:41 +0100344static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100346 struct snd_pcm_runtime *runtime = substream->runtime;
347 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
348 struct snd_ymfpci *chip = ypcm->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 u32 pos, delta;
350
351 spin_lock(&chip->reg_lock);
352 if (ypcm->running) {
353 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
354 if (pos < ypcm->last_pos)
355 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
356 else
357 delta = pos - ypcm->last_pos;
358 ypcm->period_pos += delta;
359 ypcm->last_pos = pos;
360 if (ypcm->period_pos >= ypcm->period_size) {
361 ypcm->period_pos %= ypcm->period_size;
Takashi Iwaiee419652009-02-05 16:11:31 +0100362 /*
Takashi Iwai6436bcf2014-02-26 12:18:27 +0100363 dev_dbg(chip->card->dev,
Takashi Iwaiee419652009-02-05 16:11:31 +0100364 "done - active_bank = 0x%x, start = 0x%x\n",
365 chip->active_bank,
366 voice->bank[chip->active_bank].start);
367 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 spin_unlock(&chip->reg_lock);
369 snd_pcm_period_elapsed(substream);
370 spin_lock(&chip->reg_lock);
371 }
372 }
373 spin_unlock(&chip->reg_lock);
374}
375
Takashi Iwai208a1b42005-11-17 14:53:41 +0100376static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int cmd)
378{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100379 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
380 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200381 struct snd_kcontrol *kctl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 int result = 0;
383
384 spin_lock(&chip->reg_lock);
385 if (ypcm->voices[0] == NULL) {
386 result = -EINVAL;
387 goto __unlock;
388 }
389 switch (cmd) {
390 case SNDRV_PCM_TRIGGER_START:
391 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
392 case SNDRV_PCM_TRIGGER_RESUME:
393 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100394 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
396 ypcm->running = 1;
397 break;
398 case SNDRV_PCM_TRIGGER_STOP:
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200399 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
400 kctl = chip->pcm_mixer[substream->number].ctl;
401 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
402 }
403 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
405 case SNDRV_PCM_TRIGGER_SUSPEND:
406 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100407 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
409 ypcm->running = 0;
410 break;
411 default:
412 result = -EINVAL;
413 break;
414 }
415 __unlock:
416 spin_unlock(&chip->reg_lock);
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200417 if (kctl)
418 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return result;
420}
Takashi Iwai208a1b42005-11-17 14:53:41 +0100421static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 int cmd)
423{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100424 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
425 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int result = 0;
427 u32 tmp;
428
429 spin_lock(&chip->reg_lock);
430 switch (cmd) {
431 case SNDRV_PCM_TRIGGER_START:
432 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
433 case SNDRV_PCM_TRIGGER_RESUME:
434 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
435 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
436 ypcm->running = 1;
437 break;
438 case SNDRV_PCM_TRIGGER_STOP:
439 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
440 case SNDRV_PCM_TRIGGER_SUSPEND:
441 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
442 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
443 ypcm->running = 0;
444 break;
445 default:
446 result = -EINVAL;
447 break;
448 }
449 spin_unlock(&chip->reg_lock);
450 return result;
451}
452
Takashi Iwai208a1b42005-11-17 14:53:41 +0100453static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 int err;
456
457 if (ypcm->voices[1] != NULL && voices < 2) {
458 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
459 ypcm->voices[1] = NULL;
460 }
461 if (voices == 1 && ypcm->voices[0] != NULL)
462 return 0; /* already allocated */
463 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
464 return 0; /* already allocated */
465 if (voices > 1) {
466 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
467 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
468 ypcm->voices[0] = NULL;
469 }
470 }
471 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
472 if (err < 0)
473 return err;
474 ypcm->voices[0]->ypcm = ypcm;
475 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
476 if (voices > 1) {
477 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
478 ypcm->voices[1]->ypcm = ypcm;
479 }
480 return 0;
481}
482
Takashi Iwai208a1b42005-11-17 14:53:41 +0100483static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
484 struct snd_pcm_runtime *runtime,
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200485 int has_pcm_volume)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100487 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 u32 format;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200489 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
490 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
491 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
Takashi Iwai208a1b42005-11-17 14:53:41 +0100492 struct snd_ymfpci_playback_bank *bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 unsigned int nbank;
Takashi Iwaid3c63762018-07-25 23:24:01 +0200494 __le32 vol_left, vol_right;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200495 u8 use_left, use_right;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100496 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Takashi Iwaida3cec32008-08-08 17:12:14 +0200498 if (snd_BUG_ON(!voice))
499 return;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200500 if (runtime->channels == 1) {
501 use_left = 1;
502 use_right = 1;
503 } else {
504 use_left = (voiceidx & 1) == 0;
505 use_right = !use_left;
506 }
507 if (has_pcm_volume) {
508 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
509 [ypcm->substream->number].left << 15);
510 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
511 [ypcm->substream->number].right << 15);
512 } else {
513 vol_left = cpu_to_le32(0x40000000);
514 vol_right = cpu_to_le32(0x40000000);
515 }
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100516 spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200517 format = runtime->channels == 2 ? 0x00010000 : 0;
518 if (snd_pcm_format_width(runtime->format) == 8)
519 format |= 0x80000000;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100520 else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
521 runtime->rate == 44100 && runtime->channels == 2 &&
522 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
523 ypcm->chip->src441_used == voice->number)) {
524 ypcm->chip->src441_used = voice->number;
525 ypcm->use_441_slot = 1;
526 format |= 0x10000000;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100527 }
528 if (ypcm->chip->src441_used == voice->number &&
529 (format & 0x10000000) == 0) {
530 ypcm->chip->src441_used = -1;
531 ypcm->use_441_slot = 0;
532 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200533 if (runtime->channels == 2 && (voiceidx & 1) != 0)
534 format |= 1;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +0100535 spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 for (nbank = 0; nbank < 2; nbank++) {
537 bank = &voice->bank[nbank];
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200538 memset(bank, 0, sizeof(*bank));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 bank->format = cpu_to_le32(format);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200540 bank->base = cpu_to_le32(runtime->dma_addr);
541 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 bank->lpfQ = cpu_to_le32(lpfQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 bank->delta =
544 bank->delta_end = cpu_to_le32(delta);
545 bank->lpfK =
546 bank->lpfK_end = cpu_to_le32(lpfK);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200547 bank->eg_gain =
548 bank->eg_gain_end = cpu_to_le32(0x40000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200550 if (ypcm->output_front) {
551 if (use_left) {
552 bank->left_gain =
553 bank->left_gain_end = vol_left;
554 }
555 if (use_right) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 bank->right_gain =
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200557 bank->right_gain_end = vol_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200559 }
560 if (ypcm->output_rear) {
Jaroslav Kysela5a25c5c2006-01-18 08:02:24 +0100561 if (!ypcm->swap_rear) {
562 if (use_left) {
563 bank->eff2_gain =
564 bank->eff2_gain_end = vol_left;
565 }
566 if (use_right) {
567 bank->eff3_gain =
568 bank->eff3_gain_end = vol_right;
569 }
570 } else {
571 /* The SPDIF out channels seem to be swapped, so we have
572 * to swap them here, too. The rear analog out channels
573 * will be wrong, but otherwise AC3 would not work.
574 */
575 if (use_left) {
576 bank->eff3_gain =
577 bank->eff3_gain_end = vol_left;
578 }
579 if (use_right) {
580 bank->eff2_gain =
581 bank->eff2_gain_end = vol_right;
582 }
583 }
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586}
587
Bill Pembertone23e7a12012-12-06 12:35:10 -0500588static int snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Takashi Iwai6974f8a2019-11-05 16:18:55 +0100590 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 4096, &chip->ac3_tmp_base) < 0)
592 return -ENOMEM;
593
594 chip->bank_effect[3][0]->base =
595 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
596 chip->bank_effect[3][0]->loop_end =
597 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
598 chip->bank_effect[4][0]->base =
599 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
600 chip->bank_effect[4][0]->loop_end =
601 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
602
603 spin_lock_irq(&chip->reg_lock);
604 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
605 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
606 spin_unlock_irq(&chip->reg_lock);
607 return 0;
608}
609
Takashi Iwai208a1b42005-11-17 14:53:41 +0100610static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 spin_lock_irq(&chip->reg_lock);
613 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
614 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
615 spin_unlock_irq(&chip->reg_lock);
616 // snd_ymfpci_irq_wait(chip);
617 if (chip->ac3_tmp_base.area) {
618 snd_dma_free_pages(&chip->ac3_tmp_base);
619 chip->ac3_tmp_base.area = NULL;
620 }
621 return 0;
622}
623
Takashi Iwai208a1b42005-11-17 14:53:41 +0100624static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
625 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100627 struct snd_pcm_runtime *runtime = substream->runtime;
628 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 int err;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
632 return err;
633 return 0;
634}
635
Takashi Iwai208a1b42005-11-17 14:53:41 +0100636static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100638 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
639 struct snd_pcm_runtime *runtime = substream->runtime;
640 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 if (runtime->private_data == NULL)
643 return 0;
644 ypcm = runtime->private_data;
645
646 /* wait, until the PCI operations are not finished */
647 snd_ymfpci_irq_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (ypcm->voices[1]) {
649 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
650 ypcm->voices[1] = NULL;
651 }
652 if (ypcm->voices[0]) {
653 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
654 ypcm->voices[0] = NULL;
655 }
656 return 0;
657}
658
Takashi Iwai208a1b42005-11-17 14:53:41 +0100659static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100661 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
662 struct snd_pcm_runtime *runtime = substream->runtime;
663 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200664 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 unsigned int nvoice;
666
667 ypcm->period_size = runtime->period_size;
668 ypcm->buffer_size = runtime->buffer_size;
669 ypcm->period_pos = 0;
670 ypcm->last_pos = 0;
671 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +0200672 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
673 substream->pcm == chip->pcm);
Clemens Ladisch177a7cd2007-07-23 17:38:44 +0200674
675 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
676 kctl = chip->pcm_mixer[substream->number].ctl;
677 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
678 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
681}
682
Takashi Iwai208a1b42005-11-17 14:53:41 +0100683static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100685 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 /* wait, until the PCI operations are not finished */
688 snd_ymfpci_irq_wait(chip);
Takashi Iwaib6ed90c2019-12-09 10:49:28 +0100689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Takashi Iwai208a1b42005-11-17 14:53:41 +0100692static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100694 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
695 struct snd_pcm_runtime *runtime = substream->runtime;
696 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
697 struct snd_ymfpci_capture_bank * bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int nbank;
699 u32 rate, format;
700
701 ypcm->period_size = runtime->period_size;
702 ypcm->buffer_size = runtime->buffer_size;
703 ypcm->period_pos = 0;
704 ypcm->last_pos = 0;
705 ypcm->shift = 0;
706 rate = ((48000 * 4096) / runtime->rate) - 1;
707 format = 0;
708 if (runtime->channels == 2) {
709 format |= 2;
710 ypcm->shift++;
711 }
712 if (snd_pcm_format_width(runtime->format) == 8)
713 format |= 1;
714 else
715 ypcm->shift++;
716 switch (ypcm->capture_bank_number) {
717 case 0:
718 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
719 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
720 break;
721 case 1:
722 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
723 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
724 break;
725 }
726 for (nbank = 0; nbank < 2; nbank++) {
727 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
728 bank->base = cpu_to_le32(runtime->dma_addr);
729 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
730 bank->start = 0;
731 bank->num_of_loops = 0;
732 }
733 return 0;
734}
735
Takashi Iwai208a1b42005-11-17 14:53:41 +0100736static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100738 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
739 struct snd_pcm_runtime *runtime = substream->runtime;
740 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
741 struct snd_ymfpci_voice *voice = ypcm->voices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (!(ypcm->running && voice))
744 return 0;
745 return le32_to_cpu(voice->bank[chip->active_bank].start);
746}
747
Takashi Iwai208a1b42005-11-17 14:53:41 +0100748static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100750 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
751 struct snd_pcm_runtime *runtime = substream->runtime;
752 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (!ypcm->running)
755 return 0;
756 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
757}
758
Takashi Iwai208a1b42005-11-17 14:53:41 +0100759static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Ingo Molnarac6424b2017-06-20 12:06:13 +0200761 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 int loops = 4;
763
764 while (loops-- > 0) {
765 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
766 continue;
767 init_waitqueue_entry(&wait, current);
768 add_wait_queue(&chip->interrupt_sleep, &wait);
769 atomic_inc(&chip->interrupt_sleep_count);
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200770 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 remove_wait_queue(&chip->interrupt_sleep, &wait);
772 }
773}
774
David Howells7d12e782006-10-05 14:55:46 +0100775static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100777 struct snd_ymfpci *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 u32 status, nvoice, mode;
Takashi Iwai208a1b42005-11-17 14:53:41 +0100779 struct snd_ymfpci_voice *voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
782 if (status & 0x80000000) {
783 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
784 spin_lock(&chip->voice_lock);
785 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
786 voice = &chip->voices[nvoice];
787 if (voice->interrupt)
788 voice->interrupt(chip, voice);
789 }
790 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
791 if (chip->capture_substream[nvoice])
792 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
793 }
794#if 0
795 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
796 if (chip->effect_substream[nvoice])
797 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
798 }
799#endif
800 spin_unlock(&chip->voice_lock);
801 spin_lock(&chip->reg_lock);
802 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
803 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
804 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
805 spin_unlock(&chip->reg_lock);
806
807 if (atomic_read(&chip->interrupt_sleep_count)) {
808 atomic_set(&chip->interrupt_sleep_count, 0);
809 wake_up(&chip->interrupt_sleep);
810 }
811 }
812
813 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
814 if (status & 1) {
815 if (chip->timer)
Clemens Ladisch6e2efaa2009-08-10 10:06:53 +0200816 snd_timer_interrupt(chip->timer, chip->timer_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
819
820 if (chip->rawmidi)
David Howells7d12e782006-10-05 14:55:46 +0100821 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return IRQ_HANDLED;
823}
824
Bhumika Goyal420b0c12017-08-12 21:01:27 +0530825static const struct snd_pcm_hardware snd_ymfpci_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 .info = (SNDRV_PCM_INFO_MMAP |
828 SNDRV_PCM_INFO_MMAP_VALID |
829 SNDRV_PCM_INFO_INTERLEAVED |
830 SNDRV_PCM_INFO_BLOCK_TRANSFER |
831 SNDRV_PCM_INFO_PAUSE |
832 SNDRV_PCM_INFO_RESUME),
833 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
834 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
835 .rate_min = 8000,
836 .rate_max = 48000,
837 .channels_min = 1,
838 .channels_max = 2,
839 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
840 .period_bytes_min = 64,
841 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
842 .periods_min = 3,
843 .periods_max = 1024,
844 .fifo_size = 0,
845};
846
Bhumika Goyal420b0c12017-08-12 21:01:27 +0530847static const struct snd_pcm_hardware snd_ymfpci_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 .info = (SNDRV_PCM_INFO_MMAP |
850 SNDRV_PCM_INFO_MMAP_VALID |
851 SNDRV_PCM_INFO_INTERLEAVED |
852 SNDRV_PCM_INFO_BLOCK_TRANSFER |
853 SNDRV_PCM_INFO_PAUSE |
854 SNDRV_PCM_INFO_RESUME),
855 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
856 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
857 .rate_min = 8000,
858 .rate_max = 48000,
859 .channels_min = 1,
860 .channels_max = 2,
861 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
862 .period_bytes_min = 64,
863 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
864 .periods_min = 3,
865 .periods_max = 1024,
866 .fifo_size = 0,
867};
868
Takashi Iwai208a1b42005-11-17 14:53:41 +0100869static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Jesper Juhl4d572772005-05-30 17:30:32 +0200871 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Takashi Iwai208a1b42005-11-17 14:53:41 +0100874static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100876 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
877 struct snd_pcm_runtime *runtime = substream->runtime;
878 struct snd_ymfpci_pcm *ypcm;
Clemens Ladisch84f9df12011-09-16 22:52:48 +0200879 int err;
880
881 runtime->hw = snd_ymfpci_playback;
882 /* FIXME? True value is 256/48 = 5.33333 ms */
883 err = snd_pcm_hw_constraint_minmax(runtime,
884 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
885 5334, UINT_MAX);
886 if (err < 0)
887 return err;
Clemens Ladisch5b0416a2011-09-16 23:08:28 +0200888 err = snd_pcm_hw_rule_noresample(runtime, 48000);
889 if (err < 0)
890 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200892 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (ypcm == NULL)
894 return -ENOMEM;
895 ypcm->chip = chip;
896 ypcm->type = PLAYBACK_VOICE;
897 ypcm->substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 runtime->private_data = ypcm;
899 runtime->private_free = snd_ymfpci_pcm_free_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return 0;
901}
902
903/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100904static void ymfpci_open_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 if (! chip->rear_opened) {
907 if (! chip->spdif_opened) /* set AC3 */
908 snd_ymfpci_writel(chip, YDSXGR_MODE,
909 snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
910 /* enable second codec (4CHEN) */
911 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
912 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
913 }
914}
915
916/* call with spinlock held */
Takashi Iwai208a1b42005-11-17 14:53:41 +0100917static void ymfpci_close_extension(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 if (! chip->rear_opened) {
920 if (! chip->spdif_opened)
921 snd_ymfpci_writel(chip, YDSXGR_MODE,
922 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
923 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
924 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
925 }
926}
927
Takashi Iwai208a1b42005-11-17 14:53:41 +0100928static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100930 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
931 struct snd_pcm_runtime *runtime = substream->runtime;
932 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 int err;
934
935 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
936 return err;
937 ypcm = runtime->private_data;
938 ypcm->output_front = 1;
939 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
Glen Masgaid9301262006-10-10 09:27:19 +0200940 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 spin_lock_irq(&chip->reg_lock);
942 if (ypcm->output_rear) {
943 ymfpci_open_extension(chip);
944 chip->rear_opened++;
945 }
946 spin_unlock_irq(&chip->reg_lock);
947 return 0;
948}
949
Takashi Iwai208a1b42005-11-17 14:53:41 +0100950static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100952 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
953 struct snd_pcm_runtime *runtime = substream->runtime;
954 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int err;
956
957 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
958 return err;
959 ypcm = runtime->private_data;
960 ypcm->output_front = 0;
961 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200962 ypcm->swap_rear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 spin_lock_irq(&chip->reg_lock);
964 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
965 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
966 ymfpci_open_extension(chip);
967 chip->spdif_pcm_bits = chip->spdif_bits;
968 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
969 chip->spdif_opened++;
970 spin_unlock_irq(&chip->reg_lock);
971
972 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
973 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
974 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
975 return 0;
976}
977
Takashi Iwai208a1b42005-11-17 14:53:41 +0100978static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Takashi Iwai208a1b42005-11-17 14:53:41 +0100980 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
981 struct snd_pcm_runtime *runtime = substream->runtime;
982 struct snd_ymfpci_pcm *ypcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 int err;
984
985 if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
986 return err;
987 ypcm = runtime->private_data;
988 ypcm->output_front = 0;
989 ypcm->output_rear = 1;
Glen Masgaid9301262006-10-10 09:27:19 +0200990 ypcm->swap_rear = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 spin_lock_irq(&chip->reg_lock);
992 ymfpci_open_extension(chip);
993 chip->rear_opened++;
994 spin_unlock_irq(&chip->reg_lock);
995 return 0;
996}
997
Takashi Iwai208a1b42005-11-17 14:53:41 +0100998static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 u32 capture_bank_number)
1000{
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;
Clemens Ladisch84f9df12011-09-16 22:52:48 +02001004 int err;
1005
1006 runtime->hw = snd_ymfpci_capture;
1007 /* FIXME? True value is 256/48 = 5.33333 ms */
1008 err = snd_pcm_hw_constraint_minmax(runtime,
1009 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1010 5334, UINT_MAX);
1011 if (err < 0)
1012 return err;
Clemens Ladisch5b0416a2011-09-16 23:08:28 +02001013 err = snd_pcm_hw_rule_noresample(runtime, 48000);
1014 if (err < 0)
1015 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001017 ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (ypcm == NULL)
1019 return -ENOMEM;
1020 ypcm->chip = chip;
1021 ypcm->type = capture_bank_number + CAPTURE_REC;
1022 ypcm->substream = substream;
1023 ypcm->capture_bank_number = capture_bank_number;
1024 chip->capture_substream[capture_bank_number] = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 runtime->private_data = ypcm;
1026 runtime->private_free = snd_ymfpci_pcm_free_substream;
1027 snd_ymfpci_hw_start(chip);
1028 return 0;
1029}
1030
Takashi Iwai208a1b42005-11-17 14:53:41 +01001031static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 return snd_ymfpci_capture_open(substream, 0);
1034}
1035
Takashi Iwai208a1b42005-11-17 14:53:41 +01001036static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 return snd_ymfpci_capture_open(substream, 1);
1039}
1040
Takashi Iwai208a1b42005-11-17 14:53:41 +01001041static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 return 0;
1044}
1045
Takashi Iwai208a1b42005-11-17 14:53:41 +01001046static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001048 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1049 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 spin_lock_irq(&chip->reg_lock);
1052 if (ypcm->output_rear && chip->rear_opened > 0) {
1053 chip->rear_opened--;
1054 ymfpci_close_extension(chip);
1055 }
1056 spin_unlock_irq(&chip->reg_lock);
1057 return snd_ymfpci_playback_close_1(substream);
1058}
1059
Takashi Iwai208a1b42005-11-17 14:53:41 +01001060static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001062 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 spin_lock_irq(&chip->reg_lock);
1065 chip->spdif_opened = 0;
1066 ymfpci_close_extension(chip);
1067 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1068 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1069 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1070 spin_unlock_irq(&chip->reg_lock);
1071 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1072 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1073 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1074 return snd_ymfpci_playback_close_1(substream);
1075}
1076
Takashi Iwai208a1b42005-11-17 14:53:41 +01001077static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001079 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 spin_lock_irq(&chip->reg_lock);
1082 if (chip->rear_opened > 0) {
1083 chip->rear_opened--;
1084 ymfpci_close_extension(chip);
1085 }
1086 spin_unlock_irq(&chip->reg_lock);
1087 return snd_ymfpci_playback_close_1(substream);
1088}
1089
Takashi Iwai208a1b42005-11-17 14:53:41 +01001090static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001092 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1093 struct snd_pcm_runtime *runtime = substream->runtime;
1094 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 if (ypcm != NULL) {
1097 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1098 snd_ymfpci_hw_stop(chip);
1099 }
1100 return 0;
1101}
1102
Julia Lawall6769e9882016-09-02 00:13:10 +02001103static const struct snd_pcm_ops snd_ymfpci_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 .open = snd_ymfpci_playback_open,
1105 .close = snd_ymfpci_playback_close,
1106 .ioctl = snd_pcm_lib_ioctl,
1107 .hw_params = snd_ymfpci_playback_hw_params,
1108 .hw_free = snd_ymfpci_playback_hw_free,
1109 .prepare = snd_ymfpci_playback_prepare,
1110 .trigger = snd_ymfpci_playback_trigger,
1111 .pointer = snd_ymfpci_playback_pointer,
1112};
1113
Julia Lawall6769e9882016-09-02 00:13:10 +02001114static const struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 .open = snd_ymfpci_capture_rec_open,
1116 .close = snd_ymfpci_capture_close,
1117 .ioctl = snd_pcm_lib_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 .hw_free = snd_ymfpci_capture_hw_free,
1119 .prepare = snd_ymfpci_capture_prepare,
1120 .trigger = snd_ymfpci_capture_trigger,
1121 .pointer = snd_ymfpci_capture_pointer,
1122};
1123
Lars-Peter Clausen38c47182015-01-02 12:24:55 +01001124int snd_ymfpci_pcm(struct snd_ymfpci *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001126 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 int err;
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1130 return err;
1131 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1134 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1135
1136 /* global setup */
1137 pcm->info_flags = 0;
1138 strcpy(pcm->name, "YMFPCI");
1139 chip->pcm = pcm;
1140
Takashi Iwaib6ed90c2019-12-09 10:49:28 +01001141 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1142 &chip->pci->dev, 64*1024, 256*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Lars-Peter Clausen38c47182015-01-02 12:24:55 +01001144 return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
Takashi Iwai0afdb8f2012-09-12 16:14:37 +02001145 snd_pcm_std_chmaps, 2, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
Julia Lawall6769e9882016-09-02 00:13:10 +02001148static const struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 .open = snd_ymfpci_capture_ac97_open,
1150 .close = snd_ymfpci_capture_close,
1151 .ioctl = snd_pcm_lib_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 .hw_free = snd_ymfpci_capture_hw_free,
1153 .prepare = snd_ymfpci_capture_prepare,
1154 .trigger = snd_ymfpci_capture_trigger,
1155 .pointer = snd_ymfpci_capture_pointer,
1156};
1157
Lars-Peter Clausen38c47182015-01-02 12:24:55 +01001158int snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001160 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 int err;
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1164 return err;
1165 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1168
1169 /* global setup */
1170 pcm->info_flags = 0;
1171 sprintf(pcm->name, "YMFPCI - %s",
1172 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1173 chip->pcm2 = pcm;
1174
Takashi Iwaib6ed90c2019-12-09 10:49:28 +01001175 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1176 &chip->pci->dev, 64*1024, 256*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return 0;
1179}
1180
Julia Lawall6769e9882016-09-02 00:13:10 +02001181static const struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 .open = snd_ymfpci_playback_spdif_open,
1183 .close = snd_ymfpci_playback_spdif_close,
1184 .ioctl = snd_pcm_lib_ioctl,
1185 .hw_params = snd_ymfpci_playback_hw_params,
1186 .hw_free = snd_ymfpci_playback_hw_free,
1187 .prepare = snd_ymfpci_playback_prepare,
1188 .trigger = snd_ymfpci_playback_trigger,
1189 .pointer = snd_ymfpci_playback_pointer,
1190};
1191
Lars-Peter Clausen38c47182015-01-02 12:24:55 +01001192int snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001194 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int err;
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1198 return err;
1199 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1202
1203 /* global setup */
1204 pcm->info_flags = 0;
1205 strcpy(pcm->name, "YMFPCI - IEC958");
1206 chip->pcm_spdif = pcm;
1207
Takashi Iwaib6ed90c2019-12-09 10:49:28 +01001208 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1209 &chip->pci->dev, 64*1024, 256*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return 0;
1212}
1213
Julia Lawall6769e9882016-09-02 00:13:10 +02001214static const struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 .open = snd_ymfpci_playback_4ch_open,
1216 .close = snd_ymfpci_playback_4ch_close,
1217 .ioctl = snd_pcm_lib_ioctl,
1218 .hw_params = snd_ymfpci_playback_hw_params,
1219 .hw_free = snd_ymfpci_playback_hw_free,
1220 .prepare = snd_ymfpci_playback_prepare,
1221 .trigger = snd_ymfpci_playback_trigger,
1222 .pointer = snd_ymfpci_playback_pointer,
1223};
1224
Takashi Iwai0afdb8f2012-09-12 16:14:37 +02001225static const struct snd_pcm_chmap_elem surround_map[] = {
1226 { .channels = 1,
Takashi Iwai5efbc262012-09-13 14:48:46 +02001227 .map = { SNDRV_CHMAP_MONO } },
Takashi Iwai0afdb8f2012-09-12 16:14:37 +02001228 { .channels = 2,
1229 .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
1230 { }
1231};
1232
Lars-Peter Clausen38c47182015-01-02 12:24:55 +01001233int snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001235 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 int err;
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1239 return err;
1240 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1243
1244 /* global setup */
1245 pcm->info_flags = 0;
1246 strcpy(pcm->name, "YMFPCI - Rear PCM");
1247 chip->pcm_4ch = pcm;
1248
Takashi Iwaib6ed90c2019-12-09 10:49:28 +01001249 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1250 &chip->pci->dev, 64*1024, 256*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Lars-Peter Clausen38c47182015-01-02 12:24:55 +01001252 return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
Takashi Iwai0afdb8f2012-09-12 16:14:37 +02001253 surround_map, 2, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
Takashi Iwai208a1b42005-11-17 14:53:41 +01001256static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1259 uinfo->count = 1;
1260 return 0;
1261}
1262
Takashi Iwai208a1b42005-11-17 14:53:41 +01001263static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1264 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001266 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 spin_lock_irq(&chip->reg_lock);
1269 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1270 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001271 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 spin_unlock_irq(&chip->reg_lock);
1273 return 0;
1274}
1275
Takashi Iwai208a1b42005-11-17 14:53:41 +01001276static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1277 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001279 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 unsigned int val;
1281 int change;
1282
1283 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1284 (ucontrol->value.iec958.status[1] << 8);
1285 spin_lock_irq(&chip->reg_lock);
1286 change = chip->spdif_bits != val;
1287 chip->spdif_bits = val;
1288 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1289 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1290 spin_unlock_irq(&chip->reg_lock);
1291 return change;
1292}
1293
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301294static const struct snd_kcontrol_new snd_ymfpci_spdif_default =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1297 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1298 .info = snd_ymfpci_spdif_default_info,
1299 .get = snd_ymfpci_spdif_default_get,
1300 .put = snd_ymfpci_spdif_default_put
1301};
1302
Takashi Iwai208a1b42005-11-17 14:53:41 +01001303static int snd_ymfpci_spdif_mask_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_mask_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] = 0x3e;
1317 ucontrol->value.iec958.status[1] = 0xff;
1318 spin_unlock_irq(&chip->reg_lock);
1319 return 0;
1320}
1321
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301322static const struct snd_kcontrol_new snd_ymfpci_spdif_mask =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1325 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1326 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1327 .info = snd_ymfpci_spdif_mask_info,
1328 .get = snd_ymfpci_spdif_mask_get,
1329};
1330
Takashi Iwai208a1b42005-11-17 14:53:41 +01001331static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1334 uinfo->count = 1;
1335 return 0;
1336}
1337
Takashi Iwai208a1b42005-11-17 14:53:41 +01001338static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1339 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001341 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 spin_lock_irq(&chip->reg_lock);
1344 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1345 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
Clemens Ladischfc80a202006-01-13 07:41:45 +01001346 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 spin_unlock_irq(&chip->reg_lock);
1348 return 0;
1349}
1350
Takashi Iwai208a1b42005-11-17 14:53:41 +01001351static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1352 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001354 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 unsigned int val;
1356 int change;
1357
1358 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1359 (ucontrol->value.iec958.status[1] << 8);
1360 spin_lock_irq(&chip->reg_lock);
1361 change = chip->spdif_pcm_bits != val;
1362 chip->spdif_pcm_bits = val;
1363 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1364 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1365 spin_unlock_irq(&chip->reg_lock);
1366 return change;
1367}
1368
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301369static const struct snd_kcontrol_new snd_ymfpci_spdif_stream =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1372 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1373 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1374 .info = snd_ymfpci_spdif_stream_info,
1375 .get = snd_ymfpci_spdif_stream_get,
1376 .put = snd_ymfpci_spdif_stream_put
1377};
1378
Takashi Iwai208a1b42005-11-17 14:53:41 +01001379static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Clemens Ladischbed68962011-01-10 16:29:06 +01001381 static const char *const texts[3] = {"AC'97", "IEC958", "ZV Port"};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Clemens Ladischbed68962011-01-10 16:29:06 +01001383 return snd_ctl_enum_info(info, 1, 3, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
Takashi Iwai208a1b42005-11-17 14:53:41 +01001386static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
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 u16 reg;
1390
1391 spin_lock_irq(&chip->reg_lock);
1392 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1393 spin_unlock_irq(&chip->reg_lock);
1394 if (!(reg & 0x100))
1395 value->value.enumerated.item[0] = 0;
1396 else
1397 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1398 return 0;
1399}
1400
Takashi Iwai208a1b42005-11-17 14:53:41 +01001401static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001403 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 u16 reg, old_reg;
1405
1406 spin_lock_irq(&chip->reg_lock);
1407 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1408 if (value->value.enumerated.item[0] == 0)
1409 reg = old_reg & ~0x100;
1410 else
1411 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1412 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1413 spin_unlock_irq(&chip->reg_lock);
1414 return reg != old_reg;
1415}
1416
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301417static const struct snd_kcontrol_new snd_ymfpci_drec_source = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1419 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1420 .name = "Direct Recording Source",
1421 .info = snd_ymfpci_drec_source_info,
1422 .get = snd_ymfpci_drec_source_get,
1423 .put = snd_ymfpci_drec_source_put
1424};
1425
1426/*
1427 * Mixer controls
1428 */
1429
Glen Masgaid602c8852005-09-28 08:19:05 +02001430#define YMFPCI_SINGLE(xname, xindex, reg, shift) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1432 .info = snd_ymfpci_info_single, \
1433 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
Glen Masgaid602c8852005-09-28 08:19:05 +02001434 .private_value = ((reg) | ((shift) << 16)) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001436#define snd_ymfpci_info_single snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Takashi Iwai208a1b42005-11-17 14:53:41 +01001438static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1439 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001441 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c8852005-09-28 08:19:05 +02001442 int reg = kcontrol->private_value & 0xffff;
1443 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1444 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Glen Masgaid602c8852005-09-28 08:19:05 +02001446 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 case YDSXGR_SPDIFOUTCTRL: break;
1448 case YDSXGR_SPDIFINCTRL: break;
1449 default: return -EINVAL;
1450 }
Glen Masgaid602c8852005-09-28 08:19:05 +02001451 ucontrol->value.integer.value[0] =
1452 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return 0;
1454}
1455
Takashi Iwai208a1b42005-11-17 14:53:41 +01001456static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1457 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001459 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Glen Masgaid602c8852005-09-28 08:19:05 +02001460 int reg = kcontrol->private_value & 0xffff;
1461 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1462 unsigned int mask = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 int change;
1464 unsigned int val, oval;
1465
Glen Masgaid602c8852005-09-28 08:19:05 +02001466 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 case YDSXGR_SPDIFOUTCTRL: break;
1468 case YDSXGR_SPDIFINCTRL: break;
1469 default: return -EINVAL;
1470 }
1471 val = (ucontrol->value.integer.value[0] & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 val <<= shift;
1473 spin_lock_irq(&chip->reg_lock);
1474 oval = snd_ymfpci_readl(chip, reg);
1475 val = (oval & ~(mask << shift)) | val;
1476 change = val != oval;
1477 snd_ymfpci_writel(chip, reg, val);
1478 spin_unlock_irq(&chip->reg_lock);
1479 return change;
1480}
1481
Takashi Iwai0cb29ea2007-01-29 15:33:49 +01001482static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
Takashi Iwai33925182006-08-28 13:29:42 +02001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484#define YMFPCI_DOUBLE(xname, xindex, reg) \
1485{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Takashi Iwai33925182006-08-28 13:29:42 +02001486 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 .info = snd_ymfpci_info_double, \
1488 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
Takashi Iwai33925182006-08-28 13:29:42 +02001489 .private_value = reg, \
1490 .tlv = { .p = db_scale_native } }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Takashi Iwai208a1b42005-11-17 14:53:41 +01001492static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 unsigned int reg = kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 if (reg < 0x80 || reg >= 0xc0)
1497 return -EINVAL;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001498 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 uinfo->count = 2;
1500 uinfo->value.integer.min = 0;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001501 uinfo->value.integer.max = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return 0;
1503}
1504
Takashi Iwai208a1b42005-11-17 14:53:41 +01001505static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001507 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001509 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 unsigned int val;
1511
1512 if (reg < 0x80 || reg >= 0xc0)
1513 return -EINVAL;
1514 spin_lock_irq(&chip->reg_lock);
1515 val = snd_ymfpci_readl(chip, reg);
1516 spin_unlock_irq(&chip->reg_lock);
1517 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1518 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return 0;
1520}
1521
Takashi Iwai208a1b42005-11-17 14:53:41 +01001522static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001524 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 unsigned int reg = kcontrol->private_value;
Adrian Bunk467a8c22005-04-11 14:11:00 +02001526 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 int change;
1528 unsigned int val1, val2, oval;
1529
1530 if (reg < 0x80 || reg >= 0xc0)
1531 return -EINVAL;
1532 val1 = ucontrol->value.integer.value[0] & mask;
1533 val2 = ucontrol->value.integer.value[1] & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 val1 <<= shift_left;
1535 val2 <<= shift_right;
1536 spin_lock_irq(&chip->reg_lock);
1537 oval = snd_ymfpci_readl(chip, reg);
1538 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1539 change = val1 != oval;
1540 snd_ymfpci_writel(chip, reg, val1);
1541 spin_unlock_irq(&chip->reg_lock);
1542 return change;
1543}
1544
Clemens Ladisch177a7cd2007-07-23 17:38:44 +02001545static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1546 struct snd_ctl_elem_value *ucontrol)
1547{
1548 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1549 unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1550 unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1551 int change;
1552 unsigned int value, oval;
1553
1554 value = ucontrol->value.integer.value[0] & 0x3fff;
1555 value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1556 spin_lock_irq(&chip->reg_lock);
1557 oval = snd_ymfpci_readl(chip, reg);
1558 change = value != oval;
1559 snd_ymfpci_writel(chip, reg, value);
1560 snd_ymfpci_writel(chip, reg2, value);
1561 spin_unlock_irq(&chip->reg_lock);
1562 return change;
1563}
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565/*
1566 * 4ch duplication
1567 */
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001568#define snd_ymfpci_info_dup4ch snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Takashi Iwai208a1b42005-11-17 14:53:41 +01001570static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001572 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1574 return 0;
1575}
1576
Takashi Iwai208a1b42005-11-17 14:53:41 +01001577static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001579 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 int change;
1581 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1582 if (change)
1583 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1584 return change;
1585}
1586
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301587static const struct snd_kcontrol_new snd_ymfpci_dup4ch = {
Raymond Yau4d20bb12012-01-17 11:41:47 +08001588 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1589 .name = "4ch Duplication",
1590 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1591 .info = snd_ymfpci_info_dup4ch,
1592 .get = snd_ymfpci_get_dup4ch,
1593 .put = snd_ymfpci_put_dup4ch,
1594};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Bill Pembertone23e7a12012-12-06 12:35:10 -05001596static struct snd_kcontrol_new snd_ymfpci_controls[] = {
Clemens Ladisch177a7cd2007-07-23 17:38:44 +02001597{
1598 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1599 .name = "Wave Playback Volume",
1600 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1601 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1602 .info = snd_ymfpci_info_double,
1603 .get = snd_ymfpci_get_double,
1604 .put = snd_ymfpci_put_nativedacvol,
1605 .private_value = YDSXGR_NATIVEDACOUTVOL,
1606 .tlv = { .p = db_scale_native },
1607},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1609YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1610YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1611YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1612YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1613YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1614YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
Raymond Yau89f33252011-09-09 19:15:01 +08001615YMFPCI_DOUBLE("FM Legacy Playback Volume", 0, YDSXGR_LEGACYOUTVOL),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1617YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1618YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1619YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
Glen Masgaid602c8852005-09-28 08:19:05 +02001620YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1621YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1622YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623};
1624
1625
1626/*
1627 * GPIO
1628 */
1629
Takashi Iwai208a1b42005-11-17 14:53:41 +01001630static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 u16 reg, mode;
1633 unsigned long flags;
1634
1635 spin_lock_irqsave(&chip->reg_lock, flags);
1636 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1637 reg &= ~(1 << (pin + 8));
1638 reg |= (1 << pin);
1639 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1640 /* set the level mode for input line */
1641 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1642 mode &= ~(3 << (pin * 2));
1643 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1644 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1645 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1646 spin_unlock_irqrestore(&chip->reg_lock, flags);
1647 return (mode >> pin) & 1;
1648}
1649
Takashi Iwai208a1b42005-11-17 14:53:41 +01001650static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 u16 reg;
1653 unsigned long flags;
1654
1655 spin_lock_irqsave(&chip->reg_lock, flags);
1656 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1657 reg &= ~(1 << pin);
1658 reg &= ~(1 << (pin + 8));
1659 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1660 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1661 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1662 spin_unlock_irqrestore(&chip->reg_lock, flags);
1663
1664 return 0;
1665}
1666
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001667#define snd_ymfpci_gpio_sw_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Takashi Iwai208a1b42005-11-17 14:53:41 +01001669static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001671 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 int pin = (int)kcontrol->private_value;
1673 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1674 return 0;
1675}
1676
Takashi Iwai208a1b42005-11-17 14:53:41 +01001677static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001679 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 int pin = (int)kcontrol->private_value;
1681
1682 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1683 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1684 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1685 return 1;
1686 }
1687 return 0;
1688}
1689
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301690static const struct snd_kcontrol_new snd_ymfpci_rear_shared = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 .name = "Shared Rear/Line-In Switch",
1692 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1693 .info = snd_ymfpci_gpio_sw_info,
1694 .get = snd_ymfpci_gpio_sw_get,
1695 .put = snd_ymfpci_gpio_sw_put,
1696 .private_value = 2,
1697};
1698
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001699/*
1700 * PCM voice volume
1701 */
1702
Takashi Iwai208a1b42005-11-17 14:53:41 +01001703static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1704 struct snd_ctl_elem_info *uinfo)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001705{
1706 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1707 uinfo->count = 2;
1708 uinfo->value.integer.min = 0;
1709 uinfo->value.integer.max = 0x8000;
1710 return 0;
1711}
1712
Takashi Iwai208a1b42005-11-17 14:53:41 +01001713static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1714 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001715{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001716 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001717 unsigned int subs = kcontrol->id.subdevice;
1718
1719 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1720 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1721 return 0;
1722}
1723
Takashi Iwai208a1b42005-11-17 14:53:41 +01001724static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1725 struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001726{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001727 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001728 unsigned int subs = kcontrol->id.subdevice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001729 struct snd_pcm_substream *substream;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001730 unsigned long flags;
1731
1732 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1733 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1734 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1735 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001736 if (chip->pcm_mixer[subs].left > 0x8000)
1737 chip->pcm_mixer[subs].left = 0x8000;
1738 if (chip->pcm_mixer[subs].right > 0x8000)
1739 chip->pcm_mixer[subs].right = 0x8000;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001740
Takashi Iwai208a1b42005-11-17 14:53:41 +01001741 substream = (struct snd_pcm_substream *)kcontrol->private_value;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001742 spin_lock_irqsave(&chip->voice_lock, flags);
1743 if (substream->runtime && substream->runtime->private_data) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01001744 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01001745 if (!ypcm->use_441_slot)
1746 ypcm->update_pcm_vol = 2;
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001747 }
1748 spin_unlock_irqrestore(&chip->voice_lock, flags);
1749 return 1;
1750 }
1751 return 0;
1752}
1753
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301754static const struct snd_kcontrol_new snd_ymfpci_pcm_volume = {
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001755 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1756 .name = "PCM Playback Volume",
1757 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1758 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1759 .info = snd_ymfpci_pcm_vol_info,
1760 .get = snd_ymfpci_pcm_vol_get,
1761 .put = snd_ymfpci_pcm_vol_put,
1762};
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765/*
1766 * Mixer routines
1767 */
1768
Takashi Iwai208a1b42005-11-17 14:53:41 +01001769static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001771 struct snd_ymfpci *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 chip->ac97_bus = NULL;
1773}
1774
Takashi Iwai208a1b42005-11-17 14:53:41 +01001775static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001777 struct snd_ymfpci *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 chip->ac97 = NULL;
1779}
1780
Bill Pembertone23e7a12012-12-06 12:35:10 -05001781int snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001783 struct snd_ac97_template ac97;
1784 struct snd_kcontrol *kctl;
1785 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 unsigned int idx;
1787 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01001788 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 .write = snd_ymfpci_codec_write,
1790 .read = snd_ymfpci_codec_read,
1791 };
1792
1793 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1794 return err;
1795 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1796 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1797
1798 memset(&ac97, 0, sizeof(ac97));
1799 ac97.private_data = chip;
1800 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1801 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1802 return err;
1803
1804 /* to be sure */
1805 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1806 AC97_EA_VRA|AC97_EA_VRM, 0);
1807
1808 for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1809 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1810 return err;
1811 }
Raymond Yau4d20bb12012-01-17 11:41:47 +08001812 if (chip->ac97->ext_id & AC97_EI_SDAC) {
1813 kctl = snd_ctl_new1(&snd_ymfpci_dup4ch, chip);
1814 err = snd_ctl_add(chip->card, kctl);
1815 if (err < 0)
1816 return err;
1817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 /* add S/PDIF control */
Takashi Iwaida3cec32008-08-08 17:12:14 +02001820 if (snd_BUG_ON(!chip->pcm_spdif))
1821 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1823 return err;
1824 kctl->id.device = chip->pcm_spdif->device;
1825 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1826 return err;
1827 kctl->id.device = chip->pcm_spdif->device;
1828 if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1829 return err;
1830 kctl->id.device = chip->pcm_spdif->device;
1831 chip->spdif_pcm_ctl = kctl;
1832
1833 /* direct recording source */
1834 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1835 (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1836 return err;
1837
1838 /*
1839 * shared rear/line-in
1840 */
1841 if (rear_switch) {
1842 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1843 return err;
1844 }
1845
Clemens Ladisch9bcf6552005-08-10 10:21:43 +02001846 /* per-voice volume */
1847 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1848 for (idx = 0; idx < 32; ++idx) {
1849 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1850 if (!kctl)
1851 return -ENOMEM;
1852 kctl->id.device = chip->pcm->device;
1853 kctl->id.subdevice = idx;
1854 kctl->private_value = (unsigned long)substream;
1855 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1856 return err;
1857 chip->pcm_mixer[idx].left = 0x8000;
1858 chip->pcm_mixer[idx].right = 0x8000;
1859 chip->pcm_mixer[idx].ctl = kctl;
1860 substream = substream->next;
1861 }
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 return 0;
1864}
1865
1866
1867/*
1868 * timer
1869 */
1870
Takashi Iwai208a1b42005-11-17 14:53:41 +01001871static int snd_ymfpci_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001873 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 unsigned long flags;
1875 unsigned int count;
1876
1877 chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 spin_lock_irqsave(&chip->reg_lock, flags);
Clemens Ladisch6e2efaa2009-08-10 10:06:53 +02001879 if (timer->sticks > 1) {
1880 chip->timer_ticks = timer->sticks;
1881 count = timer->sticks - 1;
1882 } else {
1883 /*
1884 * Divisor 1 is not allowed; fake it by using divisor 2 and
1885 * counting two ticks for each interrupt.
1886 */
1887 chip->timer_ticks = 2;
1888 count = 2 - 1;
1889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1891 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1892 spin_unlock_irqrestore(&chip->reg_lock, flags);
1893 return 0;
1894}
1895
Takashi Iwai208a1b42005-11-17 14:53:41 +01001896static int snd_ymfpci_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001898 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 unsigned long flags;
1900
1901 chip = snd_timer_chip(timer);
1902 spin_lock_irqsave(&chip->reg_lock, flags);
1903 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1904 spin_unlock_irqrestore(&chip->reg_lock, flags);
1905 return 0;
1906}
1907
Takashi Iwai208a1b42005-11-17 14:53:41 +01001908static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 unsigned long *num, unsigned long *den)
1910{
1911 *num = 1;
Clemens Ladisch6e2efaa2009-08-10 10:06:53 +02001912 *den = 96000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return 0;
1914}
1915
Takashi Iwai208a1b42005-11-17 14:53:41 +01001916static struct snd_timer_hardware snd_ymfpci_timer_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 .flags = SNDRV_TIMER_HW_AUTO,
Clemens Ladisch6e2efaa2009-08-10 10:06:53 +02001918 .resolution = 10417, /* 1 / 96 kHz = 10.41666...us */
1919 .ticks = 0x10000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 .start = snd_ymfpci_timer_start,
1921 .stop = snd_ymfpci_timer_stop,
1922 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1923};
1924
Bill Pembertone23e7a12012-12-06 12:35:10 -05001925int snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001927 struct snd_timer *timer = NULL;
1928 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 int err;
1930
1931 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1932 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1933 tid.card = chip->card->number;
1934 tid.device = device;
1935 tid.subdevice = 0;
1936 if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1937 strcpy(timer->name, "YMFPCI timer");
1938 timer->private_data = chip;
1939 timer->hw = snd_ymfpci_timer_hw;
1940 }
1941 chip->timer = timer;
1942 return err;
1943}
1944
1945
1946/*
1947 * proc interface
1948 */
1949
Takashi Iwai208a1b42005-11-17 14:53:41 +01001950static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1951 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Takashi Iwai208a1b42005-11-17 14:53:41 +01001953 struct snd_ymfpci *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 int i;
1955
1956 snd_iprintf(buffer, "YMFPCI\n\n");
1957 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1958 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1959}
1960
Bill Pembertone23e7a12012-12-06 12:35:10 -05001961static int snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Takashi Iwai47f27692019-02-04 16:01:39 +01001963 return snd_card_ro_proc_new(card, "ymfpci", chip, snd_ymfpci_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
1966/*
1967 * initialization routines
1968 */
1969
1970static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1971{
1972 u8 cmd;
1973
1974 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1975#if 0 // force to reset
1976 if (cmd & 0x03) {
1977#endif
1978 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1979 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1980 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1981 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1982 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1983#if 0
1984 }
1985#endif
1986}
1987
Takashi Iwai208a1b42005-11-17 14:53:41 +01001988static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
1990 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1991}
1992
Takashi Iwai208a1b42005-11-17 14:53:41 +01001993static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 u32 val;
1996 int timeout = 1000;
1997
1998 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1999 if (val)
2000 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
2001 while (timeout-- > 0) {
2002 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
2003 if ((val & 0x00000002) == 0)
2004 break;
2005 }
2006}
2007
Clemens Ladisch102fa902006-10-11 12:05:59 +02002008static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2009{
2010 int err, is_1e;
2011 const char *name;
2012
2013 err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2014 &chip->pci->dev);
2015 if (err >= 0) {
David Woodhouseb82a82d2008-05-29 14:40:00 +03002016 if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
Takashi Iwai6436bcf2014-02-26 12:18:27 +01002017 dev_err(chip->card->dev,
2018 "DSP microcode has wrong size\n");
Clemens Ladisch102fa902006-10-11 12:05:59 +02002019 err = -EINVAL;
2020 }
2021 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002022 if (err < 0)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002023 return err;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002024 is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2025 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2026 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2027 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2028 name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2029 err = request_firmware(&chip->controller_microcode, name,
2030 &chip->pci->dev);
2031 if (err >= 0) {
David Woodhouseb82a82d2008-05-29 14:40:00 +03002032 if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
Takashi Iwai6436bcf2014-02-26 12:18:27 +01002033 dev_err(chip->card->dev,
2034 "controller microcode has wrong size\n");
Clemens Ladisch102fa902006-10-11 12:05:59 +02002035 err = -EINVAL;
2036 }
2037 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002038 if (err < 0)
Clemens Ladisch102fa902006-10-11 12:05:59 +02002039 return err;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002040 return 0;
2041}
Clemens Ladisch7e0af292007-05-03 17:59:54 +02002042
2043MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
2044MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
2045MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
2046
Takashi Iwai208a1b42005-11-17 14:53:41 +01002047static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 int i;
2050 u16 ctrl;
David Woodhouseb82a82d2008-05-29 14:40:00 +03002051 const __le32 *inst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2054 snd_ymfpci_disable_dsp(chip);
2055 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2056 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2057 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2058 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2059 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2060 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2061 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2062 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2063 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2064
2065 /* setup DSP instruction code */
David Woodhouseb82a82d2008-05-29 14:40:00 +03002066 inst = (const __le32 *)chip->dsp_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
David Woodhouseb82a82d2008-05-29 14:40:00 +03002068 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2069 le32_to_cpu(inst[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 /* setup control instruction code */
David Woodhouseb82a82d2008-05-29 14:40:00 +03002072 inst = (const __le32 *)chip->controller_microcode->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
David Woodhouseb82a82d2008-05-29 14:40:00 +03002074 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2075 le32_to_cpu(inst[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 snd_ymfpci_enable_dsp(chip);
2078}
2079
Bill Pembertone23e7a12012-12-06 12:35:10 -05002080static int snd_ymfpci_memalloc(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 long size, playback_ctrl_size;
2083 int voice, bank, reg;
2084 u8 *ptr;
2085 dma_addr_t ptr_addr;
2086
2087 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2088 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2089 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2090 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2091 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2092
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002093 size = ALIGN(playback_ctrl_size, 0x100) +
2094 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2095 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2096 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 chip->work_size;
2098 /* work_ptr must be aligned to 256 bytes, but it's already
2099 covered with the kernel page allocation mechanism */
Takashi Iwai6974f8a2019-11-05 16:18:55 +01002100 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 size, &chip->work_ptr) < 0)
2102 return -ENOMEM;
2103 ptr = chip->work_ptr.area;
2104 ptr_addr = chip->work_ptr.addr;
2105 memset(ptr, 0, size); /* for sure */
2106
2107 chip->bank_base_playback = ptr;
2108 chip->bank_base_playback_addr = ptr_addr;
Takashi Iwaid3c63762018-07-25 23:24:01 +02002109 chip->ctrl_playback = (__le32 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002111 ptr += ALIGN(playback_ctrl_size, 0x100);
2112 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2114 chip->voices[voice].number = voice;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002115 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 chip->voices[voice].bank_addr = ptr_addr;
2117 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002118 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 ptr += chip->bank_size_playback;
2120 ptr_addr += chip->bank_size_playback;
2121 }
2122 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002123 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2124 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 chip->bank_base_capture = ptr;
2126 chip->bank_base_capture_addr = ptr_addr;
2127 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2128 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002129 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 ptr += chip->bank_size_capture;
2131 ptr_addr += chip->bank_size_capture;
2132 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002133 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2134 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 chip->bank_base_effect = ptr;
2136 chip->bank_base_effect_addr = ptr_addr;
2137 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2138 for (bank = 0; bank < 2; bank++) {
Takashi Iwai208a1b42005-11-17 14:53:41 +01002139 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 ptr += chip->bank_size_effect;
2141 ptr_addr += chip->bank_size_effect;
2142 }
Clemens Ladisch7ab39922006-10-09 08:13:32 +02002143 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2144 ptr_addr = ALIGN(ptr_addr, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 chip->work_base = ptr;
2146 chip->work_base_addr = ptr_addr;
2147
Takashi Iwaida3cec32008-08-08 17:12:14 +02002148 snd_BUG_ON(ptr + chip->work_size !=
2149 chip->work_ptr.area + chip->work_ptr.bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2152 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2153 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2154 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2155 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2156
2157 /* S/PDIF output initialization */
2158 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2159 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2160 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2161
2162 /* S/PDIF input initialization */
2163 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2164
2165 /* digital mixer setup */
2166 for (reg = 0x80; reg < 0xc0; reg += 4)
2167 snd_ymfpci_writel(chip, reg, 0);
2168 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
Takashi Iwai4a3b6982008-06-25 17:17:00 +02002169 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2171 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2172 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2173 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2174 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2175 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2176
2177 return 0;
2178}
2179
Takashi Iwai208a1b42005-11-17 14:53:41 +01002180static int snd_ymfpci_free(struct snd_ymfpci *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 u16 ctrl;
2183
Takashi Iwaida3cec32008-08-08 17:12:14 +02002184 if (snd_BUG_ON(!chip))
2185 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 if (chip->res_reg_area) { /* don't touch busy hardware */
2188 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2189 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2190 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2191 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2192 snd_ymfpci_disable_dsp(chip);
2193 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2194 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2195 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2196 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2197 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2198 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2199 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2200 }
2201
2202 snd_ymfpci_ac3_done(chip);
2203
2204 /* Set PCI device to D3 state */
2205#if 0
2206 /* FIXME: temporarily disabled, otherwise we cannot fire up
2207 * the chip again unless reboot. ACPI bug?
2208 */
Yijing Wangdb10e7f2013-06-27 20:55:11 +08002209 pci_set_power_state(chip->pci, PCI_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210#endif
2211
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002212#ifdef CONFIG_PM_SLEEP
Takashi Iwai7009fa52012-11-22 16:23:22 +01002213 kfree(chip->saved_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214#endif
Takashi Iwai95866d382008-03-22 10:11:08 +01002215 if (chip->irq >= 0)
2216 free_irq(chip->irq, chip);
Takashi Iwaib1d57762005-10-10 11:56:31 +02002217 release_and_free_resource(chip->mpu_res);
2218 release_and_free_resource(chip->fm_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 snd_ymfpci_free_gameport(chip);
Markus Elfringff6defa2015-01-03 22:55:54 +01002220 iounmap(chip->reg_area_virt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (chip->work_ptr.area)
2222 snd_dma_free_pages(&chip->work_ptr);
2223
Takashi Iwaib1d57762005-10-10 11:56:31 +02002224 release_and_free_resource(chip->res_reg_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2227
2228 pci_disable_device(chip->pci);
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002229 release_firmware(chip->dsp_microcode);
2230 release_firmware(chip->controller_microcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 kfree(chip);
2232 return 0;
2233}
2234
Takashi Iwai208a1b42005-11-17 14:53:41 +01002235static int snd_ymfpci_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002237 struct snd_ymfpci *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return snd_ymfpci_free(chip);
2239}
2240
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002241#ifdef CONFIG_PM_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242static int saved_regs_index[] = {
2243 /* spdif */
2244 YDSXGR_SPDIFOUTCTRL,
2245 YDSXGR_SPDIFOUTSTATUS,
2246 YDSXGR_SPDIFINCTRL,
2247 /* volumes */
2248 YDSXGR_PRIADCLOOPVOL,
2249 YDSXGR_NATIVEDACINVOL,
2250 YDSXGR_NATIVEDACOUTVOL,
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01002251 YDSXGR_BUF441OUTVOL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 YDSXGR_NATIVEADCINVOL,
2253 YDSXGR_SPDIFLOOPVOL,
2254 YDSXGR_SPDIFOUTVOL,
2255 YDSXGR_ZVOUTVOL,
2256 YDSXGR_LEGACYOUTVOL,
2257 /* address bases */
2258 YDSXGR_PLAYCTRLBASE,
2259 YDSXGR_RECCTRLBASE,
2260 YDSXGR_EFFCTRLBASE,
2261 YDSXGR_WORKBASE,
2262 /* capture set up */
2263 YDSXGR_MAPOFREC,
2264 YDSXGR_RECFORMAT,
2265 YDSXGR_RECSLOTSR,
2266 YDSXGR_ADCFORMAT,
2267 YDSXGR_ADCSLOTSR,
2268};
2269#define YDSXGR_NUM_SAVED_REGS ARRAY_SIZE(saved_regs_index)
2270
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002271static int snd_ymfpci_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002273 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwaided46232005-11-17 16:09:43 +01002274 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 unsigned int i;
2276
Takashi Iwaided46232005-11-17 16:09:43 +01002277 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 snd_ac97_suspend(chip->ac97);
2279 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2280 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2281 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
Takashi Iwai28aa1652012-03-13 08:07:41 +01002282 pci_read_config_word(chip->pci, PCIR_DSXG_LEGACY,
2283 &chip->saved_dsxg_legacy);
2284 pci_read_config_word(chip->pci, PCIR_DSXG_ELEGACY,
2285 &chip->saved_dsxg_elegacy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
Takashi Iwai4a3b6982008-06-25 17:17:00 +02002287 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 snd_ymfpci_disable_dsp(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return 0;
2290}
2291
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002292static int snd_ymfpci_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002294 struct pci_dev *pci = to_pci_dev(dev);
2295 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwaided46232005-11-17 16:09:43 +01002296 struct snd_ymfpci *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 unsigned int i;
2298
Takashi Iwaided46232005-11-17 16:09:43 +01002299 snd_ymfpci_aclink_reset(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 snd_ymfpci_codec_ready(chip, 0);
2301 snd_ymfpci_download_image(chip);
2302 udelay(100);
2303
2304 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2305 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2306
2307 snd_ac97_resume(chip->ac97);
2308
Takashi Iwai28aa1652012-03-13 08:07:41 +01002309 pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY,
2310 chip->saved_dsxg_legacy);
2311 pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY,
2312 chip->saved_dsxg_elegacy);
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 /* start hw again */
2315 if (chip->start_count > 0) {
2316 spin_lock_irq(&chip->reg_lock);
2317 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2318 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2319 spin_unlock_irq(&chip->reg_lock);
2320 }
Takashi Iwaided46232005-11-17 16:09:43 +01002321 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return 0;
2323}
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002324
2325SIMPLE_DEV_PM_OPS(snd_ymfpci_pm, snd_ymfpci_suspend, snd_ymfpci_resume);
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002326#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Bill Pembertone23e7a12012-12-06 12:35:10 -05002328int snd_ymfpci_create(struct snd_card *card,
2329 struct pci_dev *pci,
2330 unsigned short old_legacy_ctrl,
2331 struct snd_ymfpci **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
Takashi Iwai208a1b42005-11-17 14:53:41 +01002333 struct snd_ymfpci *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 int err;
Takashi Iwai208a1b42005-11-17 14:53:41 +01002335 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 .dev_free = snd_ymfpci_dev_free,
2337 };
2338
2339 *rchip = NULL;
2340
2341 /* enable PCI device */
2342 if ((err = pci_enable_device(pci)) < 0)
2343 return err;
2344
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002345 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (chip == NULL) {
2347 pci_disable_device(pci);
2348 return -ENOMEM;
2349 }
2350 chip->old_legacy_ctrl = old_legacy_ctrl;
2351 spin_lock_init(&chip->reg_lock);
2352 spin_lock_init(&chip->voice_lock);
2353 init_waitqueue_head(&chip->interrupt_sleep);
2354 atomic_set(&chip->interrupt_sleep_count, 0);
2355 chip->card = card;
2356 chip->pci = pci;
2357 chip->irq = -1;
2358 chip->device_id = pci->device;
Auke Kok44c10132007-06-08 15:46:36 -07002359 chip->rev = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 chip->reg_area_phys = pci_resource_start(pci, 0);
2361 chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2362 pci_set_master(pci);
Teru KAMOGASHIRA9ed12612006-12-04 18:03:53 +01002363 chip->src441_used = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
Takashi Iwai6436bcf2014-02-26 12:18:27 +01002366 dev_err(chip->card->dev,
2367 "unable to grab memory region 0x%lx-0x%lx\n",
2368 chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
Markus Elfringba2186e2017-09-06 21:12:51 +02002369 err = -EBUSY;
2370 goto free_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
Takashi Iwai437a5a42006-11-21 12:14:23 +01002372 if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002373 KBUILD_MODNAME, chip)) {
Takashi Iwai6436bcf2014-02-26 12:18:27 +01002374 dev_err(chip->card->dev, "unable to grab IRQ %d\n", pci->irq);
Markus Elfringba2186e2017-09-06 21:12:51 +02002375 err = -EBUSY;
2376 goto free_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
2378 chip->irq = pci->irq;
2379
2380 snd_ymfpci_aclink_reset(pci);
2381 if (snd_ymfpci_codec_ready(chip, 0) < 0) {
Markus Elfringba2186e2017-09-06 21:12:51 +02002382 err = -EIO;
2383 goto free_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
2385
Clemens Ladisch102fa902006-10-11 12:05:59 +02002386 err = snd_ymfpci_request_firmware(chip);
2387 if (err < 0) {
Takashi Iwai6436bcf2014-02-26 12:18:27 +01002388 dev_err(chip->card->dev, "firmware request failed: %d\n", err);
Markus Elfringba2186e2017-09-06 21:12:51 +02002389 goto free_chip;
Clemens Ladisch102fa902006-10-11 12:05:59 +02002390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 snd_ymfpci_download_image(chip);
2392
2393 udelay(100); /* seems we need a delay after downloading image.. */
2394
2395 if (snd_ymfpci_memalloc(chip) < 0) {
Markus Elfringba2186e2017-09-06 21:12:51 +02002396 err = -EIO;
2397 goto free_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399
Markus Elfringba2186e2017-09-06 21:12:51 +02002400 err = snd_ymfpci_ac3_init(chip);
2401 if (err < 0)
2402 goto free_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002404#ifdef CONFIG_PM_SLEEP
Kees Cook6da2ec52018-06-12 13:55:00 -07002405 chip->saved_regs = kmalloc_array(YDSXGR_NUM_SAVED_REGS, sizeof(u32),
2406 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (chip->saved_regs == NULL) {
Markus Elfringba2186e2017-09-06 21:12:51 +02002408 err = -ENOMEM;
2409 goto free_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411#endif
2412
Markus Elfringba2186e2017-09-06 21:12:51 +02002413 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
2414 if (err < 0)
2415 goto free_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 snd_ymfpci_proc_init(card, chip);
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 *rchip = chip;
2420 return 0;
Markus Elfringba2186e2017-09-06 21:12:51 +02002421
2422free_chip:
2423 snd_ymfpci_free(chip);
2424 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}