xref: /openbmc/linux/sound/pci/emu10k1/emu10k1_callback.c (revision 46055699e5f81db8c70946609f445c572983eca5)
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(hw, DCYSUSV, vp->ch,
140 		DCYSUSV_PHASE1_MASK | DCYSUSV_DECAYTIME_MASK | DCYSUSV_CHANNELENABLE_MASK);
141 	if (vp->block) {
142 		struct snd_emu10k1_memblk *emem;
143 		emem = (struct snd_emu10k1_memblk *)vp->block;
144 		if (emem->map_locked > 0)
145 			emem->map_locked--;
146 	}
147 }
148 
149 /*
150  * release the voice to system
151  */
152 static void
153 free_voice(struct snd_emux_voice *vp)
154 {
155 	struct snd_emu10k1 *hw;
156 
157 	hw = vp->hw;
158 	/* FIXME: emu10k1_synth is broken. */
159 	/* This can get called with hw == 0 */
160 	/* Problem apparent on plug, unplug then plug */
161 	/* on the Audigy 2 ZS Notebook. */
162 	if (hw && (vp->ch >= 0)) {
163 		snd_emu10k1_ptr_write(hw, IFATN, vp->ch, 0xff00);
164 		snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0x807f | DCYSUSV_CHANNELENABLE_MASK);
165 		// snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0);
166 		snd_emu10k1_ptr_write(hw, VTFT, vp->ch, 0xffff);
167 		snd_emu10k1_ptr_write(hw, CVCF, vp->ch, 0xffff);
168 		snd_emu10k1_voice_free(hw, &hw->voices[vp->ch]);
169 		vp->emu->num_voices--;
170 		vp->ch = -1;
171 	}
172 }
173 
174 
175 /*
176  * update registers
177  */
178 static void
179 update_voice(struct snd_emux_voice *vp, int update)
180 {
181 	struct snd_emu10k1 *hw;
182 
183 	hw = vp->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);
191 	}
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);
200 }
201 
202 
203 /*
204  * look up voice table - get the best voice in order of preference
205  */
206 /* spinlock held! */
207 static void
208 lookup_voices(struct snd_emux *emu, struct snd_emu10k1 *hw,
209 	      struct best_voice *best, int active_only)
210 {
211 	struct snd_emux_voice *vp;
212 	struct best_voice *bp;
213 	int  i;
214 
215 	for (i = 0; i < V_END; i++) {
216 		best[i].time = (unsigned int)-1; /* XXX MAX_?INT really */
217 		best[i].voice = -1;
218 	}
219 
220 	/*
221 	 * Go through them all and get a best one to use.
222 	 * NOTE: could also look at volume and pick the quietest one.
223 	 */
224 	for (i = 0; i < emu->max_voices; i++) {
225 		int state, val;
226 
227 		vp = &emu->voices[i];
228 		state = vp->state;
229 		if (state == SNDRV_EMUX_ST_OFF) {
230 			if (vp->ch < 0) {
231 				if (active_only)
232 					continue;
233 				bp = best + V_FREE;
234 			} else
235 				bp = best + V_OFF;
236 		}
237 		else if (state == SNDRV_EMUX_ST_RELEASED ||
238 			 state == SNDRV_EMUX_ST_PENDING) {
239 			bp = best + V_RELEASED;
240 #if 1
241 			val = snd_emu10k1_ptr_read(hw, CVCF_CURRENTVOL, vp->ch);
242 			if (! val)
243 				bp = best + V_OFF;
244 #endif
245 		}
246 		else if (state == SNDRV_EMUX_ST_STANDBY)
247 			continue;
248 		else if (state & SNDRV_EMUX_ST_ON)
249 			bp = best + V_PLAYING;
250 		else
251 			continue;
252 
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);
257 			if (val >= vp->reg.loopstart)
258 				bp = best + V_OFF;
259 		}
260 
261 		if (vp->time < bp->time) {
262 			bp->time = vp->time;
263 			bp->voice = i;
264 		}
265 	}
266 }
267 
268 /*
269  * get an empty voice
270  *
271  * emu->voice_lock is already held.
272  */
273 static struct snd_emux_voice *
274 get_voice(struct snd_emux *emu, struct snd_emux_port *port)
275 {
276 	struct snd_emu10k1 *hw;
277 	struct snd_emux_voice *vp;
278 	struct best_voice best[V_END];
279 	int i;
280 
281 	hw = emu->hw;
282 
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];
287 			if (vp->ch < 0) {
288 				/* allocate a voice */
289 				struct snd_emu10k1_voice *hwvoice;
290 				if (snd_emu10k1_voice_alloc(hw, EMU10K1_SYNTH, 1, &hwvoice) < 0 || hwvoice == NULL)
291 					continue;
292 				vp->ch = hwvoice->number;
293 				emu->num_voices++;
294 			}
295 			return vp;
296 		}
297 	}
298 
299 	/* not found */
300 	return NULL;
301 }
302 
303 /*
304  * prepare envelopes and LFOs
305  */
306 static int
307 start_voice(struct snd_emux_voice *vp)
308 {
309 	unsigned int temp;
310 	int ch;
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;
316 
317 	hw = vp->hw;
318 	ch = vp->ch;
319 	if (snd_BUG_ON(ch < 0))
320 		return -EINVAL;
321 	chan = vp->chan;
322 
323 	emem = (struct snd_emu10k1_memblk *)vp->block;
324 	if (emem == NULL)
325 		return -EINVAL;
326 	emem->map_locked++;
327 	if (snd_emu10k1_memblk_map(hw, emem) < 0) {
328 		/* dev_err(hw->card->devK, "emu: cannot map!\n"); */
329 		return -ENOMEM;
330 	}
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;
336 
337 	/* set channel routing */
338 	/* A = left(0), B = right(1), C = reverb(c), D = chorus(d) */
339 	if (hw->audigy) {
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);
343 	} else {
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);
347 	}
348 
349 	temp = vp->reg.parm.reverb;
350 	temp += (int)vp->chan->control[MIDI_CTL_E1_REVERB_DEPTH] * 9 / 10;
351 	LIMITMAX(temp, 255);
352 	addr = vp->reg.loopstart;
353 	psst = (temp << 24) | addr;
354 
355 	addr = vp->reg.loopend;
356 	temp = vp->reg.parm.chorus;
357 	temp += (int)chan->control[MIDI_CTL_E3_CHORUS_DEPTH] * 9 / 10;
358 	LIMITMAX(temp, 255);
359 	dsl = (temp << 24) | addr;
360 
361 	map = (hw->silent_page.addr << hw->address_mode) | (hw->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
362 
363 	addr = vp->reg.start;
364 	temp = vp->reg.parm.filterQ;
365 	ccca = (temp << 28) | addr;
366 	if (vp->apitch < 0xe400)
367 		ccca |= CCCA_INTERPROM_0;
368 	else {
369 		unsigned int shift = (vp->apitch - 0xe000) >> 10;
370 		ccca |= shift << 25;
371 	}
372 	if (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_8BITS)
373 		ccca |= CCCA_8BITSELECT;
374 
375 	vtarget = (unsigned int)vp->vtarget << 16;
376 
377 	snd_emu10k1_ptr_write_multiple(hw, ch,
378 		/* channel to be silent and idle */
379 		DCYSUSV, 0,
380 		VTFT, VTFT_FILTERTARGET_MASK,
381 		CVCF, CVCF_CURRENTFILTER_MASK,
382 		PTRX, 0,
383 		CPF, 0,
384 
385 		/* set pitch offset */
386 		IP, vp->apitch,
387 
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 */
396 
397 		/* cutoff and volume */
398 		IFATN, (unsigned int)vp->acutoff << 8 | (unsigned char)vp->avol,
399 
400 		/* modulation envelope heights */
401 		PEFE, vp->reg.parm.pefe,
402 
403 		/* lfo1/2 delay */
404 		LFOVAL1, vp->reg.parm.lfo1delay,
405 		LFOVAL2, vp->reg.parm.lfo2delay,
406 
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),
413 
414 		/* reverb and loop start (reverb 8bit, MSB) */
415 		PSST, psst,
416 
417 		/* chorus & loop end (chorus 8bit, MSB) */
418 		DSL, dsl,
419 
420 		/* clear filter delay memory */
421 		Z1, 0,
422 		Z2, 0,
423 
424 		/* invalidate maps */
425 		MAPA, map,
426 		MAPB, map,
427 
428 		/* Q & current address (Q 4bit value, MSB) */
429 		CCCA, ccca,
430 
431 		/* reset volume */
432 		VTFT, vtarget | vp->ftarget,
433 		CVCF, vtarget | CVCF_CURRENTFILTER_MASK,
434 
435 		REGLIST_END);
436 #if 0
437 	/* cache */
438 	{
439 		unsigned int val, sample;
440 		val = 32;
441 		if (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_8BITS)
442 			sample = 0x80808080;
443 		else {
444 			sample = 0;
445 			val *= 2;
446 		}
447 
448 		/* cache */
449 		snd_emu10k1_ptr_write(hw, CCR, ch, 0x1c << 16);
450 		snd_emu10k1_ptr_write(hw, CDE, ch, sample);
451 		snd_emu10k1_ptr_write(hw, CDF, ch, sample);
452 
453 		/* invalidate maps */
454 		temp = ((unsigned int)hw->silent_page.addr << hw_address_mode) | (hw->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
455 		snd_emu10k1_ptr_write(hw, MAPA, ch, temp);
456 		snd_emu10k1_ptr_write(hw, MAPB, ch, temp);
457 
458 		/* fill cache */
459 		val -= 4;
460 		val <<= 25;
461 		val |= 0x1c << 16;
462 		snd_emu10k1_ptr_write(hw, CCR, ch, val);
463 	}
464 #endif
465 
466 	return 0;
467 }
468 
469 /*
470  * Start envelope
471  */
472 static void
473 trigger_voice(struct snd_emux_voice *vp)
474 {
475 	unsigned int ptarget;
476 	struct snd_emu10k1 *hw;
477 	struct snd_emu10k1_memblk *emem;
478 
479 	hw = vp->hw;
480 
481 	emem = (struct snd_emu10k1_memblk *)vp->block;
482 	if (! emem || emem->mapped_page < 0)
483 		return; /* not mapped */
484 
485 #if 0
486 	ptarget = (unsigned int)vp->ptarget << 16;
487 #else
488 	ptarget = IP_TO_CP(vp->apitch);
489 #endif
490 	snd_emu10k1_ptr_write_multiple(hw, vp->ch,
491 		/* set pitch target and pan (volume) */
492 		PTRX, ptarget | (vp->apan << 8) | vp->aaux,
493 
494 		/* current pitch and fractional address */
495 		CPF, ptarget,
496 
497 		/* enable envelope engine */
498 		DCYSUSV, vp->reg.parm.voldcysus | DCYSUSV_CHANNELENABLE_MASK,
499 
500 		REGLIST_END);
501 }
502 
503 #define MOD_SENSE 18
504 
505 /* calculate lfo1 modulation height and cutoff register */
506 static u32
507 make_fmmod(struct snd_emux_voice *vp)
508 {
509 	short pitch;
510 	unsigned char cutoff;
511 	int modulation;
512 
513 	pitch = (char)(vp->reg.parm.fmmod>>8);
514 	cutoff = (vp->reg.parm.fmmod & 0xff);
515 	modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
516 	pitch += (MOD_SENSE * modulation) / 1200;
517 	LIMITVALUE(pitch, -128, 127);
518 	return ((unsigned char)pitch << 8) | cutoff;
519 }
520 
521 /* calculate set lfo2 pitch & frequency register */
522 static u32
523 make_fm2frq2(struct snd_emux_voice *vp)
524 {
525 	short pitch;
526 	unsigned char freq;
527 	int modulation;
528 
529 	pitch = (char)(vp->reg.parm.fm2frq2>>8);
530 	freq = vp->reg.parm.fm2frq2 & 0xff;
531 	modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
532 	pitch += (MOD_SENSE * modulation) / 1200;
533 	LIMITVALUE(pitch, -128, 127);
534 	return ((unsigned char)pitch << 8) | freq;
535 }
536