1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Routines for control of YMF724/740/744/754 chips
5 */
6
7 #include <linux/delay.h>
8 #include <linux/firmware.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/pci.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/module.h>
16 #include <linux/io.h>
17
18 #include <sound/core.h>
19 #include <sound/control.h>
20 #include <sound/info.h>
21 #include <sound/tlv.h>
22 #include "ymfpci.h"
23 #include <sound/asoundef.h>
24 #include <sound/mpu401.h>
25
26 #include <asm/byteorder.h>
27
28 /*
29 * common I/O routines
30 */
31
32 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
33
snd_ymfpci_writeb(struct snd_ymfpci * chip,u32 offset,u8 val)34 static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
35 {
36 writeb(val, chip->reg_area_virt + offset);
37 }
38
snd_ymfpci_readw(struct snd_ymfpci * chip,u32 offset)39 static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
40 {
41 return readw(chip->reg_area_virt + offset);
42 }
43
snd_ymfpci_writew(struct snd_ymfpci * chip,u32 offset,u16 val)44 static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
45 {
46 writew(val, chip->reg_area_virt + offset);
47 }
48
snd_ymfpci_readl(struct snd_ymfpci * chip,u32 offset)49 static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
50 {
51 return readl(chip->reg_area_virt + offset);
52 }
53
snd_ymfpci_writel(struct snd_ymfpci * chip,u32 offset,u32 val)54 static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
55 {
56 writel(val, chip->reg_area_virt + offset);
57 }
58
snd_ymfpci_codec_ready(struct snd_ymfpci * chip,int secondary)59 static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
60 {
61 unsigned long end_time;
62 u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
63
64 end_time = jiffies + msecs_to_jiffies(750);
65 do {
66 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
67 return 0;
68 schedule_timeout_uninterruptible(1);
69 } while (time_before(jiffies, end_time));
70 dev_err(chip->card->dev,
71 "codec_ready: codec %i is not ready [0x%x]\n",
72 secondary, snd_ymfpci_readw(chip, reg));
73 return -EBUSY;
74 }
75
snd_ymfpci_codec_write(struct snd_ac97 * ac97,u16 reg,u16 val)76 static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
77 {
78 struct snd_ymfpci *chip = ac97->private_data;
79 u32 cmd;
80
81 snd_ymfpci_codec_ready(chip, 0);
82 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
83 snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
84 }
85
snd_ymfpci_codec_read(struct snd_ac97 * ac97,u16 reg)86 static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
87 {
88 struct snd_ymfpci *chip = ac97->private_data;
89
90 if (snd_ymfpci_codec_ready(chip, 0))
91 return ~0;
92 snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
93 if (snd_ymfpci_codec_ready(chip, 0))
94 return ~0;
95 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
96 int i;
97 for (i = 0; i < 600; i++)
98 snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
99 }
100 return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
101 }
102
103 /*
104 * Misc routines
105 */
106
snd_ymfpci_calc_delta(u32 rate)107 static u32 snd_ymfpci_calc_delta(u32 rate)
108 {
109 switch (rate) {
110 case 8000: return 0x02aaab00;
111 case 11025: return 0x03accd00;
112 case 16000: return 0x05555500;
113 case 22050: return 0x07599a00;
114 case 32000: return 0x0aaaab00;
115 case 44100: return 0x0eb33300;
116 default: return ((rate << 16) / 375) << 5;
117 }
118 }
119
120 static const u32 def_rate[8] = {
121 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
122 };
123
snd_ymfpci_calc_lpfK(u32 rate)124 static u32 snd_ymfpci_calc_lpfK(u32 rate)
125 {
126 u32 i;
127 static const u32 val[8] = {
128 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
129 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
130 };
131
132 if (rate == 44100)
133 return 0x40000000; /* FIXME: What's the right value? */
134 for (i = 0; i < 8; i++)
135 if (rate <= def_rate[i])
136 return val[i];
137 return val[0];
138 }
139
snd_ymfpci_calc_lpfQ(u32 rate)140 static u32 snd_ymfpci_calc_lpfQ(u32 rate)
141 {
142 u32 i;
143 static const u32 val[8] = {
144 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
145 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
146 };
147
148 if (rate == 44100)
149 return 0x370A0000;
150 for (i = 0; i < 8; i++)
151 if (rate <= def_rate[i])
152 return val[i];
153 return val[0];
154 }
155
156 /*
157 * Hardware start management
158 */
159
snd_ymfpci_hw_start(struct snd_ymfpci * chip)160 static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
161 {
162 guard(spinlock_irqsave)(&chip->reg_lock);
163 if (chip->start_count++ > 0)
164 return;
165 snd_ymfpci_writel(chip, YDSXGR_MODE,
166 snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
167 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
168 }
169
snd_ymfpci_hw_stop(struct snd_ymfpci * chip)170 static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
171 {
172 long timeout = 1000;
173
174 guard(spinlock_irqsave)(&chip->reg_lock);
175 if (--chip->start_count > 0)
176 return;
177 snd_ymfpci_writel(chip, YDSXGR_MODE,
178 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
179 while (timeout-- > 0) {
180 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
181 break;
182 }
183 if (atomic_read(&chip->interrupt_sleep_count)) {
184 atomic_set(&chip->interrupt_sleep_count, 0);
185 wake_up(&chip->interrupt_sleep);
186 }
187 }
188
189 /*
190 * Playback voice management
191 */
192
voice_alloc(struct snd_ymfpci * chip,enum snd_ymfpci_voice_type type,int pair,struct snd_ymfpci_voice ** rvoice)193 static int voice_alloc(struct snd_ymfpci *chip,
194 enum snd_ymfpci_voice_type type, int pair,
195 struct snd_ymfpci_voice **rvoice)
196 {
197 struct snd_ymfpci_voice *voice, *voice2;
198 int idx;
199
200 *rvoice = NULL;
201 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
202 voice = &chip->voices[idx];
203 voice2 = pair ? &chip->voices[idx+1] : NULL;
204 if (voice->use || (voice2 && voice2->use))
205 continue;
206 voice->use = 1;
207 if (voice2)
208 voice2->use = 1;
209 switch (type) {
210 case YMFPCI_PCM:
211 voice->pcm = 1;
212 if (voice2)
213 voice2->pcm = 1;
214 break;
215 case YMFPCI_SYNTH:
216 voice->synth = 1;
217 break;
218 case YMFPCI_MIDI:
219 voice->midi = 1;
220 break;
221 }
222 snd_ymfpci_hw_start(chip);
223 if (voice2)
224 snd_ymfpci_hw_start(chip);
225 *rvoice = voice;
226 return 0;
227 }
228 return -ENOMEM;
229 }
230
snd_ymfpci_voice_alloc(struct snd_ymfpci * chip,enum snd_ymfpci_voice_type type,int pair,struct snd_ymfpci_voice ** rvoice)231 static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
232 enum snd_ymfpci_voice_type type, int pair,
233 struct snd_ymfpci_voice **rvoice)
234 {
235 int result;
236
237 if (snd_BUG_ON(!rvoice))
238 return -EINVAL;
239 if (snd_BUG_ON(pair && type != YMFPCI_PCM))
240 return -EINVAL;
241
242 guard(spinlock_irqsave)(&chip->voice_lock);
243 for (;;) {
244 result = voice_alloc(chip, type, pair, rvoice);
245 if (result == 0 || type != YMFPCI_PCM)
246 break;
247 /* TODO: synth/midi voice deallocation */
248 break;
249 }
250 return result;
251 }
252
snd_ymfpci_voice_free(struct snd_ymfpci * chip,struct snd_ymfpci_voice * pvoice)253 static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
254 {
255 if (snd_BUG_ON(!pvoice))
256 return -EINVAL;
257 snd_ymfpci_hw_stop(chip);
258 guard(spinlock_irqsave)(&chip->voice_lock);
259 if (pvoice->number == chip->src441_used) {
260 chip->src441_used = -1;
261 pvoice->ypcm->use_441_slot = 0;
262 }
263 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
264 pvoice->ypcm = NULL;
265 pvoice->interrupt = NULL;
266 return 0;
267 }
268
269 /*
270 * PCM part
271 */
272
snd_ymfpci_pcm_interrupt(struct snd_ymfpci * chip,struct snd_ymfpci_voice * voice)273 static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
274 {
275 struct snd_ymfpci_pcm *ypcm;
276 u32 pos, delta;
277
278 ypcm = voice->ypcm;
279 if (!ypcm)
280 return;
281 if (ypcm->substream == NULL)
282 return;
283 guard(spinlock)(&chip->reg_lock);
284 if (ypcm->running) {
285 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
286 if (pos < ypcm->last_pos)
287 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
288 else
289 delta = pos - ypcm->last_pos;
290 ypcm->period_pos += delta;
291 ypcm->last_pos = pos;
292 if (ypcm->period_pos >= ypcm->period_size) {
293 /*
294 dev_dbg(chip->card->dev,
295 "done - active_bank = 0x%x, start = 0x%x\n",
296 chip->active_bank,
297 voice->bank[chip->active_bank].start);
298 */
299 ypcm->period_pos %= ypcm->period_size;
300 spin_unlock(&chip->reg_lock);
301 snd_pcm_period_elapsed(ypcm->substream);
302 spin_lock(&chip->reg_lock);
303 }
304
305 if (unlikely(ypcm->update_pcm_vol)) {
306 unsigned int subs = ypcm->substream->number;
307 unsigned int next_bank = 1 - chip->active_bank;
308 struct snd_ymfpci_playback_bank *bank;
309 __le32 volume;
310
311 bank = &voice->bank[next_bank];
312 volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
313 bank->left_gain_end = volume;
314 if (ypcm->output_rear)
315 bank->eff2_gain_end = volume;
316 if (ypcm->voices[1])
317 bank = &ypcm->voices[1]->bank[next_bank];
318 volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
319 bank->right_gain_end = volume;
320 if (ypcm->output_rear)
321 bank->eff3_gain_end = volume;
322 ypcm->update_pcm_vol--;
323 }
324 }
325 }
326
snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream * substream)327 static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
328 {
329 struct snd_pcm_runtime *runtime = substream->runtime;
330 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
331 struct snd_ymfpci *chip = ypcm->chip;
332 u32 pos, delta;
333
334 guard(spinlock)(&chip->reg_lock);
335 if (ypcm->running) {
336 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
337 if (pos < ypcm->last_pos)
338 delta = pos + (ypcm->buffer_size - ypcm->last_pos);
339 else
340 delta = pos - ypcm->last_pos;
341 ypcm->period_pos += delta;
342 ypcm->last_pos = pos;
343 if (ypcm->period_pos >= ypcm->period_size) {
344 ypcm->period_pos %= ypcm->period_size;
345 /*
346 dev_dbg(chip->card->dev,
347 "done - active_bank = 0x%x, start = 0x%x\n",
348 chip->active_bank,
349 voice->bank[chip->active_bank].start);
350 */
351 spin_unlock(&chip->reg_lock);
352 snd_pcm_period_elapsed(substream);
353 spin_lock(&chip->reg_lock);
354 }
355 }
356 }
357
snd_ymfpci_playback_trigger(struct snd_pcm_substream * substream,int cmd)358 static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
359 int cmd)
360 {
361 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
362 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
363 struct snd_kcontrol *kctl = NULL;
364 int result = 0;
365
366 guard(spinlock)(&chip->reg_lock);
367 if (ypcm->voices[0] == NULL)
368 return -EINVAL;
369 switch (cmd) {
370 case SNDRV_PCM_TRIGGER_START:
371 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
372 case SNDRV_PCM_TRIGGER_RESUME:
373 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
374 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
375 chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
376 ypcm->running = 1;
377 break;
378 case SNDRV_PCM_TRIGGER_STOP:
379 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
380 kctl = chip->pcm_mixer[substream->number].ctl;
381 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
382 }
383 fallthrough;
384 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
385 case SNDRV_PCM_TRIGGER_SUSPEND:
386 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
387 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
388 chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
389 ypcm->running = 0;
390 break;
391 default:
392 return -EINVAL;
393 }
394 if (kctl)
395 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
396 return result;
397 }
snd_ymfpci_capture_trigger(struct snd_pcm_substream * substream,int cmd)398 static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
399 int cmd)
400 {
401 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
402 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
403 int result = 0;
404 u32 tmp;
405
406 guard(spinlock)(&chip->reg_lock);
407 switch (cmd) {
408 case SNDRV_PCM_TRIGGER_START:
409 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
410 case SNDRV_PCM_TRIGGER_RESUME:
411 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
412 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
413 ypcm->running = 1;
414 break;
415 case SNDRV_PCM_TRIGGER_STOP:
416 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
417 case SNDRV_PCM_TRIGGER_SUSPEND:
418 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
419 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
420 ypcm->running = 0;
421 break;
422 default:
423 result = -EINVAL;
424 break;
425 }
426 return result;
427 }
428
snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm * ypcm,int voices)429 static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
430 {
431 int err;
432
433 if (ypcm->voices[1] != NULL && voices < 2) {
434 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
435 ypcm->voices[1] = NULL;
436 }
437 if (voices == 1 && ypcm->voices[0] != NULL)
438 return 0; /* already allocated */
439 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
440 return 0; /* already allocated */
441 if (voices > 1) {
442 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
443 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
444 ypcm->voices[0] = NULL;
445 }
446 }
447 err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
448 if (err < 0)
449 return err;
450 ypcm->voices[0]->ypcm = ypcm;
451 ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
452 if (voices > 1) {
453 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
454 ypcm->voices[1]->ypcm = ypcm;
455 }
456 return 0;
457 }
458
snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm * ypcm,unsigned int voiceidx,struct snd_pcm_runtime * runtime,int has_pcm_volume)459 static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
460 struct snd_pcm_runtime *runtime,
461 int has_pcm_volume)
462 {
463 struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
464 u32 format;
465 u32 delta = snd_ymfpci_calc_delta(runtime->rate);
466 u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
467 u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
468 struct snd_ymfpci_playback_bank *bank;
469 unsigned int nbank;
470 __le32 vol_left, vol_right;
471 u8 use_left, use_right;
472
473 if (snd_BUG_ON(!voice))
474 return;
475 if (runtime->channels == 1) {
476 use_left = 1;
477 use_right = 1;
478 } else {
479 use_left = (voiceidx & 1) == 0;
480 use_right = !use_left;
481 }
482 if (has_pcm_volume) {
483 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
484 [ypcm->substream->number].left << 15);
485 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
486 [ypcm->substream->number].right << 15);
487 } else {
488 vol_left = cpu_to_le32(0x40000000);
489 vol_right = cpu_to_le32(0x40000000);
490 }
491 scoped_guard(spinlock_irqsave, &ypcm->chip->voice_lock) {
492 format = runtime->channels == 2 ? 0x00010000 : 0;
493 if (snd_pcm_format_width(runtime->format) == 8)
494 format |= 0x80000000;
495 else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
496 runtime->rate == 44100 && runtime->channels == 2 &&
497 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
498 ypcm->chip->src441_used == voice->number)) {
499 ypcm->chip->src441_used = voice->number;
500 ypcm->use_441_slot = 1;
501 format |= 0x10000000;
502 }
503 if (ypcm->chip->src441_used == voice->number &&
504 (format & 0x10000000) == 0) {
505 ypcm->chip->src441_used = -1;
506 ypcm->use_441_slot = 0;
507 }
508 if (runtime->channels == 2 && (voiceidx & 1) != 0)
509 format |= 1;
510 }
511 for (nbank = 0; nbank < 2; nbank++) {
512 bank = &voice->bank[nbank];
513 memset(bank, 0, sizeof(*bank));
514 bank->format = cpu_to_le32(format);
515 bank->base = cpu_to_le32(runtime->dma_addr);
516 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
517 bank->lpfQ = cpu_to_le32(lpfQ);
518 bank->delta =
519 bank->delta_end = cpu_to_le32(delta);
520 bank->lpfK =
521 bank->lpfK_end = cpu_to_le32(lpfK);
522 bank->eg_gain =
523 bank->eg_gain_end = cpu_to_le32(0x40000000);
524
525 if (ypcm->output_front) {
526 if (use_left) {
527 bank->left_gain =
528 bank->left_gain_end = vol_left;
529 }
530 if (use_right) {
531 bank->right_gain =
532 bank->right_gain_end = vol_right;
533 }
534 }
535 if (ypcm->output_rear) {
536 if (!ypcm->swap_rear) {
537 if (use_left) {
538 bank->eff2_gain =
539 bank->eff2_gain_end = vol_left;
540 }
541 if (use_right) {
542 bank->eff3_gain =
543 bank->eff3_gain_end = vol_right;
544 }
545 } else {
546 /* The SPDIF out channels seem to be swapped, so we have
547 * to swap them here, too. The rear analog out channels
548 * will be wrong, but otherwise AC3 would not work.
549 */
550 if (use_left) {
551 bank->eff3_gain =
552 bank->eff3_gain_end = vol_left;
553 }
554 if (use_right) {
555 bank->eff2_gain =
556 bank->eff2_gain_end = vol_right;
557 }
558 }
559 }
560 }
561 }
562
snd_ymfpci_ac3_init(struct snd_ymfpci * chip)563 static int snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
564 {
565 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
566 4096, &chip->ac3_tmp_base) < 0)
567 return -ENOMEM;
568
569 chip->bank_effect[3][0]->base =
570 chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
571 chip->bank_effect[3][0]->loop_end =
572 chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
573 chip->bank_effect[4][0]->base =
574 chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
575 chip->bank_effect[4][0]->loop_end =
576 chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
577
578 guard(spinlock_irq)(&chip->reg_lock);
579 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
580 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
581 return 0;
582 }
583
snd_ymfpci_ac3_done(struct snd_ymfpci * chip)584 static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
585 {
586 scoped_guard(spinlock_irq, &chip->reg_lock) {
587 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
588 snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
589 }
590 // snd_ymfpci_irq_wait(chip);
591 if (chip->ac3_tmp_base.area) {
592 snd_dma_free_pages(&chip->ac3_tmp_base);
593 chip->ac3_tmp_base.area = NULL;
594 }
595 return 0;
596 }
597
snd_ymfpci_playback_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)598 static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
599 struct snd_pcm_hw_params *hw_params)
600 {
601 struct snd_pcm_runtime *runtime = substream->runtime;
602 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
603 int err;
604
605 err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params));
606 if (err < 0)
607 return err;
608 return 0;
609 }
610
snd_ymfpci_playback_hw_free(struct snd_pcm_substream * substream)611 static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
612 {
613 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
614 struct snd_pcm_runtime *runtime = substream->runtime;
615 struct snd_ymfpci_pcm *ypcm;
616
617 if (runtime->private_data == NULL)
618 return 0;
619 ypcm = runtime->private_data;
620
621 /* wait, until the PCI operations are not finished */
622 snd_ymfpci_irq_wait(chip);
623 if (ypcm->voices[1]) {
624 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
625 ypcm->voices[1] = NULL;
626 }
627 if (ypcm->voices[0]) {
628 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
629 ypcm->voices[0] = NULL;
630 }
631 return 0;
632 }
633
snd_ymfpci_playback_prepare(struct snd_pcm_substream * substream)634 static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
635 {
636 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
637 struct snd_pcm_runtime *runtime = substream->runtime;
638 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
639 struct snd_kcontrol *kctl;
640 unsigned int nvoice;
641
642 ypcm->period_size = runtime->period_size;
643 ypcm->buffer_size = runtime->buffer_size;
644 ypcm->period_pos = 0;
645 ypcm->last_pos = 0;
646 for (nvoice = 0; nvoice < runtime->channels; nvoice++)
647 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
648 substream->pcm == chip->pcm);
649
650 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
651 kctl = chip->pcm_mixer[substream->number].ctl;
652 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
653 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
654 }
655 return 0;
656 }
657
snd_ymfpci_capture_hw_free(struct snd_pcm_substream * substream)658 static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
659 {
660 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
661
662 /* wait, until the PCI operations are not finished */
663 snd_ymfpci_irq_wait(chip);
664 return 0;
665 }
666
snd_ymfpci_capture_prepare(struct snd_pcm_substream * substream)667 static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
668 {
669 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
670 struct snd_pcm_runtime *runtime = substream->runtime;
671 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
672 struct snd_ymfpci_capture_bank * bank;
673 int nbank;
674 u32 rate, format;
675
676 ypcm->period_size = runtime->period_size;
677 ypcm->buffer_size = runtime->buffer_size;
678 ypcm->period_pos = 0;
679 ypcm->last_pos = 0;
680 ypcm->shift = 0;
681 rate = ((48000 * 4096) / runtime->rate) - 1;
682 format = 0;
683 if (runtime->channels == 2) {
684 format |= 2;
685 ypcm->shift++;
686 }
687 if (snd_pcm_format_width(runtime->format) == 8)
688 format |= 1;
689 else
690 ypcm->shift++;
691 switch (ypcm->capture_bank_number) {
692 case 0:
693 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
694 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
695 break;
696 case 1:
697 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
698 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
699 break;
700 }
701 for (nbank = 0; nbank < 2; nbank++) {
702 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
703 bank->base = cpu_to_le32(runtime->dma_addr);
704 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
705 bank->start = 0;
706 bank->num_of_loops = 0;
707 }
708 return 0;
709 }
710
snd_ymfpci_playback_pointer(struct snd_pcm_substream * substream)711 static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
712 {
713 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
714 struct snd_pcm_runtime *runtime = substream->runtime;
715 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
716 struct snd_ymfpci_voice *voice = ypcm->voices[0];
717
718 if (!(ypcm->running && voice))
719 return 0;
720 return le32_to_cpu(voice->bank[chip->active_bank].start);
721 }
722
snd_ymfpci_capture_pointer(struct snd_pcm_substream * substream)723 static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
724 {
725 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
726 struct snd_pcm_runtime *runtime = substream->runtime;
727 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
728
729 if (!ypcm->running)
730 return 0;
731 return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
732 }
733
snd_ymfpci_irq_wait(struct snd_ymfpci * chip)734 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
735 {
736 wait_queue_entry_t wait;
737 int loops = 4;
738
739 while (loops-- > 0) {
740 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
741 continue;
742 init_waitqueue_entry(&wait, current);
743 add_wait_queue(&chip->interrupt_sleep, &wait);
744 atomic_inc(&chip->interrupt_sleep_count);
745 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
746 remove_wait_queue(&chip->interrupt_sleep, &wait);
747 }
748 }
749
snd_ymfpci_interrupt(int irq,void * dev_id)750 static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
751 {
752 struct snd_ymfpci *chip = dev_id;
753 u32 status, nvoice, mode;
754 struct snd_ymfpci_voice *voice;
755
756 status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
757 if (status & 0x80000000) {
758 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
759 scoped_guard(spinlock, &chip->voice_lock) {
760 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
761 voice = &chip->voices[nvoice];
762 if (voice->interrupt)
763 voice->interrupt(chip, voice);
764 }
765 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
766 if (chip->capture_substream[nvoice])
767 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
768 }
769 #if 0
770 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
771 if (chip->effect_substream[nvoice])
772 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
773 }
774 #endif
775 }
776 scoped_guard(spinlock, &chip->reg_lock) {
777 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
778 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
779 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
780 }
781
782 if (atomic_read(&chip->interrupt_sleep_count)) {
783 atomic_set(&chip->interrupt_sleep_count, 0);
784 wake_up(&chip->interrupt_sleep);
785 }
786 }
787
788 status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
789 if (status & 1) {
790 if (chip->timer)
791 snd_timer_interrupt(chip->timer, chip->timer_ticks);
792 }
793 snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
794
795 if (chip->rawmidi)
796 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
797 return IRQ_HANDLED;
798 }
799
800 static const struct snd_pcm_hardware snd_ymfpci_playback =
801 {
802 .info = (SNDRV_PCM_INFO_MMAP |
803 SNDRV_PCM_INFO_MMAP_VALID |
804 SNDRV_PCM_INFO_INTERLEAVED |
805 SNDRV_PCM_INFO_BLOCK_TRANSFER |
806 SNDRV_PCM_INFO_PAUSE |
807 SNDRV_PCM_INFO_RESUME),
808 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
809 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
810 .rate_min = 8000,
811 .rate_max = 48000,
812 .channels_min = 1,
813 .channels_max = 2,
814 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
815 .period_bytes_min = 64,
816 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
817 .periods_min = 3,
818 .periods_max = 1024,
819 .fifo_size = 0,
820 };
821
822 static const struct snd_pcm_hardware snd_ymfpci_capture =
823 {
824 .info = (SNDRV_PCM_INFO_MMAP |
825 SNDRV_PCM_INFO_MMAP_VALID |
826 SNDRV_PCM_INFO_INTERLEAVED |
827 SNDRV_PCM_INFO_BLOCK_TRANSFER |
828 SNDRV_PCM_INFO_PAUSE |
829 SNDRV_PCM_INFO_RESUME),
830 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
831 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
832 .rate_min = 8000,
833 .rate_max = 48000,
834 .channels_min = 1,
835 .channels_max = 2,
836 .buffer_bytes_max = 256 * 1024, /* FIXME: enough? */
837 .period_bytes_min = 64,
838 .period_bytes_max = 256 * 1024, /* FIXME: enough? */
839 .periods_min = 3,
840 .periods_max = 1024,
841 .fifo_size = 0,
842 };
843
snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime * runtime)844 static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
845 {
846 kfree(runtime->private_data);
847 }
848
snd_ymfpci_playback_open_1(struct snd_pcm_substream * substream)849 static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
850 {
851 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
852 struct snd_pcm_runtime *runtime = substream->runtime;
853 struct snd_ymfpci_pcm *ypcm;
854 int err;
855
856 runtime->hw = snd_ymfpci_playback;
857 /* FIXME? True value is 256/48 = 5.33333 ms */
858 err = snd_pcm_hw_constraint_minmax(runtime,
859 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
860 5334, UINT_MAX);
861 if (err < 0)
862 return err;
863 err = snd_pcm_hw_rule_noresample(runtime, 48000);
864 if (err < 0)
865 return err;
866
867 ypcm = kzalloc_obj(*ypcm);
868 if (ypcm == NULL)
869 return -ENOMEM;
870 ypcm->chip = chip;
871 ypcm->type = PLAYBACK_VOICE;
872 ypcm->substream = substream;
873 runtime->private_data = ypcm;
874 runtime->private_free = snd_ymfpci_pcm_free_substream;
875 return 0;
876 }
877
878 /* call with spinlock held */
ymfpci_open_extension(struct snd_ymfpci * chip)879 static void ymfpci_open_extension(struct snd_ymfpci *chip)
880 {
881 if (! chip->rear_opened) {
882 if (! chip->spdif_opened) /* set AC3 */
883 snd_ymfpci_writel(chip, YDSXGR_MODE,
884 snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
885 /* enable second codec (4CHEN) */
886 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
887 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
888 }
889 }
890
891 /* call with spinlock held */
ymfpci_close_extension(struct snd_ymfpci * chip)892 static void ymfpci_close_extension(struct snd_ymfpci *chip)
893 {
894 if (! chip->rear_opened) {
895 if (! chip->spdif_opened)
896 snd_ymfpci_writel(chip, YDSXGR_MODE,
897 snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
898 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
899 (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
900 }
901 }
902
snd_ymfpci_playback_open(struct snd_pcm_substream * substream)903 static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
904 {
905 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
906 struct snd_pcm_runtime *runtime = substream->runtime;
907 struct snd_ymfpci_pcm *ypcm;
908 int err;
909
910 err = snd_ymfpci_playback_open_1(substream);
911 if (err < 0)
912 return err;
913 ypcm = runtime->private_data;
914 ypcm->output_front = 1;
915 ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
916 ypcm->swap_rear = 0;
917 guard(spinlock_irq)(&chip->reg_lock);
918 if (ypcm->output_rear) {
919 ymfpci_open_extension(chip);
920 chip->rear_opened++;
921 }
922 return 0;
923 }
924
snd_ymfpci_playback_spdif_open(struct snd_pcm_substream * substream)925 static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
926 {
927 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
928 struct snd_pcm_runtime *runtime = substream->runtime;
929 struct snd_ymfpci_pcm *ypcm;
930 int err;
931
932 err = snd_ymfpci_playback_open_1(substream);
933 if (err < 0)
934 return err;
935 ypcm = runtime->private_data;
936 ypcm->output_front = 0;
937 ypcm->output_rear = 1;
938 ypcm->swap_rear = 1;
939 scoped_guard(spinlock_irq, &chip->reg_lock) {
940 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
941 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
942 ymfpci_open_extension(chip);
943 chip->spdif_pcm_bits = chip->spdif_bits;
944 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
945 chip->spdif_opened++;
946 }
947
948 chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
949 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
950 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
951 return 0;
952 }
953
snd_ymfpci_playback_4ch_open(struct snd_pcm_substream * substream)954 static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
955 {
956 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
957 struct snd_pcm_runtime *runtime = substream->runtime;
958 struct snd_ymfpci_pcm *ypcm;
959 int err;
960
961 err = snd_ymfpci_playback_open_1(substream);
962 if (err < 0)
963 return err;
964 ypcm = runtime->private_data;
965 ypcm->output_front = 0;
966 ypcm->output_rear = 1;
967 ypcm->swap_rear = 0;
968 guard(spinlock_irq)(&chip->reg_lock);
969 ymfpci_open_extension(chip);
970 chip->rear_opened++;
971 return 0;
972 }
973
snd_ymfpci_capture_open(struct snd_pcm_substream * substream,u32 capture_bank_number)974 static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
975 u32 capture_bank_number)
976 {
977 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
978 struct snd_pcm_runtime *runtime = substream->runtime;
979 struct snd_ymfpci_pcm *ypcm;
980 int err;
981
982 runtime->hw = snd_ymfpci_capture;
983 /* FIXME? True value is 256/48 = 5.33333 ms */
984 err = snd_pcm_hw_constraint_minmax(runtime,
985 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
986 5334, UINT_MAX);
987 if (err < 0)
988 return err;
989 err = snd_pcm_hw_rule_noresample(runtime, 48000);
990 if (err < 0)
991 return err;
992
993 ypcm = kzalloc_obj(*ypcm);
994 if (ypcm == NULL)
995 return -ENOMEM;
996 ypcm->chip = chip;
997 ypcm->type = capture_bank_number + CAPTURE_REC;
998 ypcm->substream = substream;
999 ypcm->capture_bank_number = capture_bank_number;
1000 chip->capture_substream[capture_bank_number] = substream;
1001 runtime->private_data = ypcm;
1002 runtime->private_free = snd_ymfpci_pcm_free_substream;
1003 snd_ymfpci_hw_start(chip);
1004 return 0;
1005 }
1006
snd_ymfpci_capture_rec_open(struct snd_pcm_substream * substream)1007 static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
1008 {
1009 return snd_ymfpci_capture_open(substream, 0);
1010 }
1011
snd_ymfpci_capture_ac97_open(struct snd_pcm_substream * substream)1012 static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
1013 {
1014 return snd_ymfpci_capture_open(substream, 1);
1015 }
1016
snd_ymfpci_playback_close_1(struct snd_pcm_substream * substream)1017 static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
1018 {
1019 return 0;
1020 }
1021
snd_ymfpci_playback_close(struct snd_pcm_substream * substream)1022 static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
1023 {
1024 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1025 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1026
1027 scoped_guard(spinlock_irq, &chip->reg_lock) {
1028 if (ypcm->output_rear && chip->rear_opened > 0) {
1029 chip->rear_opened--;
1030 ymfpci_close_extension(chip);
1031 }
1032 }
1033 return snd_ymfpci_playback_close_1(substream);
1034 }
1035
snd_ymfpci_playback_spdif_close(struct snd_pcm_substream * substream)1036 static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
1037 {
1038 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1039
1040 scoped_guard(spinlock_irq, &chip->reg_lock) {
1041 chip->spdif_opened = 0;
1042 ymfpci_close_extension(chip);
1043 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1044 snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1045 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1046 }
1047 chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1048 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1049 SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1050 return snd_ymfpci_playback_close_1(substream);
1051 }
1052
snd_ymfpci_playback_4ch_close(struct snd_pcm_substream * substream)1053 static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
1054 {
1055 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1056
1057 scoped_guard(spinlock_irq, &chip->reg_lock) {
1058 if (chip->rear_opened > 0) {
1059 chip->rear_opened--;
1060 ymfpci_close_extension(chip);
1061 }
1062 }
1063 return snd_ymfpci_playback_close_1(substream);
1064 }
1065
snd_ymfpci_capture_close(struct snd_pcm_substream * substream)1066 static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
1067 {
1068 struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1069 struct snd_pcm_runtime *runtime = substream->runtime;
1070 struct snd_ymfpci_pcm *ypcm = runtime->private_data;
1071
1072 if (ypcm != NULL) {
1073 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1074 snd_ymfpci_hw_stop(chip);
1075 }
1076 return 0;
1077 }
1078
1079 static const struct snd_pcm_ops snd_ymfpci_playback_ops = {
1080 .open = snd_ymfpci_playback_open,
1081 .close = snd_ymfpci_playback_close,
1082 .hw_params = snd_ymfpci_playback_hw_params,
1083 .hw_free = snd_ymfpci_playback_hw_free,
1084 .prepare = snd_ymfpci_playback_prepare,
1085 .trigger = snd_ymfpci_playback_trigger,
1086 .pointer = snd_ymfpci_playback_pointer,
1087 };
1088
1089 static const struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
1090 .open = snd_ymfpci_capture_rec_open,
1091 .close = snd_ymfpci_capture_close,
1092 .hw_free = snd_ymfpci_capture_hw_free,
1093 .prepare = snd_ymfpci_capture_prepare,
1094 .trigger = snd_ymfpci_capture_trigger,
1095 .pointer = snd_ymfpci_capture_pointer,
1096 };
1097
snd_ymfpci_pcm(struct snd_ymfpci * chip,int device)1098 int snd_ymfpci_pcm(struct snd_ymfpci *chip, int device)
1099 {
1100 struct snd_pcm *pcm;
1101 int err;
1102
1103 err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm);
1104 if (err < 0)
1105 return err;
1106 pcm->private_data = chip;
1107
1108 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1109 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1110
1111 /* global setup */
1112 pcm->info_flags = 0;
1113 strscpy(pcm->name, "YMFPCI");
1114 chip->pcm = pcm;
1115
1116 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1117 &chip->pci->dev, 64*1024, 256*1024);
1118
1119 return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1120 snd_pcm_std_chmaps, 2, 0, NULL);
1121 }
1122
1123 static const struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
1124 .open = snd_ymfpci_capture_ac97_open,
1125 .close = snd_ymfpci_capture_close,
1126 .hw_free = snd_ymfpci_capture_hw_free,
1127 .prepare = snd_ymfpci_capture_prepare,
1128 .trigger = snd_ymfpci_capture_trigger,
1129 .pointer = snd_ymfpci_capture_pointer,
1130 };
1131
snd_ymfpci_pcm2(struct snd_ymfpci * chip,int device)1132 int snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device)
1133 {
1134 struct snd_pcm *pcm;
1135 int err;
1136
1137 err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm);
1138 if (err < 0)
1139 return err;
1140 pcm->private_data = chip;
1141
1142 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1143
1144 /* global setup */
1145 pcm->info_flags = 0;
1146 sprintf(pcm->name, "YMFPCI - %s",
1147 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1148 chip->pcm2 = pcm;
1149
1150 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1151 &chip->pci->dev, 64*1024, 256*1024);
1152
1153 return 0;
1154 }
1155
1156 static const struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
1157 .open = snd_ymfpci_playback_spdif_open,
1158 .close = snd_ymfpci_playback_spdif_close,
1159 .hw_params = snd_ymfpci_playback_hw_params,
1160 .hw_free = snd_ymfpci_playback_hw_free,
1161 .prepare = snd_ymfpci_playback_prepare,
1162 .trigger = snd_ymfpci_playback_trigger,
1163 .pointer = snd_ymfpci_playback_pointer,
1164 };
1165
snd_ymfpci_pcm_spdif(struct snd_ymfpci * chip,int device)1166 int snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device)
1167 {
1168 struct snd_pcm *pcm;
1169 int err;
1170
1171 err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm);
1172 if (err < 0)
1173 return err;
1174 pcm->private_data = chip;
1175
1176 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1177
1178 /* global setup */
1179 pcm->info_flags = 0;
1180 strscpy(pcm->name, "YMFPCI - IEC958");
1181 chip->pcm_spdif = pcm;
1182
1183 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1184 &chip->pci->dev, 64*1024, 256*1024);
1185
1186 return 0;
1187 }
1188
1189 static const struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
1190 .open = snd_ymfpci_playback_4ch_open,
1191 .close = snd_ymfpci_playback_4ch_close,
1192 .hw_params = snd_ymfpci_playback_hw_params,
1193 .hw_free = snd_ymfpci_playback_hw_free,
1194 .prepare = snd_ymfpci_playback_prepare,
1195 .trigger = snd_ymfpci_playback_trigger,
1196 .pointer = snd_ymfpci_playback_pointer,
1197 };
1198
1199 static const struct snd_pcm_chmap_elem surround_map[] = {
1200 { .channels = 1,
1201 .map = { SNDRV_CHMAP_MONO } },
1202 { .channels = 2,
1203 .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
1204 { }
1205 };
1206
snd_ymfpci_pcm_4ch(struct snd_ymfpci * chip,int device)1207 int snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device)
1208 {
1209 struct snd_pcm *pcm;
1210 int err;
1211
1212 err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm);
1213 if (err < 0)
1214 return err;
1215 pcm->private_data = chip;
1216
1217 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1218
1219 /* global setup */
1220 pcm->info_flags = 0;
1221 strscpy(pcm->name, "YMFPCI - Rear PCM");
1222 chip->pcm_4ch = pcm;
1223
1224 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1225 &chip->pci->dev, 64*1024, 256*1024);
1226
1227 return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1228 surround_map, 2, 0, NULL);
1229 }
1230
snd_ymfpci_spdif_default_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1231 static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1232 {
1233 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1234 uinfo->count = 1;
1235 return 0;
1236 }
1237
snd_ymfpci_spdif_default_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1238 static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1239 struct snd_ctl_elem_value *ucontrol)
1240 {
1241 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1242
1243 guard(spinlock_irq)(&chip->reg_lock);
1244 ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1245 ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1246 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1247 return 0;
1248 }
1249
snd_ymfpci_spdif_default_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1250 static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1251 struct snd_ctl_elem_value *ucontrol)
1252 {
1253 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1254 unsigned int val;
1255 int change;
1256
1257 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1258 (ucontrol->value.iec958.status[1] << 8);
1259 guard(spinlock_irq)(&chip->reg_lock);
1260 change = chip->spdif_bits != val;
1261 chip->spdif_bits = val;
1262 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1263 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1264 return change;
1265 }
1266
1267 static const struct snd_kcontrol_new snd_ymfpci_spdif_default =
1268 {
1269 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1270 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1271 .info = snd_ymfpci_spdif_default_info,
1272 .get = snd_ymfpci_spdif_default_get,
1273 .put = snd_ymfpci_spdif_default_put
1274 };
1275
snd_ymfpci_spdif_mask_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1276 static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1277 {
1278 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1279 uinfo->count = 1;
1280 return 0;
1281 }
1282
snd_ymfpci_spdif_mask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1283 static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1284 struct snd_ctl_elem_value *ucontrol)
1285 {
1286 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1287
1288 guard(spinlock_irq)(&chip->reg_lock);
1289 ucontrol->value.iec958.status[0] = 0x3e;
1290 ucontrol->value.iec958.status[1] = 0xff;
1291 return 0;
1292 }
1293
1294 static const struct snd_kcontrol_new snd_ymfpci_spdif_mask =
1295 {
1296 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1297 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1298 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1299 .info = snd_ymfpci_spdif_mask_info,
1300 .get = snd_ymfpci_spdif_mask_get,
1301 };
1302
snd_ymfpci_spdif_stream_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1303 static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1304 {
1305 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1306 uinfo->count = 1;
1307 return 0;
1308 }
1309
snd_ymfpci_spdif_stream_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1310 static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1311 struct snd_ctl_elem_value *ucontrol)
1312 {
1313 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1314
1315 guard(spinlock_irq)(&chip->reg_lock);
1316 ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1317 ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1318 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1319 return 0;
1320 }
1321
snd_ymfpci_spdif_stream_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1322 static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1323 struct snd_ctl_elem_value *ucontrol)
1324 {
1325 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1326 unsigned int val;
1327 int change;
1328
1329 val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1330 (ucontrol->value.iec958.status[1] << 8);
1331 guard(spinlock_irq)(&chip->reg_lock);
1332 change = chip->spdif_pcm_bits != val;
1333 chip->spdif_pcm_bits = val;
1334 if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1335 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1336 return change;
1337 }
1338
1339 static const struct snd_kcontrol_new snd_ymfpci_spdif_stream =
1340 {
1341 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1342 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1343 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1344 .info = snd_ymfpci_spdif_stream_info,
1345 .get = snd_ymfpci_spdif_stream_get,
1346 .put = snd_ymfpci_spdif_stream_put
1347 };
1348
snd_ymfpci_drec_source_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * info)1349 static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
1350 {
1351 static const char *const texts[3] = {"AC'97", "IEC958", "ZV Port"};
1352
1353 return snd_ctl_enum_info(info, 1, 3, texts);
1354 }
1355
snd_ymfpci_drec_source_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * value)1356 static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1357 {
1358 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1359 u16 reg;
1360
1361 guard(spinlock_irq)(&chip->reg_lock);
1362 reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1363 if (!(reg & 0x100))
1364 value->value.enumerated.item[0] = 0;
1365 else
1366 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1367 return 0;
1368 }
1369
snd_ymfpci_drec_source_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * value)1370 static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1371 {
1372 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1373 u16 reg, old_reg;
1374
1375 guard(spinlock_irq)(&chip->reg_lock);
1376 old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1377 if (value->value.enumerated.item[0] == 0)
1378 reg = old_reg & ~0x100;
1379 else
1380 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1381 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1382 return reg != old_reg;
1383 }
1384
1385 static const struct snd_kcontrol_new snd_ymfpci_drec_source = {
1386 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1387 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1388 .name = "Direct Recording Source",
1389 .info = snd_ymfpci_drec_source_info,
1390 .get = snd_ymfpci_drec_source_get,
1391 .put = snd_ymfpci_drec_source_put
1392 };
1393
1394 /*
1395 * Mixer controls
1396 */
1397
1398 #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
1399 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1400 .info = snd_ymfpci_info_single, \
1401 .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1402 .private_value = ((reg) | ((shift) << 16)) }
1403
1404 #define snd_ymfpci_info_single snd_ctl_boolean_mono_info
1405
snd_ymfpci_get_single(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1406 static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1407 struct snd_ctl_elem_value *ucontrol)
1408 {
1409 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1410 int reg = kcontrol->private_value & 0xffff;
1411 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1412 unsigned int mask = 1;
1413
1414 switch (reg) {
1415 case YDSXGR_SPDIFOUTCTRL: break;
1416 case YDSXGR_SPDIFINCTRL: break;
1417 default: return -EINVAL;
1418 }
1419 ucontrol->value.integer.value[0] =
1420 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
1421 return 0;
1422 }
1423
snd_ymfpci_put_single(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1424 static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1425 struct snd_ctl_elem_value *ucontrol)
1426 {
1427 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1428 int reg = kcontrol->private_value & 0xffff;
1429 unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1430 unsigned int mask = 1;
1431 int change;
1432 unsigned int val, oval;
1433
1434 switch (reg) {
1435 case YDSXGR_SPDIFOUTCTRL: break;
1436 case YDSXGR_SPDIFINCTRL: break;
1437 default: return -EINVAL;
1438 }
1439 val = (ucontrol->value.integer.value[0] & mask);
1440 val <<= shift;
1441 guard(spinlock_irq)(&chip->reg_lock);
1442 oval = snd_ymfpci_readl(chip, reg);
1443 val = (oval & ~(mask << shift)) | val;
1444 change = val != oval;
1445 snd_ymfpci_writel(chip, reg, val);
1446 return change;
1447 }
1448
1449 static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
1450
1451 #define YMFPCI_DOUBLE(xname, xindex, reg) \
1452 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1453 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
1454 .info = snd_ymfpci_info_double, \
1455 .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
1456 .private_value = reg, \
1457 .tlv = { .p = db_scale_native } }
1458
snd_ymfpci_info_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1459 static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1460 {
1461 unsigned int reg = kcontrol->private_value;
1462
1463 if (reg < 0x80 || reg >= 0xc0)
1464 return -EINVAL;
1465 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1466 uinfo->count = 2;
1467 uinfo->value.integer.min = 0;
1468 uinfo->value.integer.max = 16383;
1469 return 0;
1470 }
1471
snd_ymfpci_get_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1472 static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1473 {
1474 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1475 unsigned int reg = kcontrol->private_value;
1476 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1477 unsigned int val;
1478
1479 if (reg < 0x80 || reg >= 0xc0)
1480 return -EINVAL;
1481 guard(spinlock_irq)(&chip->reg_lock);
1482 val = snd_ymfpci_readl(chip, reg);
1483 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1484 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
1485 return 0;
1486 }
1487
snd_ymfpci_put_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1488 static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1489 {
1490 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1491 unsigned int reg = kcontrol->private_value;
1492 unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1493 int change;
1494 unsigned int val1, val2, oval;
1495
1496 if (reg < 0x80 || reg >= 0xc0)
1497 return -EINVAL;
1498 val1 = ucontrol->value.integer.value[0] & mask;
1499 val2 = ucontrol->value.integer.value[1] & mask;
1500 val1 <<= shift_left;
1501 val2 <<= shift_right;
1502 guard(spinlock_irq)(&chip->reg_lock);
1503 oval = snd_ymfpci_readl(chip, reg);
1504 val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1505 change = val1 != oval;
1506 snd_ymfpci_writel(chip, reg, val1);
1507 return change;
1508 }
1509
snd_ymfpci_put_nativedacvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1510 static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1511 struct snd_ctl_elem_value *ucontrol)
1512 {
1513 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1514 unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1515 unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1516 int change;
1517 unsigned int value, oval;
1518
1519 value = ucontrol->value.integer.value[0] & 0x3fff;
1520 value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1521 guard(spinlock_irq)(&chip->reg_lock);
1522 oval = snd_ymfpci_readl(chip, reg);
1523 change = value != oval;
1524 snd_ymfpci_writel(chip, reg, value);
1525 snd_ymfpci_writel(chip, reg2, value);
1526 return change;
1527 }
1528
1529 /*
1530 * 4ch duplication
1531 */
1532 #define snd_ymfpci_info_dup4ch snd_ctl_boolean_mono_info
1533
snd_ymfpci_get_dup4ch(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1534 static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1535 {
1536 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1537 ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1538 return 0;
1539 }
1540
snd_ymfpci_put_dup4ch(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1541 static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1542 {
1543 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1544 int change;
1545 change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1546 if (change)
1547 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1548 return change;
1549 }
1550
1551 static const struct snd_kcontrol_new snd_ymfpci_dup4ch = {
1552 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1553 .name = "4ch Duplication",
1554 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1555 .info = snd_ymfpci_info_dup4ch,
1556 .get = snd_ymfpci_get_dup4ch,
1557 .put = snd_ymfpci_put_dup4ch,
1558 };
1559
1560 static const struct snd_kcontrol_new snd_ymfpci_controls[] = {
1561 {
1562 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1563 .name = "Wave Playback Volume",
1564 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1565 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1566 .info = snd_ymfpci_info_double,
1567 .get = snd_ymfpci_get_double,
1568 .put = snd_ymfpci_put_nativedacvol,
1569 .private_value = YDSXGR_NATIVEDACOUTVOL,
1570 .tlv = { .p = db_scale_native },
1571 },
1572 YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1573 YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1574 YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1575 YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1576 YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1577 YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1578 YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1579 YMFPCI_DOUBLE("FM Legacy Playback Volume", 0, YDSXGR_LEGACYOUTVOL),
1580 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1581 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1582 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1583 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1584 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1585 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1586 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
1587 };
1588
1589
1590 /*
1591 * GPIO
1592 */
1593
snd_ymfpci_get_gpio_out(struct snd_ymfpci * chip,int pin)1594 static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
1595 {
1596 u16 reg, mode;
1597
1598 guard(spinlock_irqsave)(&chip->reg_lock);
1599 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1600 reg &= ~(1 << (pin + 8));
1601 reg |= (1 << pin);
1602 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1603 /* set the level mode for input line */
1604 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1605 mode &= ~(3 << (pin * 2));
1606 snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1607 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1608 mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1609 return (mode >> pin) & 1;
1610 }
1611
snd_ymfpci_set_gpio_out(struct snd_ymfpci * chip,int pin,int enable)1612 static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
1613 {
1614 u16 reg;
1615
1616 guard(spinlock_irqsave)(&chip->reg_lock);
1617 reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1618 reg &= ~(1 << pin);
1619 reg &= ~(1 << (pin + 8));
1620 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1621 snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1622 snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1623
1624 return 0;
1625 }
1626
1627 #define snd_ymfpci_gpio_sw_info snd_ctl_boolean_mono_info
1628
snd_ymfpci_gpio_sw_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1629 static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1630 {
1631 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1632 int pin = (int)kcontrol->private_value;
1633 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1634 return 0;
1635 }
1636
snd_ymfpci_gpio_sw_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1637 static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1638 {
1639 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1640 int pin = (int)kcontrol->private_value;
1641
1642 if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1643 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1644 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1645 return 1;
1646 }
1647 return 0;
1648 }
1649
1650 static const struct snd_kcontrol_new snd_ymfpci_rear_shared = {
1651 .name = "Shared Rear/Line-In Switch",
1652 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1653 .info = snd_ymfpci_gpio_sw_info,
1654 .get = snd_ymfpci_gpio_sw_get,
1655 .put = snd_ymfpci_gpio_sw_put,
1656 .private_value = 2,
1657 };
1658
1659 /*
1660 * PCM voice volume
1661 */
1662
snd_ymfpci_pcm_vol_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1663 static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1664 struct snd_ctl_elem_info *uinfo)
1665 {
1666 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1667 uinfo->count = 2;
1668 uinfo->value.integer.min = 0;
1669 uinfo->value.integer.max = 0x8000;
1670 return 0;
1671 }
1672
snd_ymfpci_pcm_vol_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1673 static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1674 struct snd_ctl_elem_value *ucontrol)
1675 {
1676 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1677 unsigned int subs = kcontrol->id.subdevice;
1678
1679 ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1680 ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1681 return 0;
1682 }
1683
snd_ymfpci_pcm_vol_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1684 static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1685 struct snd_ctl_elem_value *ucontrol)
1686 {
1687 struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1688 unsigned int subs = kcontrol->id.subdevice;
1689 struct snd_pcm_substream *substream;
1690
1691 if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1692 ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1693 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1694 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1695 if (chip->pcm_mixer[subs].left > 0x8000)
1696 chip->pcm_mixer[subs].left = 0x8000;
1697 if (chip->pcm_mixer[subs].right > 0x8000)
1698 chip->pcm_mixer[subs].right = 0x8000;
1699
1700 substream = (struct snd_pcm_substream *)kcontrol->private_value;
1701 guard(spinlock_irqsave)(&chip->voice_lock);
1702 if (substream->runtime && substream->runtime->private_data) {
1703 struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1704 if (!ypcm->use_441_slot)
1705 ypcm->update_pcm_vol = 2;
1706 }
1707 return 1;
1708 }
1709 return 0;
1710 }
1711
1712 static const struct snd_kcontrol_new snd_ymfpci_pcm_volume = {
1713 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1714 .name = "PCM Playback Volume",
1715 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1716 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1717 .info = snd_ymfpci_pcm_vol_info,
1718 .get = snd_ymfpci_pcm_vol_get,
1719 .put = snd_ymfpci_pcm_vol_put,
1720 };
1721
1722
1723 /*
1724 * Mixer routines
1725 */
1726
snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus * bus)1727 static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1728 {
1729 struct snd_ymfpci *chip = bus->private_data;
1730 chip->ac97_bus = NULL;
1731 }
1732
snd_ymfpci_mixer_free_ac97(struct snd_ac97 * ac97)1733 static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
1734 {
1735 struct snd_ymfpci *chip = ac97->private_data;
1736 chip->ac97 = NULL;
1737 }
1738
snd_ymfpci_mixer(struct snd_ymfpci * chip,int rear_switch)1739 int snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
1740 {
1741 struct snd_ac97_template ac97;
1742 struct snd_kcontrol *kctl;
1743 struct snd_pcm_substream *substream;
1744 unsigned int idx;
1745 int err;
1746 static const struct snd_ac97_bus_ops ops = {
1747 .write = snd_ymfpci_codec_write,
1748 .read = snd_ymfpci_codec_read,
1749 };
1750
1751 err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
1752 if (err < 0)
1753 return err;
1754 chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1755 chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1756
1757 memset(&ac97, 0, sizeof(ac97));
1758 ac97.private_data = chip;
1759 ac97.private_free = snd_ymfpci_mixer_free_ac97;
1760 err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
1761 if (err < 0)
1762 return err;
1763
1764 /* to be sure */
1765 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1766 AC97_EA_VRA|AC97_EA_VRM, 0);
1767
1768 for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1769 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip));
1770 if (err < 0)
1771 return err;
1772 }
1773 if (chip->ac97->ext_id & AC97_EI_SDAC) {
1774 kctl = snd_ctl_new1(&snd_ymfpci_dup4ch, chip);
1775 err = snd_ctl_add(chip->card, kctl);
1776 if (err < 0)
1777 return err;
1778 }
1779
1780 /* add S/PDIF control */
1781 if (snd_BUG_ON(!chip->pcm_spdif))
1782 return -ENXIO;
1783 kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip);
1784 kctl->id.device = chip->pcm_spdif->device;
1785 err = snd_ctl_add(chip->card, kctl);
1786 if (err < 0)
1787 return err;
1788 kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip);
1789 kctl->id.device = chip->pcm_spdif->device;
1790 err = snd_ctl_add(chip->card, kctl);
1791 if (err < 0)
1792 return err;
1793 kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip);
1794 kctl->id.device = chip->pcm_spdif->device;
1795 err = snd_ctl_add(chip->card, kctl);
1796 if (err < 0)
1797 return err;
1798 chip->spdif_pcm_ctl = kctl;
1799
1800 /* direct recording source */
1801 if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754) {
1802 kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip);
1803 err = snd_ctl_add(chip->card, kctl);
1804 if (err < 0)
1805 return err;
1806 }
1807
1808 /*
1809 * shared rear/line-in
1810 */
1811 if (rear_switch) {
1812 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip));
1813 if (err < 0)
1814 return err;
1815 }
1816
1817 /* per-voice volume */
1818 substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1819 for (idx = 0; idx < 32; ++idx) {
1820 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1821 if (!kctl)
1822 return -ENOMEM;
1823 kctl->id.device = chip->pcm->device;
1824 kctl->id.subdevice = idx;
1825 kctl->private_value = (unsigned long)substream;
1826 err = snd_ctl_add(chip->card, kctl);
1827 if (err < 0)
1828 return err;
1829 chip->pcm_mixer[idx].left = 0x8000;
1830 chip->pcm_mixer[idx].right = 0x8000;
1831 chip->pcm_mixer[idx].ctl = kctl;
1832 substream = substream->next;
1833 }
1834
1835 return 0;
1836 }
1837
1838
1839 /*
1840 * timer
1841 */
1842
snd_ymfpci_timer_start(struct snd_timer * timer)1843 static int snd_ymfpci_timer_start(struct snd_timer *timer)
1844 {
1845 struct snd_ymfpci *chip;
1846 unsigned int count;
1847
1848 chip = snd_timer_chip(timer);
1849 guard(spinlock_irqsave)(&chip->reg_lock);
1850 if (timer->sticks > 1) {
1851 chip->timer_ticks = timer->sticks;
1852 count = timer->sticks - 1;
1853 } else {
1854 /*
1855 * Divisor 1 is not allowed; fake it by using divisor 2 and
1856 * counting two ticks for each interrupt.
1857 */
1858 chip->timer_ticks = 2;
1859 count = 2 - 1;
1860 }
1861 snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1862 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1863 return 0;
1864 }
1865
snd_ymfpci_timer_stop(struct snd_timer * timer)1866 static int snd_ymfpci_timer_stop(struct snd_timer *timer)
1867 {
1868 struct snd_ymfpci *chip;
1869
1870 chip = snd_timer_chip(timer);
1871 guard(spinlock_irqsave)(&chip->reg_lock);
1872 snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1873 return 0;
1874 }
1875
snd_ymfpci_timer_precise_resolution(struct snd_timer * timer,unsigned long * num,unsigned long * den)1876 static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
1877 unsigned long *num, unsigned long *den)
1878 {
1879 *num = 1;
1880 *den = 96000;
1881 return 0;
1882 }
1883
1884 static const struct snd_timer_hardware snd_ymfpci_timer_hw = {
1885 .flags = SNDRV_TIMER_HW_AUTO,
1886 .resolution = 10417, /* 1 / 96 kHz = 10.41666...us */
1887 .ticks = 0x10000,
1888 .start = snd_ymfpci_timer_start,
1889 .stop = snd_ymfpci_timer_stop,
1890 .precise_resolution = snd_ymfpci_timer_precise_resolution,
1891 };
1892
snd_ymfpci_timer(struct snd_ymfpci * chip,int device)1893 int snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
1894 {
1895 struct snd_timer *timer = NULL;
1896 struct snd_timer_id tid;
1897 int err;
1898
1899 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1900 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1901 tid.card = chip->card->number;
1902 tid.device = device;
1903 tid.subdevice = 0;
1904 err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer);
1905 if (err >= 0) {
1906 strscpy(timer->name, "YMFPCI timer");
1907 timer->private_data = chip;
1908 timer->hw = snd_ymfpci_timer_hw;
1909 }
1910 chip->timer = timer;
1911 return err;
1912 }
1913
1914
1915 /*
1916 * proc interface
1917 */
1918
snd_ymfpci_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)1919 static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1920 struct snd_info_buffer *buffer)
1921 {
1922 struct snd_ymfpci *chip = entry->private_data;
1923 int i;
1924
1925 snd_iprintf(buffer, "YMFPCI\n\n");
1926 for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1927 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1928 }
1929
snd_ymfpci_proc_init(struct snd_card * card,struct snd_ymfpci * chip)1930 static int snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
1931 {
1932 return snd_card_ro_proc_new(card, "ymfpci", chip, snd_ymfpci_proc_read);
1933 }
1934
1935 /*
1936 * initialization routines
1937 */
1938
snd_ymfpci_aclink_reset(struct pci_dev * pci)1939 static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1940 {
1941 u8 cmd;
1942
1943 pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1944 #if 0 // force to reset
1945 if (cmd & 0x03) {
1946 #endif
1947 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1948 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1949 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1950 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1951 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1952 #if 0
1953 }
1954 #endif
1955 }
1956
snd_ymfpci_enable_dsp(struct snd_ymfpci * chip)1957 static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
1958 {
1959 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1960 }
1961
snd_ymfpci_disable_dsp(struct snd_ymfpci * chip)1962 static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
1963 {
1964 u32 val;
1965 int timeout = 1000;
1966
1967 val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1968 if (val)
1969 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
1970 while (timeout-- > 0) {
1971 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
1972 if ((val & 0x00000002) == 0)
1973 break;
1974 }
1975 }
1976
snd_ymfpci_request_firmware(struct snd_ymfpci * chip)1977 static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
1978 {
1979 int err, is_1e;
1980 const char *name;
1981
1982 err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
1983 &chip->pci->dev);
1984 if (err >= 0) {
1985 if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
1986 dev_err(chip->card->dev,
1987 "DSP microcode has wrong size\n");
1988 err = -EINVAL;
1989 }
1990 }
1991 if (err < 0)
1992 return err;
1993 is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
1994 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
1995 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
1996 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
1997 name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
1998 err = request_firmware(&chip->controller_microcode, name,
1999 &chip->pci->dev);
2000 if (err >= 0) {
2001 if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
2002 dev_err(chip->card->dev,
2003 "controller microcode has wrong size\n");
2004 err = -EINVAL;
2005 }
2006 }
2007 if (err < 0)
2008 return err;
2009 return 0;
2010 }
2011
2012 MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
2013 MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
2014 MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
2015
snd_ymfpci_download_image(struct snd_ymfpci * chip)2016 static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
2017 {
2018 int i;
2019 u16 ctrl;
2020 const __le32 *inst;
2021
2022 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2023 snd_ymfpci_disable_dsp(chip);
2024 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2025 snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2026 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2027 snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2028 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2029 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2030 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2031 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2032 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2033
2034 /* setup DSP instruction code */
2035 inst = (const __le32 *)chip->dsp_microcode->data;
2036 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2037 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2038 le32_to_cpu(inst[i]));
2039
2040 /* setup control instruction code */
2041 inst = (const __le32 *)chip->controller_microcode->data;
2042 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2043 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2044 le32_to_cpu(inst[i]));
2045
2046 snd_ymfpci_enable_dsp(chip);
2047 }
2048
snd_ymfpci_memalloc(struct snd_ymfpci * chip)2049 static int snd_ymfpci_memalloc(struct snd_ymfpci *chip)
2050 {
2051 long size, playback_ctrl_size;
2052 int voice, bank, reg;
2053 u8 *ptr;
2054 dma_addr_t ptr_addr;
2055
2056 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2057 chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2058 chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2059 chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2060 chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2061
2062 size = ALIGN(playback_ctrl_size, 0x100) +
2063 ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2064 ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2065 ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
2066 chip->work_size;
2067 /* work_ptr must be aligned to 256 bytes, but it's already
2068 covered with the kernel page allocation mechanism */
2069 chip->work_ptr = snd_devm_alloc_pages(&chip->pci->dev,
2070 SNDRV_DMA_TYPE_DEV, size);
2071 if (!chip->work_ptr)
2072 return -ENOMEM;
2073 ptr = chip->work_ptr->area;
2074 ptr_addr = chip->work_ptr->addr;
2075 memset(ptr, 0, size); /* for sure */
2076
2077 chip->bank_base_playback = ptr;
2078 chip->bank_base_playback_addr = ptr_addr;
2079 chip->ctrl_playback = (__le32 *)ptr;
2080 chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
2081 ptr += ALIGN(playback_ctrl_size, 0x100);
2082 ptr_addr += ALIGN(playback_ctrl_size, 0x100);
2083 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2084 chip->voices[voice].number = voice;
2085 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
2086 chip->voices[voice].bank_addr = ptr_addr;
2087 for (bank = 0; bank < 2; bank++) {
2088 chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
2089 ptr += chip->bank_size_playback;
2090 ptr_addr += chip->bank_size_playback;
2091 }
2092 }
2093 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2094 ptr_addr = ALIGN(ptr_addr, 0x100);
2095 chip->bank_base_capture = ptr;
2096 chip->bank_base_capture_addr = ptr_addr;
2097 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2098 for (bank = 0; bank < 2; bank++) {
2099 chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
2100 ptr += chip->bank_size_capture;
2101 ptr_addr += chip->bank_size_capture;
2102 }
2103 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2104 ptr_addr = ALIGN(ptr_addr, 0x100);
2105 chip->bank_base_effect = ptr;
2106 chip->bank_base_effect_addr = ptr_addr;
2107 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2108 for (bank = 0; bank < 2; bank++) {
2109 chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
2110 ptr += chip->bank_size_effect;
2111 ptr_addr += chip->bank_size_effect;
2112 }
2113 ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2114 ptr_addr = ALIGN(ptr_addr, 0x100);
2115 chip->work_base = ptr;
2116 chip->work_base_addr = ptr_addr;
2117
2118 snd_BUG_ON(ptr + PAGE_ALIGN(chip->work_size) !=
2119 chip->work_ptr->area + chip->work_ptr->bytes);
2120
2121 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2122 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2123 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2124 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2125 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2126
2127 /* S/PDIF output initialization */
2128 chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2129 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2130 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2131
2132 /* S/PDIF input initialization */
2133 snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2134
2135 /* digital mixer setup */
2136 for (reg = 0x80; reg < 0xc0; reg += 4)
2137 snd_ymfpci_writel(chip, reg, 0);
2138 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2139 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
2140 snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2141 snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2142 snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2143 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2144 snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2145 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2146
2147 return 0;
2148 }
2149
snd_ymfpci_free(struct snd_card * card)2150 static void snd_ymfpci_free(struct snd_card *card)
2151 {
2152 struct snd_ymfpci *chip = card->private_data;
2153 u16 ctrl;
2154
2155 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2156 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2157 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2158 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2159 snd_ymfpci_disable_dsp(chip);
2160 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2161 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2162 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2163 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2164 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2165 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2166 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2167
2168 snd_ymfpci_ac3_done(chip);
2169
2170 snd_ymfpci_free_gameport(chip);
2171
2172 pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, chip->old_legacy_ctrl);
2173
2174 release_firmware(chip->dsp_microcode);
2175 release_firmware(chip->controller_microcode);
2176 }
2177
snd_ymfpci_suspend(struct device * dev)2178 static int snd_ymfpci_suspend(struct device *dev)
2179 {
2180 struct snd_card *card = dev_get_drvdata(dev);
2181 struct snd_ymfpci *chip = card->private_data;
2182 unsigned int i, legacy_reg_count = DSXG_PCI_NUM_SAVED_LEGACY_REGS;
2183
2184 if (chip->pci->device >= 0x0010) /* YMF 744/754 */
2185 legacy_reg_count = DSXG_PCI_NUM_SAVED_REGS;
2186
2187 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2188 snd_ac97_suspend(chip->ac97);
2189
2190 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2191 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2192
2193 chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2194
2195 for (i = 0; i < legacy_reg_count; i++)
2196 pci_read_config_word(chip->pci, pci_saved_regs_index[i],
2197 chip->saved_dsxg_pci_regs + i);
2198
2199 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2200 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2201 snd_ymfpci_disable_dsp(chip);
2202 return 0;
2203 }
2204
snd_ymfpci_resume(struct device * dev)2205 static int snd_ymfpci_resume(struct device *dev)
2206 {
2207 struct pci_dev *pci = to_pci_dev(dev);
2208 struct snd_card *card = dev_get_drvdata(dev);
2209 struct snd_ymfpci *chip = card->private_data;
2210 unsigned int i, legacy_reg_count = DSXG_PCI_NUM_SAVED_LEGACY_REGS;
2211
2212 if (chip->pci->device >= 0x0010) /* YMF 744/754 */
2213 legacy_reg_count = DSXG_PCI_NUM_SAVED_REGS;
2214
2215 snd_ymfpci_aclink_reset(pci);
2216 snd_ymfpci_codec_ready(chip, 0);
2217 snd_ymfpci_download_image(chip);
2218 udelay(100);
2219
2220 for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2221 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2222
2223 snd_ac97_resume(chip->ac97);
2224
2225 for (i = 0; i < legacy_reg_count; i++)
2226 pci_write_config_word(chip->pci, pci_saved_regs_index[i],
2227 chip->saved_dsxg_pci_regs[i]);
2228
2229 /* start hw again */
2230 if (chip->start_count > 0) {
2231 guard(spinlock_irq)(&chip->reg_lock);
2232 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2233 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2234 }
2235 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2236 return 0;
2237 }
2238
2239 DEFINE_SIMPLE_DEV_PM_OPS(snd_ymfpci_pm, snd_ymfpci_suspend, snd_ymfpci_resume);
2240
snd_ymfpci_create(struct snd_card * card,struct pci_dev * pci,u16 old_legacy_ctrl)2241 int snd_ymfpci_create(struct snd_card *card,
2242 struct pci_dev *pci,
2243 u16 old_legacy_ctrl)
2244 {
2245 struct snd_ymfpci *chip = card->private_data;
2246 int err;
2247
2248 /* enable PCI device */
2249 err = pcim_enable_device(pci);
2250 if (err < 0)
2251 return err;
2252
2253 chip->old_legacy_ctrl = old_legacy_ctrl;
2254 spin_lock_init(&chip->reg_lock);
2255 spin_lock_init(&chip->voice_lock);
2256 init_waitqueue_head(&chip->interrupt_sleep);
2257 atomic_set(&chip->interrupt_sleep_count, 0);
2258 chip->card = card;
2259 chip->pci = pci;
2260 chip->irq = -1;
2261 chip->device_id = pci->device;
2262 chip->rev = pci->revision;
2263
2264 err = pcim_request_all_regions(pci, "YMFPCI");
2265 if (err < 0)
2266 return err;
2267
2268 chip->reg_area_phys = pci_resource_start(pci, 0);
2269 chip->reg_area_virt = devm_ioremap(&pci->dev, chip->reg_area_phys, 0x8000);
2270 if (!chip->reg_area_virt) {
2271 dev_err(chip->card->dev,
2272 "unable to grab memory region 0x%lx-0x%lx\n",
2273 chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
2274 return -EBUSY;
2275 }
2276 pci_set_master(pci);
2277 chip->src441_used = -1;
2278
2279 if (devm_request_irq(&pci->dev, pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2280 KBUILD_MODNAME, chip)) {
2281 dev_err(chip->card->dev, "unable to grab IRQ %d\n", pci->irq);
2282 return -EBUSY;
2283 }
2284 chip->irq = pci->irq;
2285 card->sync_irq = chip->irq;
2286 card->private_free = snd_ymfpci_free;
2287
2288 snd_ymfpci_aclink_reset(pci);
2289 if (snd_ymfpci_codec_ready(chip, 0) < 0)
2290 return -EIO;
2291
2292 err = snd_ymfpci_request_firmware(chip);
2293 if (err < 0) {
2294 dev_err(chip->card->dev, "firmware request failed: %d\n", err);
2295 return err;
2296 }
2297 snd_ymfpci_download_image(chip);
2298
2299 udelay(100); /* seems we need a delay after downloading image.. */
2300
2301 if (snd_ymfpci_memalloc(chip) < 0)
2302 return -EIO;
2303
2304 err = snd_ymfpci_ac3_init(chip);
2305 if (err < 0)
2306 return err;
2307
2308 snd_ymfpci_proc_init(card, chip);
2309
2310 return 0;
2311 }
2312