xref: /openbmc/linux/sound/pci/emu10k1/emu10k1_callback.c (revision a915d60426d4348a0b91f9870e299056fd604a32)
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_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) - 64;
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, 1, NULL, &hwvoice) < 0)
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 + 64;
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 		/* cache */
432 		CCR, REG_VAL_PUT(CCR_CACHEINVALIDSIZE, 64),
433 
434 		/* reset volume */
435 		VTFT, vtarget | vp->ftarget,
436 		CVCF, vtarget | CVCF_CURRENTFILTER_MASK,
437 
438 		REGLIST_END);
439 
440 	hw->voices[ch].dirty = 1;
441 	return 0;
442 }
443 
444 /*
445  * Start envelope
446  */
447 static void
448 trigger_voice(struct snd_emux_voice *vp)
449 {
450 	unsigned int ptarget;
451 	struct snd_emu10k1 *hw;
452 	struct snd_emu10k1_memblk *emem;
453 
454 	hw = vp->hw;
455 
456 	emem = (struct snd_emu10k1_memblk *)vp->block;
457 	if (! emem || emem->mapped_page < 0)
458 		return; /* not mapped */
459 
460 #if 0
461 	ptarget = (unsigned int)vp->ptarget << 16;
462 #else
463 	ptarget = IP_TO_CP(vp->apitch);
464 #endif
465 	snd_emu10k1_ptr_write_multiple(hw, vp->ch,
466 		/* set pitch target and pan (volume) */
467 		PTRX, ptarget | (vp->apan << 8) | vp->aaux,
468 
469 		/* current pitch and fractional address */
470 		CPF, ptarget,
471 
472 		/* enable envelope engine */
473 		DCYSUSV, vp->reg.parm.voldcysus | DCYSUSV_CHANNELENABLE_MASK,
474 
475 		REGLIST_END);
476 }
477 
478 #define MOD_SENSE 18
479 
480 /* calculate lfo1 modulation height and cutoff register */
481 static u32
482 make_fmmod(struct snd_emux_voice *vp)
483 {
484 	short pitch;
485 	unsigned char cutoff;
486 	int modulation;
487 
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;
494 }
495 
496 /* calculate set lfo2 pitch & frequency register */
497 static u32
498 make_fm2frq2(struct snd_emux_voice *vp)
499 {
500 	short pitch;
501 	unsigned char freq;
502 	int modulation;
503 
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;
510 }
511