xref: /openbmc/linux/sound/pci/emu10k1/emu10k1_callback.c (revision 5c2664cc09f94ba11c61908d5c7dac1c35d6dee8)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  synth callback routines for Emu10k1
4  *
5  *  Copyright (C) 2000 Takashi Iwai <tiwai@suse.de>
6  */
7 
8 #include <linux/export.h>
9 #include "emu10k1_synth_local.h"
10 #include <sound/asoundef.h>
11 
12 /* voice status */
13 enum {
14 	V_FREE=0, V_OFF, V_RELEASED, V_PLAYING, V_END
15 };
16 
17 /* Keeps track of what we are finding */
18 struct best_voice {
19 	unsigned int time;
20 	int voice;
21 };
22 
23 /*
24  * prototypes
25  */
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);
38 
39 /*
40  * Ensure a value is between two points
41  * macro evaluates its args more than once, so changed to upper-case.
42  */
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)
45 
46 
47 /*
48  * set up operators
49  */
50 static const struct snd_emux_operators emu10k1_ops = {
51 	.owner =	THIS_MODULE,
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,
61 };
62 
63 void
64 snd_emu10k1_ops_setup(struct snd_emux *emux)
65 {
66 	emux->ops = emu10k1_ops;
67 }
68 
69 
70 /*
71  * get more voice for pcm
72  *
73  * terminate most inactive voice and give it as a pcm voice.
74  *
75  * voice_lock is already held.
76  */
77 int
78 snd_emu10k1_synth_get_voice(struct snd_emu10k1 *hw)
79 {
80 	struct snd_emux *emu;
81 	struct snd_emux_voice *vp;
82 	struct best_voice best[V_END];
83 	int i;
84 
85 	emu = hw->synth;
86 
87 	lookup_voices(emu, hw, best, 1); /* no OFF voices */
88 	for (i = 0; i < V_END; i++) {
89 		if (best[i].voice >= 0) {
90 			int ch;
91 			vp = &emu->voices[best[i].voice];
92 			ch = vp->ch;
93 			if (ch < 0) {
94 				/*
95 				dev_warn(emu->card->dev,
96 				       "synth_get_voice: ch < 0 (%d) ??", i);
97 				*/
98 				continue;
99 			}
100 			vp->emu->num_voices--;
101 			vp->ch = -1;
102 			vp->state = SNDRV_EMUX_ST_OFF;
103 			return ch;
104 		}
105 	}
106 
107 	/* not found */
108 	return -ENOMEM;
109 }
110 
111 
112 /*
113  * turn off the voice (not terminated)
114  */
115 static void
116 release_voice(struct snd_emux_voice *vp)
117 {
118 	struct snd_emu10k1 *hw;
119 
120 	hw = vp->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,
124 		REGLIST_END);
125 }
126 
127 
128 /*
129  * terminate the voice
130  */
131 static void
132 terminate_voice(struct snd_emux_voice *vp)
133 {
134 	struct snd_emu10k1 *hw;
135 
136 	if (snd_BUG_ON(!vp))
137 		return;
138 	hw = vp->hw;
139 	snd_emu10k1_ptr_write_multiple(hw, vp->ch,
140 		DCYSUSV, 0,
141 		VTFT, VTFT_FILTERTARGET_MASK,
142 		CVCF, CVCF_CURRENTFILTER_MASK,
143 		PTRX, 0,
144 		CPF, 0,
145 		REGLIST_END);
146 	if (vp->block) {
147 		struct snd_emu10k1_memblk *emem;
148 		emem = (struct snd_emu10k1_memblk *)vp->block;
149 		if (emem->map_locked > 0)
150 			emem->map_locked--;
151 	}
152 }
153 
154 /*
155  * release the voice to system
156  */
157 static void
158 free_voice(struct snd_emux_voice *vp)
159 {
160 	struct snd_emu10k1 *hw;
161 
162 	hw = vp->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_ptr_write(hw, IFATN, vp->ch, 0xff00);
169 		snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0x807f | DCYSUSV_CHANNELENABLE_MASK);
170 		// snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0);
171 		snd_emu10k1_ptr_write(hw, VTFT, vp->ch, 0xffff);
172 		snd_emu10k1_ptr_write(hw, CVCF, vp->ch, 0xffff);
173 		snd_emu10k1_voice_free(hw, &hw->voices[vp->ch]);
174 		vp->emu->num_voices--;
175 		vp->ch = -1;
176 	}
177 }
178 
179 
180 /*
181  * update registers
182  */
183 static void
184 update_voice(struct snd_emux_voice *vp, int update)
185 {
186 	struct snd_emu10k1 *hw;
187 
188 	hw = vp->hw;
189 	if (update & SNDRV_EMUX_UPDATE_VOLUME)
190 		snd_emu10k1_ptr_write(hw, IFATN_ATTENUATION, vp->ch, vp->avol);
191 	if (update & SNDRV_EMUX_UPDATE_PITCH)
192 		snd_emu10k1_ptr_write(hw, IP, vp->ch, vp->apitch);
193 	if (update & SNDRV_EMUX_UPDATE_PAN) {
194 		snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_A, vp->ch, vp->apan);
195 		snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_B, vp->ch, vp->aaux);
196 	}
197 	if (update & SNDRV_EMUX_UPDATE_FMMOD)
198 		snd_emu10k1_ptr_write(hw, FMMOD, vp->ch, make_fmmod(vp));
199 	if (update & SNDRV_EMUX_UPDATE_TREMFREQ)
200 		snd_emu10k1_ptr_write(hw, TREMFRQ, vp->ch, vp->reg.parm.tremfrq);
201 	if (update & SNDRV_EMUX_UPDATE_FM2FRQ2)
202 		snd_emu10k1_ptr_write(hw, FM2FRQ2, vp->ch, make_fm2frq2(vp));
203 	if (update & SNDRV_EMUX_UPDATE_Q)
204 		snd_emu10k1_ptr_write(hw, CCCA_RESONANCE, vp->ch, vp->reg.parm.filterQ);
205 }
206 
207 
208 /*
209  * look up voice table - get the best voice in order of preference
210  */
211 /* spinlock held! */
212 static void
213 lookup_voices(struct snd_emux *emu, struct snd_emu10k1 *hw,
214 	      struct best_voice *best, int active_only)
215 {
216 	struct snd_emux_voice *vp;
217 	struct best_voice *bp;
218 	int  i;
219 
220 	for (i = 0; i < V_END; i++) {
221 		best[i].time = (unsigned int)-1; /* XXX MAX_?INT really */
222 		best[i].voice = -1;
223 	}
224 
225 	/*
226 	 * Go through them all and get a best one to use.
227 	 * NOTE: could also look at volume and pick the quietest one.
228 	 */
229 	for (i = 0; i < emu->max_voices; i++) {
230 		int state, val;
231 
232 		vp = &emu->voices[i];
233 		state = vp->state;
234 		if (state == SNDRV_EMUX_ST_OFF) {
235 			if (vp->ch < 0) {
236 				if (active_only)
237 					continue;
238 				bp = best + V_FREE;
239 			} else
240 				bp = best + V_OFF;
241 		}
242 		else if (state == SNDRV_EMUX_ST_RELEASED ||
243 			 state == SNDRV_EMUX_ST_PENDING) {
244 			bp = best + V_RELEASED;
245 #if 1
246 			val = snd_emu10k1_ptr_read(hw, CVCF_CURRENTVOL, vp->ch);
247 			if (! val)
248 				bp = best + V_OFF;
249 #endif
250 		}
251 		else if (state == SNDRV_EMUX_ST_STANDBY)
252 			continue;
253 		else if (state & SNDRV_EMUX_ST_ON)
254 			bp = best + V_PLAYING;
255 		else
256 			continue;
257 
258 		/* check if sample is finished playing (non-looping only) */
259 		if (bp != best + V_OFF && bp != best + V_FREE &&
260 		    (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_SINGLESHOT)) {
261 			val = snd_emu10k1_ptr_read(hw, CCCA_CURRADDR, vp->ch) - 64;
262 			if (val >= vp->reg.loopstart)
263 				bp = best + V_OFF;
264 		}
265 
266 		if (vp->time < bp->time) {
267 			bp->time = vp->time;
268 			bp->voice = i;
269 		}
270 	}
271 }
272 
273 /*
274  * get an empty voice
275  *
276  * emu->voice_lock is already held.
277  */
278 static struct snd_emux_voice *
279 get_voice(struct snd_emux *emu, struct snd_emux_port *port)
280 {
281 	struct snd_emu10k1 *hw;
282 	struct snd_emux_voice *vp;
283 	struct best_voice best[V_END];
284 	int i;
285 
286 	hw = emu->hw;
287 
288 	lookup_voices(emu, hw, best, 0);
289 	for (i = 0; i < V_END; i++) {
290 		if (best[i].voice >= 0) {
291 			vp = &emu->voices[best[i].voice];
292 			if (vp->ch < 0) {
293 				/* allocate a voice */
294 				struct snd_emu10k1_voice *hwvoice;
295 				if (snd_emu10k1_voice_alloc(hw, EMU10K1_SYNTH, 1, &hwvoice) < 0 || hwvoice == NULL)
296 					continue;
297 				vp->ch = hwvoice->number;
298 				emu->num_voices++;
299 			}
300 			return vp;
301 		}
302 	}
303 
304 	/* not found */
305 	return NULL;
306 }
307 
308 /*
309  * prepare envelopes and LFOs
310  */
311 static int
312 start_voice(struct snd_emux_voice *vp)
313 {
314 	unsigned int temp;
315 	int ch;
316 	u32 psst, dsl, map, ccca, vtarget;
317 	unsigned int addr, mapped_offset;
318 	struct snd_midi_channel *chan;
319 	struct snd_emu10k1 *hw;
320 	struct snd_emu10k1_memblk *emem;
321 
322 	hw = vp->hw;
323 	ch = vp->ch;
324 	if (snd_BUG_ON(ch < 0))
325 		return -EINVAL;
326 	chan = vp->chan;
327 
328 	emem = (struct snd_emu10k1_memblk *)vp->block;
329 	if (emem == NULL)
330 		return -EINVAL;
331 	emem->map_locked++;
332 	if (snd_emu10k1_memblk_map(hw, emem) < 0) {
333 		/* dev_err(hw->card->devK, "emu: cannot map!\n"); */
334 		return -ENOMEM;
335 	}
336 	mapped_offset = snd_emu10k1_memblk_offset(emem) >> 1;
337 	vp->reg.start += mapped_offset;
338 	vp->reg.end += mapped_offset;
339 	vp->reg.loopstart += mapped_offset;
340 	vp->reg.loopend += mapped_offset;
341 
342 	/* set channel routing */
343 	/* A = left(0), B = right(1), C = reverb(c), D = chorus(d) */
344 	if (hw->audigy) {
345 		temp = FXBUS_MIDI_LEFT | (FXBUS_MIDI_RIGHT << 8) |
346 			(FXBUS_MIDI_REVERB << 16) | (FXBUS_MIDI_CHORUS << 24);
347 		snd_emu10k1_ptr_write(hw, A_FXRT1, ch, temp);
348 	} else {
349 		temp = (FXBUS_MIDI_LEFT << 16) | (FXBUS_MIDI_RIGHT << 20) |
350 			(FXBUS_MIDI_REVERB << 24) | (FXBUS_MIDI_CHORUS << 28);
351 		snd_emu10k1_ptr_write(hw, FXRT, ch, temp);
352 	}
353 
354 	temp = vp->reg.parm.reverb;
355 	temp += (int)vp->chan->control[MIDI_CTL_E1_REVERB_DEPTH] * 9 / 10;
356 	LIMITMAX(temp, 255);
357 	addr = vp->reg.loopstart;
358 	psst = (temp << 24) | addr;
359 
360 	addr = vp->reg.loopend;
361 	temp = vp->reg.parm.chorus;
362 	temp += (int)chan->control[MIDI_CTL_E3_CHORUS_DEPTH] * 9 / 10;
363 	LIMITMAX(temp, 255);
364 	dsl = (temp << 24) | addr;
365 
366 	map = (hw->silent_page.addr << hw->address_mode) | (hw->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
367 
368 	addr = vp->reg.start + 64;
369 	temp = vp->reg.parm.filterQ;
370 	ccca = (temp << 28) | addr;
371 	if (vp->apitch < 0xe400)
372 		ccca |= CCCA_INTERPROM_0;
373 	else {
374 		unsigned int shift = (vp->apitch - 0xe000) >> 10;
375 		ccca |= shift << 25;
376 	}
377 	if (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_8BITS)
378 		ccca |= CCCA_8BITSELECT;
379 
380 	vtarget = (unsigned int)vp->vtarget << 16;
381 
382 	snd_emu10k1_ptr_write_multiple(hw, ch,
383 		/* channel to be silent and idle */
384 		DCYSUSV, 0,
385 		VTFT, VTFT_FILTERTARGET_MASK,
386 		CVCF, CVCF_CURRENTFILTER_MASK,
387 		PTRX, 0,
388 		CPF, 0,
389 
390 		/* set pitch offset */
391 		IP, vp->apitch,
392 
393 		/* set envelope parameters */
394 		ENVVAL, vp->reg.parm.moddelay,
395 		ATKHLDM, vp->reg.parm.modatkhld,
396 		DCYSUSM, vp->reg.parm.moddcysus,
397 		ENVVOL, vp->reg.parm.voldelay,
398 		ATKHLDV, vp->reg.parm.volatkhld,
399 		/* decay/sustain parameter for volume envelope is used
400 		   for triggerg the voice */
401 
402 		/* cutoff and volume */
403 		IFATN, (unsigned int)vp->acutoff << 8 | (unsigned char)vp->avol,
404 
405 		/* modulation envelope heights */
406 		PEFE, vp->reg.parm.pefe,
407 
408 		/* lfo1/2 delay */
409 		LFOVAL1, vp->reg.parm.lfo1delay,
410 		LFOVAL2, vp->reg.parm.lfo2delay,
411 
412 		/* lfo1 pitch & cutoff shift */
413 		FMMOD, make_fmmod(vp),
414 		/* lfo1 volume & freq */
415 		TREMFRQ, vp->reg.parm.tremfrq,
416 		/* lfo2 pitch & freq */
417 		FM2FRQ2, make_fm2frq2(vp),
418 
419 		/* reverb and loop start (reverb 8bit, MSB) */
420 		PSST, psst,
421 
422 		/* chorus & loop end (chorus 8bit, MSB) */
423 		DSL, dsl,
424 
425 		/* clear filter delay memory */
426 		Z1, 0,
427 		Z2, 0,
428 
429 		/* invalidate maps */
430 		MAPA, map,
431 		MAPB, map,
432 
433 		/* Q & current address (Q 4bit value, MSB) */
434 		CCCA, ccca,
435 
436 		/* cache */
437 		CCR, REG_VAL_PUT(CCR_CACHEINVALIDSIZE, 64),
438 
439 		/* reset volume */
440 		VTFT, vtarget | vp->ftarget,
441 		CVCF, vtarget | CVCF_CURRENTFILTER_MASK,
442 
443 		REGLIST_END);
444 
445 	return 0;
446 }
447 
448 /*
449  * Start envelope
450  */
451 static void
452 trigger_voice(struct snd_emux_voice *vp)
453 {
454 	unsigned int ptarget;
455 	struct snd_emu10k1 *hw;
456 	struct snd_emu10k1_memblk *emem;
457 
458 	hw = vp->hw;
459 
460 	emem = (struct snd_emu10k1_memblk *)vp->block;
461 	if (! emem || emem->mapped_page < 0)
462 		return; /* not mapped */
463 
464 #if 0
465 	ptarget = (unsigned int)vp->ptarget << 16;
466 #else
467 	ptarget = IP_TO_CP(vp->apitch);
468 #endif
469 	snd_emu10k1_ptr_write_multiple(hw, vp->ch,
470 		/* set pitch target and pan (volume) */
471 		PTRX, ptarget | (vp->apan << 8) | vp->aaux,
472 
473 		/* current pitch and fractional address */
474 		CPF, ptarget,
475 
476 		/* enable envelope engine */
477 		DCYSUSV, vp->reg.parm.voldcysus | DCYSUSV_CHANNELENABLE_MASK,
478 
479 		REGLIST_END);
480 }
481 
482 #define MOD_SENSE 18
483 
484 /* calculate lfo1 modulation height and cutoff register */
485 static u32
486 make_fmmod(struct snd_emux_voice *vp)
487 {
488 	short pitch;
489 	unsigned char cutoff;
490 	int modulation;
491 
492 	pitch = (char)(vp->reg.parm.fmmod>>8);
493 	cutoff = (vp->reg.parm.fmmod & 0xff);
494 	modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
495 	pitch += (MOD_SENSE * modulation) / 1200;
496 	LIMITVALUE(pitch, -128, 127);
497 	return ((unsigned char)pitch << 8) | cutoff;
498 }
499 
500 /* calculate set lfo2 pitch & frequency register */
501 static u32
502 make_fm2frq2(struct snd_emux_voice *vp)
503 {
504 	short pitch;
505 	unsigned char freq;
506 	int modulation;
507 
508 	pitch = (char)(vp->reg.parm.fm2frq2>>8);
509 	freq = vp->reg.parm.fm2frq2 & 0xff;
510 	modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
511 	pitch += (MOD_SENSE * modulation) / 1200;
512 	LIMITVALUE(pitch, -128, 127);
513 	return ((unsigned char)pitch << 8) | freq;
514 }
515