1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * synth callback routines for Emu10k1
5 * Copyright (C) 2000 Takashi Iwai <tiwai@suse.de>
8 #include <linux/export.h>
9 #include "emu10k1_synth_local.h"
10 #include <sound/asoundef.h>
14 V_FREE=0, V_OFF, V_RELEASED, V_PLAYING, V_END
17 /* Keeps track of what we are finding */
26 static void lookup_voices(struct snd_emux *emux, struct snd_emu10k1 *hw,
27 struct best_voice *best, int active_only);
28 static struct snd_emux_voice *get_voice(struct snd_emux *emux,
29 struct snd_emux_port *port);
30 static int start_voice(struct snd_emux_voice *vp);
31 static void trigger_voice(struct snd_emux_voice *vp);
32 static void release_voice(struct snd_emux_voice *vp);
33 static void update_voice(struct snd_emux_voice *vp, int update);
34 static void terminate_voice(struct snd_emux_voice *vp);
35 static void free_voice(struct snd_emux_voice *vp);
36 static u32 make_fmmod(struct snd_emux_voice *vp);
37 static u32 make_fm2frq2(struct snd_emux_voice *vp);
40 * Ensure a value is between two points
41 * macro evaluates its args more than once, so changed to upper-case.
43 #define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
44 #define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
50 static const struct snd_emux_operators emu10k1_ops = {
52 .get_voice = get_voice,
53 .prepare = start_voice,
54 .trigger = trigger_voice,
55 .release = release_voice,
56 .update = update_voice,
57 .terminate = terminate_voice,
58 .free_voice = free_voice,
59 .sample_new = snd_emu10k1_sample_new,
60 .sample_free = snd_emu10k1_sample_free,
64 snd_emu10k1_ops_setup(struct snd_emux *emux)
66 emux->ops = emu10k1_ops;
71 * get more voice for pcm
73 * terminate most inactive voice and give it as a pcm voice.
75 * voice_lock is already held.
78 snd_emu10k1_synth_get_voice(struct snd_emu10k1 *hw)
81 struct snd_emux_voice *vp;
82 struct best_voice best[V_END];
87 lookup_voices(emu, hw, best, 1); /* no OFF voices */
88 for (i = 0; i < V_END; i++) {
89 if (best[i].voice >= 0) {
91 vp = &emu->voices[best[i].voice];
95 dev_warn(emu->card->dev,
96 "synth_get_voice: ch < 0 (%d) ??", i);
100 vp->emu->num_voices--;
102 vp->state = SNDRV_EMUX_ST_OFF;
113 * turn off the voice (not terminated)
116 release_voice(struct snd_emux_voice *vp)
118 struct snd_emu10k1 *hw;
121 snd_emu10k1_ptr_write_multiple(hw, vp->ch,
122 DCYSUSM, (unsigned char)vp->reg.parm.modrelease | DCYSUSM_PHASE1_MASK,
123 DCYSUSV, (unsigned char)vp->reg.parm.volrelease | DCYSUSV_PHASE1_MASK | DCYSUSV_CHANNELENABLE_MASK,
129 * terminate the voice
132 terminate_voice(struct snd_emux_voice *vp)
134 struct snd_emu10k1 *hw;
139 snd_emu10k1_ptr_write_multiple(hw, vp->ch,
141 VTFT, VTFT_FILTERTARGET_MASK,
142 CVCF, CVCF_CURRENTFILTER_MASK,
147 struct snd_emu10k1_memblk *emem;
148 emem = (struct snd_emu10k1_memblk *)vp->block;
149 if (emem->map_locked > 0)
155 * release the voice to system
158 free_voice(struct snd_emux_voice *vp)
160 struct snd_emu10k1 *hw;
163 /* FIXME: emu10k1_synth is broken. */
164 /* This can get called with hw == 0 */
165 /* Problem apparent on plug, unplug then plug */
166 /* on the Audigy 2 ZS Notebook. */
167 if (hw && (vp->ch >= 0)) {
168 snd_emu10k1_voice_free(hw, &hw->voices[vp->ch]);
169 vp->emu->num_voices--;
179 update_voice(struct snd_emux_voice *vp, int update)
181 struct snd_emu10k1 *hw;
184 if (update & SNDRV_EMUX_UPDATE_VOLUME)
185 snd_emu10k1_ptr_write(hw, IFATN_ATTENUATION, vp->ch, vp->avol);
186 if (update & SNDRV_EMUX_UPDATE_PITCH)
187 snd_emu10k1_ptr_write(hw, IP, vp->ch, vp->apitch);
188 if (update & SNDRV_EMUX_UPDATE_PAN) {
189 snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_A, vp->ch, vp->apan);
190 snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_B, vp->ch, vp->aaux);
192 if (update & SNDRV_EMUX_UPDATE_FMMOD)
193 snd_emu10k1_ptr_write(hw, FMMOD, vp->ch, make_fmmod(vp));
194 if (update & SNDRV_EMUX_UPDATE_TREMFREQ)
195 snd_emu10k1_ptr_write(hw, TREMFRQ, vp->ch, vp->reg.parm.tremfrq);
196 if (update & SNDRV_EMUX_UPDATE_FM2FRQ2)
197 snd_emu10k1_ptr_write(hw, FM2FRQ2, vp->ch, make_fm2frq2(vp));
198 if (update & SNDRV_EMUX_UPDATE_Q)
199 snd_emu10k1_ptr_write(hw, CCCA_RESONANCE, vp->ch, vp->reg.parm.filterQ);
204 * look up voice table - get the best voice in order of preference
208 lookup_voices(struct snd_emux *emu, struct snd_emu10k1 *hw,
209 struct best_voice *best, int active_only)
211 struct snd_emux_voice *vp;
212 struct best_voice *bp;
215 for (i = 0; i < V_END; i++) {
216 best[i].time = (unsigned int)-1; /* XXX MAX_?INT really */
221 * Go through them all and get a best one to use.
222 * NOTE: could also look at volume and pick the quietest one.
224 for (i = 0; i < emu->max_voices; i++) {
227 vp = &emu->voices[i];
229 if (state == SNDRV_EMUX_ST_OFF) {
237 else if (state == SNDRV_EMUX_ST_RELEASED ||
238 state == SNDRV_EMUX_ST_PENDING) {
239 bp = best + V_RELEASED;
241 val = snd_emu10k1_ptr_read(hw, CVCF_CURRENTVOL, vp->ch);
246 else if (state == SNDRV_EMUX_ST_STANDBY)
248 else if (state & SNDRV_EMUX_ST_ON)
249 bp = best + V_PLAYING;
253 /* check if sample is finished playing (non-looping only) */
254 if (bp != best + V_OFF && bp != best + V_FREE &&
255 (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_SINGLESHOT)) {
256 val = snd_emu10k1_ptr_read(hw, CCCA_CURRADDR, vp->ch) - 64;
257 if (val >= vp->reg.loopstart)
261 if (vp->time < bp->time) {
271 * emu->voice_lock is already held.
273 static struct snd_emux_voice *
274 get_voice(struct snd_emux *emu, struct snd_emux_port *port)
276 struct snd_emu10k1 *hw;
277 struct snd_emux_voice *vp;
278 struct best_voice best[V_END];
283 lookup_voices(emu, hw, best, 0);
284 for (i = 0; i < V_END; i++) {
285 if (best[i].voice >= 0) {
286 vp = &emu->voices[best[i].voice];
288 /* allocate a voice */
289 struct snd_emu10k1_voice *hwvoice;
290 if (snd_emu10k1_voice_alloc(hw, EMU10K1_SYNTH, 1, 1, NULL, &hwvoice) < 0)
292 vp->ch = hwvoice->number;
304 * prepare envelopes and LFOs
307 start_voice(struct snd_emux_voice *vp)
311 u32 psst, dsl, map, ccca, vtarget;
312 unsigned int addr, mapped_offset;
313 struct snd_midi_channel *chan;
314 struct snd_emu10k1 *hw;
315 struct snd_emu10k1_memblk *emem;
319 if (snd_BUG_ON(ch < 0))
323 emem = (struct snd_emu10k1_memblk *)vp->block;
327 if (snd_emu10k1_memblk_map(hw, emem) < 0) {
328 /* dev_err(hw->card->devK, "emu: cannot map!\n"); */
331 mapped_offset = snd_emu10k1_memblk_offset(emem) >> 1;
332 vp->reg.start += mapped_offset;
333 vp->reg.end += mapped_offset;
334 vp->reg.loopstart += mapped_offset;
335 vp->reg.loopend += mapped_offset;
337 /* set channel routing */
338 /* A = left(0), B = right(1), C = reverb(c), D = chorus(d) */
340 temp = FXBUS_MIDI_LEFT | (FXBUS_MIDI_RIGHT << 8) |
341 (FXBUS_MIDI_REVERB << 16) | (FXBUS_MIDI_CHORUS << 24);
342 snd_emu10k1_ptr_write(hw, A_FXRT1, ch, temp);
344 temp = (FXBUS_MIDI_LEFT << 16) | (FXBUS_MIDI_RIGHT << 20) |
345 (FXBUS_MIDI_REVERB << 24) | (FXBUS_MIDI_CHORUS << 28);
346 snd_emu10k1_ptr_write(hw, FXRT, ch, temp);
349 temp = vp->reg.parm.reverb;
350 temp += (int)vp->chan->control[MIDI_CTL_E1_REVERB_DEPTH] * 9 / 10;
352 addr = vp->reg.loopstart;
353 psst = (temp << 24) | addr;
355 addr = vp->reg.loopend;
356 temp = vp->reg.parm.chorus;
357 temp += (int)chan->control[MIDI_CTL_E3_CHORUS_DEPTH] * 9 / 10;
359 dsl = (temp << 24) | addr;
361 map = (hw->silent_page.addr << hw->address_mode) | (hw->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
363 addr = vp->reg.start + 64;
364 temp = vp->reg.parm.filterQ;
365 ccca = (temp << 28) | addr;
366 if (vp->apitch < 0xe400)
367 ccca |= CCCA_INTERPROM_0;
369 unsigned int shift = (vp->apitch - 0xe000) >> 10;
372 if (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_8BITS)
373 ccca |= CCCA_8BITSELECT;
375 vtarget = (unsigned int)vp->vtarget << 16;
377 snd_emu10k1_ptr_write_multiple(hw, ch,
378 /* channel to be silent and idle */
380 VTFT, VTFT_FILTERTARGET_MASK,
381 CVCF, CVCF_CURRENTFILTER_MASK,
385 /* set pitch offset */
388 /* set envelope parameters */
389 ENVVAL, vp->reg.parm.moddelay,
390 ATKHLDM, vp->reg.parm.modatkhld,
391 DCYSUSM, vp->reg.parm.moddcysus,
392 ENVVOL, vp->reg.parm.voldelay,
393 ATKHLDV, vp->reg.parm.volatkhld,
394 /* decay/sustain parameter for volume envelope is used
395 for triggerg the voice */
397 /* cutoff and volume */
398 IFATN, (unsigned int)vp->acutoff << 8 | (unsigned char)vp->avol,
400 /* modulation envelope heights */
401 PEFE, vp->reg.parm.pefe,
404 LFOVAL1, vp->reg.parm.lfo1delay,
405 LFOVAL2, vp->reg.parm.lfo2delay,
407 /* lfo1 pitch & cutoff shift */
408 FMMOD, make_fmmod(vp),
409 /* lfo1 volume & freq */
410 TREMFRQ, vp->reg.parm.tremfrq,
411 /* lfo2 pitch & freq */
412 FM2FRQ2, make_fm2frq2(vp),
414 /* reverb and loop start (reverb 8bit, MSB) */
417 /* chorus & loop end (chorus 8bit, MSB) */
420 /* clear filter delay memory */
424 /* invalidate maps */
428 /* Q & current address (Q 4bit value, MSB) */
432 CCR, REG_VAL_PUT(CCR_CACHEINVALIDSIZE, 64),
435 VTFT, vtarget | vp->ftarget,
436 CVCF, vtarget | CVCF_CURRENTFILTER_MASK,
440 hw->voices[ch].dirty = 1;
448 trigger_voice(struct snd_emux_voice *vp)
450 unsigned int ptarget;
451 struct snd_emu10k1 *hw;
452 struct snd_emu10k1_memblk *emem;
456 emem = (struct snd_emu10k1_memblk *)vp->block;
457 if (! emem || emem->mapped_page < 0)
458 return; /* not mapped */
461 ptarget = (unsigned int)vp->ptarget << 16;
463 ptarget = IP_TO_CP(vp->apitch);
465 snd_emu10k1_ptr_write_multiple(hw, vp->ch,
466 /* set pitch target and pan (volume) */
467 PTRX, ptarget | (vp->apan << 8) | vp->aaux,
469 /* current pitch and fractional address */
472 /* enable envelope engine */
473 DCYSUSV, vp->reg.parm.voldcysus | DCYSUSV_CHANNELENABLE_MASK,
480 /* calculate lfo1 modulation height and cutoff register */
482 make_fmmod(struct snd_emux_voice *vp)
485 unsigned char cutoff;
488 pitch = (char)(vp->reg.parm.fmmod>>8);
489 cutoff = (vp->reg.parm.fmmod & 0xff);
490 modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
491 pitch += (MOD_SENSE * modulation) / 1200;
492 LIMITVALUE(pitch, -128, 127);
493 return ((unsigned char)pitch << 8) | cutoff;
496 /* calculate set lfo2 pitch & frequency register */
498 make_fm2frq2(struct snd_emux_voice *vp)
504 pitch = (char)(vp->reg.parm.fm2frq2>>8);
505 freq = vp->reg.parm.fm2frq2 & 0xff;
506 modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
507 pitch += (MOD_SENSE * modulation) / 1200;
508 LIMITVALUE(pitch, -128, 127);
509 return ((unsigned char)pitch << 8) | freq;