xref: /openbmc/linux/sound/pci/lola/lola_mixer.c (revision 461ba3e7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Support for Digigram Lola PCI-e boards
4  *
5  *  Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/vmalloc.h>
11 #include <linux/io.h>
12 #include <sound/core.h>
13 #include <sound/control.h>
14 #include <sound/pcm.h>
15 #include <sound/tlv.h>
16 #include "lola.h"
17 
18 static int lola_init_pin(struct lola *chip, struct lola_pin *pin,
19 			 int dir, int nid)
20 {
21 	unsigned int val;
22 	int err;
23 
24 	pin->nid = nid;
25 	err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
26 	if (err < 0) {
27 		dev_err(chip->card->dev, "Can't read wcaps for 0x%x\n", nid);
28 		return err;
29 	}
30 	val &= 0x00f00fff; /* test TYPE and bits 0..11 */
31 	if (val == 0x00400200)    /* Type = 4, Digital = 1 */
32 		pin->is_analog = false;
33 	else if (val == 0x0040000a && dir == CAPT) /* Dig=0, InAmp/ovrd */
34 		pin->is_analog = true;
35 	else if (val == 0x0040000c && dir == PLAY) /* Dig=0, OutAmp/ovrd */
36 		pin->is_analog = true;
37 	else {
38 		dev_err(chip->card->dev, "Invalid wcaps 0x%x for 0x%x\n", val, nid);
39 		return -EINVAL;
40 	}
41 
42 	/* analog parameters only following, so continue in case of Digital pin
43 	 */
44 	if (!pin->is_analog)
45 		return 0;
46 
47 	if (dir == PLAY)
48 		err = lola_read_param(chip, nid, LOLA_PAR_AMP_OUT_CAP, &val);
49 	else
50 		err = lola_read_param(chip, nid, LOLA_PAR_AMP_IN_CAP, &val);
51 	if (err < 0) {
52 		dev_err(chip->card->dev, "Can't read AMP-caps for 0x%x\n", nid);
53 		return err;
54 	}
55 
56 	pin->amp_mute = LOLA_AMP_MUTE_CAPABLE(val);
57 	pin->amp_step_size = LOLA_AMP_STEP_SIZE(val);
58 	pin->amp_num_steps = LOLA_AMP_NUM_STEPS(val);
59 	if (pin->amp_num_steps) {
60 		/* zero as mute state */
61 		pin->amp_num_steps++;
62 		pin->amp_step_size++;
63 	}
64 	pin->amp_offset = LOLA_AMP_OFFSET(val);
65 
66 	err = lola_codec_read(chip, nid, LOLA_VERB_GET_MAX_LEVEL, 0, 0, &val,
67 			      NULL);
68 	if (err < 0) {
69 		dev_err(chip->card->dev, "Can't get MAX_LEVEL 0x%x\n", nid);
70 		return err;
71 	}
72 	pin->max_level = val & 0x3ff;   /* 10 bits */
73 
74 	pin->config_default_reg = 0;
75 	pin->fixed_gain_list_len = 0;
76 	pin->cur_gain_step = 0;
77 
78 	return 0;
79 }
80 
81 int lola_init_pins(struct lola *chip, int dir, int *nidp)
82 {
83 	int i, err, nid;
84 	nid = *nidp;
85 	for (i = 0; i < chip->pin[dir].num_pins; i++, nid++) {
86 		err = lola_init_pin(chip, &chip->pin[dir].pins[i], dir, nid);
87 		if (err < 0)
88 			return err;
89 		if (chip->pin[dir].pins[i].is_analog)
90 			chip->pin[dir].num_analog_pins++;
91 	}
92 	*nidp = nid;
93 	return 0;
94 }
95 
96 void lola_free_mixer(struct lola *chip)
97 {
98 	vfree(chip->mixer.array_saved);
99 }
100 
101 int lola_init_mixer_widget(struct lola *chip, int nid)
102 {
103 	unsigned int val;
104 	int err;
105 
106 	err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
107 	if (err < 0) {
108 		dev_err(chip->card->dev, "Can't read wcaps for 0x%x\n", nid);
109 		return err;
110 	}
111 
112 	if ((val & 0xfff00000) != 0x02f00000) { /* test SubType and Type */
113 		dev_dbg(chip->card->dev, "No valid mixer widget\n");
114 		return 0;
115 	}
116 
117 	chip->mixer.nid = nid;
118 	chip->mixer.caps = val;
119 	chip->mixer.array = (struct lola_mixer_array __iomem *)
120 		(chip->bar[BAR1].remap_addr + LOLA_BAR1_SOURCE_GAIN_ENABLE);
121 
122 	/* reserve memory to copy mixer data for sleep mode transitions */
123 	chip->mixer.array_saved = vmalloc(sizeof(struct lola_mixer_array));
124 	if (!chip->mixer.array_saved)
125 		return -ENOMEM;
126 
127 	/* mixer matrix sources are physical input data and play streams */
128 	chip->mixer.src_stream_outs = chip->pcm[PLAY].num_streams;
129 	chip->mixer.src_phys_ins = chip->pin[CAPT].num_pins;
130 
131 	/* mixer matrix destinations are record streams and physical output */
132 	chip->mixer.dest_stream_ins = chip->pcm[CAPT].num_streams;
133 	chip->mixer.dest_phys_outs = chip->pin[PLAY].num_pins;
134 
135 	/* mixer matrix may have unused areas between PhysIn and
136 	 * Play or Record and PhysOut zones
137 	 */
138 	chip->mixer.src_stream_out_ofs = chip->mixer.src_phys_ins +
139 		LOLA_MIXER_SRC_INPUT_PLAY_SEPARATION(val);
140 	chip->mixer.dest_phys_out_ofs = chip->mixer.dest_stream_ins +
141 		LOLA_MIXER_DEST_REC_OUTPUT_SEPARATION(val);
142 
143 	/* example : MixerMatrix of LoLa881 (LoLa16161 uses unused zones)
144 	 * +-+  0-------8------16-------8------16
145 	 * | |  |       |       |       |       |
146 	 * |s|  | INPUT |       | INPUT |       |
147 	 * | |->|  ->   |unused |  ->   |unused |
148 	 * |r|  |CAPTURE|       | OUTPUT|       |
149 	 * | |  |  MIX  |       |  MIX  |       |
150 	 * |c|  8--------------------------------
151 	 * | |  |       |       |       |       |
152 	 * | |  |       |       |       |       |
153 	 * |g|  |unused |unused |unused |unused |
154 	 * | |  |       |       |       |       |
155 	 * |a|  |       |       |       |       |
156 	 * | |  16-------------------------------
157 	 * |i|  |       |       |       |       |
158 	 * | |  | PLAYBK|       | PLAYBK|       |
159 	 * |n|->|  ->   |unused |  ->   |unused |
160 	 * | |  |CAPTURE|       | OUTPUT|       |
161 	 * | |  |  MIX  |       |  MIX  |       |
162 	 * |a|  8--------------------------------
163 	 * |r|  |       |       |       |       |
164 	 * |r|  |       |       |       |       |
165 	 * |a|  |unused |unused |unused |unused |
166 	 * |y|  |       |       |       |       |
167 	 * | |  |       |       |       |       |
168 	 * +++  16--|---------------|------------
169 	 *      +---V---------------V-----------+
170 	 *      |  dest_mix_gain_enable array   |
171 	 *      +-------------------------------+
172 	 */
173 	/* example : MixerMatrix of LoLa280
174 	 * +-+  0-------8-2
175 	 * | |  |       | |
176 	 * |s|  | INPUT | |     INPUT
177 	 * |r|->|  ->   | |      ->
178 	 * |c|  |CAPTURE| | <-  OUTPUT
179 	 * | |  |  MIX  | |      MIX
180 	 * |g|  8----------
181 	 * |a|  |       | |
182 	 * |i|  | PLAYBK| |     PLAYBACK
183 	 * |n|->|  ->   | |      ->
184 	 * | |  |CAPTURE| | <-  OUTPUT
185 	 * |a|  |  MIX  | |      MIX
186 	 * |r|  8---|----|-
187 	 * |r|  +---V----V-------------------+
188 	 * |a|  | dest_mix_gain_enable array |
189 	 * |y|  +----------------------------+
190 	 */
191 	if (chip->mixer.src_stream_out_ofs > MAX_AUDIO_INOUT_COUNT ||
192 	    chip->mixer.dest_phys_out_ofs > MAX_STREAM_IN_COUNT) {
193 		dev_err(chip->card->dev, "Invalid mixer widget size\n");
194 		return -EINVAL;
195 	}
196 
197 	chip->mixer.src_mask = ((1U << chip->mixer.src_phys_ins) - 1) |
198 		(((1U << chip->mixer.src_stream_outs) - 1)
199 		 << chip->mixer.src_stream_out_ofs);
200 	chip->mixer.dest_mask = ((1U << chip->mixer.dest_stream_ins) - 1) |
201 		(((1U << chip->mixer.dest_phys_outs) - 1)
202 		 << chip->mixer.dest_phys_out_ofs);
203 
204 	dev_dbg(chip->card->dev, "Mixer src_mask=%x, dest_mask=%x\n",
205 		    chip->mixer.src_mask, chip->mixer.dest_mask);
206 
207 	return 0;
208 }
209 
210 static int lola_mixer_set_src_gain(struct lola *chip, unsigned int id,
211 				   unsigned short gain, bool on)
212 {
213 	unsigned int oldval, val;
214 
215 	if (!(chip->mixer.src_mask & (1 << id)))
216 		return -EINVAL;
217 	oldval = val = readl(&chip->mixer.array->src_gain_enable);
218 	if (on)
219 		val |= (1 << id);
220 	else
221 		val &= ~(1 << id);
222 	/* test if values unchanged */
223 	if ((val == oldval) &&
224 	    (gain == readw(&chip->mixer.array->src_gain[id])))
225 		return 0;
226 
227 	dev_dbg(chip->card->dev,
228 		"lola_mixer_set_src_gain (id=%d, gain=%d) enable=%x\n",
229 			id, gain, val);
230 	writew(gain, &chip->mixer.array->src_gain[id]);
231 	writel(val, &chip->mixer.array->src_gain_enable);
232 	lola_codec_flush(chip);
233 	/* inform micro-controller about the new source gain */
234 	return lola_codec_write(chip, chip->mixer.nid,
235 				LOLA_VERB_SET_SOURCE_GAIN, id, 0);
236 }
237 
238 #if 0 /* not used */
239 static int lola_mixer_set_src_gains(struct lola *chip, unsigned int mask,
240 				    unsigned short *gains)
241 {
242 	int i;
243 
244 	if ((chip->mixer.src_mask & mask) != mask)
245 		return -EINVAL;
246 	for (i = 0; i < LOLA_MIXER_DIM; i++) {
247 		if (mask & (1 << i)) {
248 			writew(*gains, &chip->mixer.array->src_gain[i]);
249 			gains++;
250 		}
251 	}
252 	writel(mask, &chip->mixer.array->src_gain_enable);
253 	lola_codec_flush(chip);
254 	if (chip->mixer.caps & LOLA_PEAK_METER_CAN_AGC_MASK) {
255 		/* update for all srcs at once */
256 		return lola_codec_write(chip, chip->mixer.nid,
257 					LOLA_VERB_SET_SOURCE_GAIN, 0x80, 0);
258 	}
259 	/* update manually */
260 	for (i = 0; i < LOLA_MIXER_DIM; i++) {
261 		if (mask & (1 << i)) {
262 			lola_codec_write(chip, chip->mixer.nid,
263 					 LOLA_VERB_SET_SOURCE_GAIN, i, 0);
264 		}
265 	}
266 	return 0;
267 }
268 #endif /* not used */
269 
270 static int lola_mixer_set_mapping_gain(struct lola *chip,
271 				       unsigned int src, unsigned int dest,
272 				       unsigned short gain, bool on)
273 {
274 	unsigned int val;
275 
276 	if (!(chip->mixer.src_mask & (1 << src)) ||
277 	    !(chip->mixer.dest_mask & (1 << dest)))
278 		return -EINVAL;
279 	if (on)
280 		writew(gain, &chip->mixer.array->dest_mix_gain[dest][src]);
281 	val = readl(&chip->mixer.array->dest_mix_gain_enable[dest]);
282 	if (on)
283 		val |= (1 << src);
284 	else
285 		val &= ~(1 << src);
286 	writel(val, &chip->mixer.array->dest_mix_gain_enable[dest]);
287 	lola_codec_flush(chip);
288 	return lola_codec_write(chip, chip->mixer.nid, LOLA_VERB_SET_MIX_GAIN,
289 				src, dest);
290 }
291 
292 #if 0 /* not used */
293 static int lola_mixer_set_dest_gains(struct lola *chip, unsigned int id,
294 				     unsigned int mask, unsigned short *gains)
295 {
296 	int i;
297 
298 	if (!(chip->mixer.dest_mask & (1 << id)) ||
299 	    (chip->mixer.src_mask & mask) != mask)
300 		return -EINVAL;
301 	for (i = 0; i < LOLA_MIXER_DIM; i++) {
302 		if (mask & (1 << i)) {
303 			writew(*gains, &chip->mixer.array->dest_mix_gain[id][i]);
304 			gains++;
305 		}
306 	}
307 	writel(mask, &chip->mixer.array->dest_mix_gain_enable[id]);
308 	lola_codec_flush(chip);
309 	/* update for all dests at once */
310 	return lola_codec_write(chip, chip->mixer.nid,
311 				LOLA_VERB_SET_DESTINATION_GAIN, id, 0);
312 }
313 #endif /* not used */
314 
315 /*
316  */
317 
318 static int set_analog_volume(struct lola *chip, int dir,
319 			     unsigned int idx, unsigned int val,
320 			     bool external_call);
321 
322 int lola_setup_all_analog_gains(struct lola *chip, int dir, bool mute)
323 {
324 	struct lola_pin *pin;
325 	int idx, max_idx;
326 
327 	pin = chip->pin[dir].pins;
328 	max_idx = chip->pin[dir].num_pins;
329 	for (idx = 0; idx < max_idx; idx++) {
330 		if (pin[idx].is_analog) {
331 			unsigned int val = mute ? 0 : pin[idx].cur_gain_step;
332 			/* set volume and do not save the value */
333 			set_analog_volume(chip, dir, idx, val, false);
334 		}
335 	}
336 	return lola_codec_flush(chip);
337 }
338 
339 void lola_save_mixer(struct lola *chip)
340 {
341 	/* mute analog output */
342 	if (chip->mixer.array_saved) {
343 		/* store contents of mixer array */
344 		memcpy_fromio(chip->mixer.array_saved, chip->mixer.array,
345 			      sizeof(*chip->mixer.array));
346 	}
347 	lola_setup_all_analog_gains(chip, PLAY, true); /* output mute */
348 }
349 
350 void lola_restore_mixer(struct lola *chip)
351 {
352 	int i;
353 
354 	/*lola_reset_setups(chip);*/
355 	if (chip->mixer.array_saved) {
356 		/* restore contents of mixer array */
357 		memcpy_toio(chip->mixer.array, chip->mixer.array_saved,
358 			    sizeof(*chip->mixer.array));
359 		/* inform micro-controller about all restored values
360 		 * and ignore return values
361 		 */
362 		for (i = 0; i < chip->mixer.src_phys_ins; i++)
363 			lola_codec_write(chip, chip->mixer.nid,
364 					 LOLA_VERB_SET_SOURCE_GAIN,
365 					 i, 0);
366 		for (i = 0; i < chip->mixer.src_stream_outs; i++)
367 			lola_codec_write(chip, chip->mixer.nid,
368 					 LOLA_VERB_SET_SOURCE_GAIN,
369 					 chip->mixer.src_stream_out_ofs + i, 0);
370 		for (i = 0; i < chip->mixer.dest_stream_ins; i++)
371 			lola_codec_write(chip, chip->mixer.nid,
372 					 LOLA_VERB_SET_DESTINATION_GAIN,
373 					 i, 0);
374 		for (i = 0; i < chip->mixer.dest_phys_outs; i++)
375 			lola_codec_write(chip, chip->mixer.nid,
376 					 LOLA_VERB_SET_DESTINATION_GAIN,
377 					 chip->mixer.dest_phys_out_ofs + i, 0);
378 		lola_codec_flush(chip);
379 	}
380 }
381 
382 /*
383  */
384 
385 static int set_analog_volume(struct lola *chip, int dir,
386 			     unsigned int idx, unsigned int val,
387 			     bool external_call)
388 {
389 	struct lola_pin *pin;
390 	int err;
391 
392 	if (idx >= chip->pin[dir].num_pins)
393 		return -EINVAL;
394 	pin = &chip->pin[dir].pins[idx];
395 	if (!pin->is_analog || pin->amp_num_steps <= val)
396 		return -EINVAL;
397 	if (external_call && pin->cur_gain_step == val)
398 		return 0;
399 	if (external_call)
400 		lola_codec_flush(chip);
401 	dev_dbg(chip->card->dev,
402 		"set_analog_volume (dir=%d idx=%d, volume=%d)\n",
403 			dir, idx, val);
404 	err = lola_codec_write(chip, pin->nid,
405 			       LOLA_VERB_SET_AMP_GAIN_MUTE, val, 0);
406 	if (err < 0)
407 		return err;
408 	if (external_call)
409 		pin->cur_gain_step = val;
410 	return 0;
411 }
412 
413 int lola_set_src_config(struct lola *chip, unsigned int src_mask, bool update)
414 {
415 	int ret = 0;
416 	int success = 0;
417 	int n, err;
418 
419 	/* SRC can be activated and the dwInputSRCMask is valid? */
420 	if ((chip->input_src_caps_mask & src_mask) != src_mask)
421 		return -EINVAL;
422 	/* handle all even Inputs - SRC is a stereo setting !!! */
423 	for (n = 0; n < chip->pin[CAPT].num_pins; n += 2) {
424 		unsigned int mask = 3U << n; /* handle the stereo case */
425 		unsigned int new_src, src_state;
426 		if (!(chip->input_src_caps_mask & mask))
427 			continue;
428 		/* if one IO needs SRC, both stereo IO will get SRC */
429 		new_src = (src_mask & mask) != 0;
430 		if (update) {
431 			src_state = (chip->input_src_mask & mask) != 0;
432 			if (src_state == new_src)
433 				continue;   /* nothing to change for this IO */
434 		}
435 		err = lola_codec_write(chip, chip->pcm[CAPT].streams[n].nid,
436 				       LOLA_VERB_SET_SRC, new_src, 0);
437 		if (!err)
438 			success++;
439 		else
440 			ret = err;
441 	}
442 	if (success)
443 		ret = lola_codec_flush(chip);
444 	if (!ret)
445 		chip->input_src_mask = src_mask;
446 	return ret;
447 }
448 
449 /*
450  */
451 static int init_mixer_values(struct lola *chip)
452 {
453 	int i;
454 
455 	/* all sample rate converters on */
456 	lola_set_src_config(chip, (1 << chip->pin[CAPT].num_pins) - 1, false);
457 
458 	/* clear all mixer matrix settings */
459 	memset_io(chip->mixer.array, 0, sizeof(*chip->mixer.array));
460 	/* inform firmware about all updated matrix columns - capture part */
461 	for (i = 0; i < chip->mixer.dest_stream_ins; i++)
462 		lola_codec_write(chip, chip->mixer.nid,
463 				 LOLA_VERB_SET_DESTINATION_GAIN,
464 				 i, 0);
465 	/* inform firmware about all updated matrix columns - output part */
466 	for (i = 0; i < chip->mixer.dest_phys_outs; i++)
467 		lola_codec_write(chip, chip->mixer.nid,
468 				 LOLA_VERB_SET_DESTINATION_GAIN,
469 				 chip->mixer.dest_phys_out_ofs + i, 0);
470 
471 	/* set all digital input source (master) gains to 0dB */
472 	for (i = 0; i < chip->mixer.src_phys_ins; i++)
473 		lola_mixer_set_src_gain(chip, i, 336, true); /* 0dB */
474 
475 	/* set all digital playback source (master) gains to 0dB */
476 	for (i = 0; i < chip->mixer.src_stream_outs; i++)
477 		lola_mixer_set_src_gain(chip,
478 					i + chip->mixer.src_stream_out_ofs,
479 					336, true); /* 0dB */
480 	/* set gain value 0dB diagonally in matrix - part INPUT -> CAPTURE */
481 	for (i = 0; i < chip->mixer.dest_stream_ins; i++) {
482 		int src = i % chip->mixer.src_phys_ins;
483 		lola_mixer_set_mapping_gain(chip, src, i, 336, true);
484 	}
485 	/* set gain value 0dB diagonally in matrix , part PLAYBACK -> OUTPUT
486 	 * (LoLa280 : playback channel 0,2,4,6 linked to output channel 0)
487 	 * (LoLa280 : playback channel 1,3,5,7 linked to output channel 1)
488 	 */
489 	for (i = 0; i < chip->mixer.src_stream_outs; i++) {
490 		int src = chip->mixer.src_stream_out_ofs + i;
491 		int dst = chip->mixer.dest_phys_out_ofs +
492 			i % chip->mixer.dest_phys_outs;
493 		lola_mixer_set_mapping_gain(chip, src, dst, 336, true);
494 	}
495 	return 0;
496 }
497 
498 /*
499  * analog mixer control element
500  */
501 static int lola_analog_vol_info(struct snd_kcontrol *kcontrol,
502 				struct snd_ctl_elem_info *uinfo)
503 {
504 	struct lola *chip = snd_kcontrol_chip(kcontrol);
505 	int dir = kcontrol->private_value;
506 
507 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
508 	uinfo->count = chip->pin[dir].num_pins;
509 	uinfo->value.integer.min = 0;
510 	uinfo->value.integer.max = chip->pin[dir].pins[0].amp_num_steps;
511 	return 0;
512 }
513 
514 static int lola_analog_vol_get(struct snd_kcontrol *kcontrol,
515 			       struct snd_ctl_elem_value *ucontrol)
516 {
517 	struct lola *chip = snd_kcontrol_chip(kcontrol);
518 	int dir = kcontrol->private_value;
519 	int i;
520 
521 	for (i = 0; i < chip->pin[dir].num_pins; i++)
522 		ucontrol->value.integer.value[i] =
523 			chip->pin[dir].pins[i].cur_gain_step;
524 	return 0;
525 }
526 
527 static int lola_analog_vol_put(struct snd_kcontrol *kcontrol,
528 			       struct snd_ctl_elem_value *ucontrol)
529 {
530 	struct lola *chip = snd_kcontrol_chip(kcontrol);
531 	int dir = kcontrol->private_value;
532 	int i, err;
533 
534 	for (i = 0; i < chip->pin[dir].num_pins; i++) {
535 		err = set_analog_volume(chip, dir, i,
536 					ucontrol->value.integer.value[i],
537 					true);
538 		if (err < 0)
539 			return err;
540 	}
541 	return 0;
542 }
543 
544 static int lola_analog_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
545 			       unsigned int size, unsigned int __user *tlv)
546 {
547 	struct lola *chip = snd_kcontrol_chip(kcontrol);
548 	int dir = kcontrol->private_value;
549 	unsigned int val1, val2;
550 	struct lola_pin *pin;
551 
552 	if (size < 4 * sizeof(unsigned int))
553 		return -ENOMEM;
554 	pin = &chip->pin[dir].pins[0];
555 
556 	val2 = pin->amp_step_size * 25;
557 	val1 = -1 * (int)pin->amp_offset * (int)val2;
558 #ifdef TLV_DB_SCALE_MUTE
559 	val2 |= TLV_DB_SCALE_MUTE;
560 #endif
561 	if (put_user(SNDRV_CTL_TLVT_DB_SCALE, tlv))
562 		return -EFAULT;
563 	if (put_user(2 * sizeof(unsigned int), tlv + 1))
564 		return -EFAULT;
565 	if (put_user(val1, tlv + 2))
566 		return -EFAULT;
567 	if (put_user(val2, tlv + 3))
568 		return -EFAULT;
569 	return 0;
570 }
571 
572 static struct snd_kcontrol_new lola_analog_mixer = {
573 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
574 	.access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
575 		   SNDRV_CTL_ELEM_ACCESS_TLV_READ |
576 		   SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
577 	.info = lola_analog_vol_info,
578 	.get = lola_analog_vol_get,
579 	.put = lola_analog_vol_put,
580 	.tlv.c = lola_analog_vol_tlv,
581 };
582 
583 static int create_analog_mixer(struct lola *chip, int dir, char *name)
584 {
585 	if (!chip->pin[dir].num_pins)
586 		return 0;
587 	/* no analog volumes on digital only adapters */
588 	if (chip->pin[dir].num_pins != chip->pin[dir].num_analog_pins)
589 		return 0;
590 	lola_analog_mixer.name = name;
591 	lola_analog_mixer.private_value = dir;
592 	return snd_ctl_add(chip->card,
593 			   snd_ctl_new1(&lola_analog_mixer, chip));
594 }
595 
596 /*
597  * Hardware sample rate converter on digital input
598  */
599 static int lola_input_src_info(struct snd_kcontrol *kcontrol,
600 			       struct snd_ctl_elem_info *uinfo)
601 {
602 	struct lola *chip = snd_kcontrol_chip(kcontrol);
603 
604 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
605 	uinfo->count = chip->pin[CAPT].num_pins;
606 	uinfo->value.integer.min = 0;
607 	uinfo->value.integer.max = 1;
608 	return 0;
609 }
610 
611 static int lola_input_src_get(struct snd_kcontrol *kcontrol,
612 			      struct snd_ctl_elem_value *ucontrol)
613 {
614 	struct lola *chip = snd_kcontrol_chip(kcontrol);
615 	int i;
616 
617 	for (i = 0; i < chip->pin[CAPT].num_pins; i++)
618 		ucontrol->value.integer.value[i] =
619 			!!(chip->input_src_mask & (1 << i));
620 	return 0;
621 }
622 
623 static int lola_input_src_put(struct snd_kcontrol *kcontrol,
624 			      struct snd_ctl_elem_value *ucontrol)
625 {
626 	struct lola *chip = snd_kcontrol_chip(kcontrol);
627 	int i;
628 	unsigned int mask;
629 
630 	mask = 0;
631 	for (i = 0; i < chip->pin[CAPT].num_pins; i++)
632 		if (ucontrol->value.integer.value[i])
633 			mask |= 1 << i;
634 	return lola_set_src_config(chip, mask, true);
635 }
636 
637 static const struct snd_kcontrol_new lola_input_src_mixer = {
638 	.name = "Digital SRC Capture Switch",
639 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
640 	.info = lola_input_src_info,
641 	.get = lola_input_src_get,
642 	.put = lola_input_src_put,
643 };
644 
645 /*
646  * Lola16161 or Lola881 can have Hardware sample rate converters
647  * on its digital input pins
648  */
649 static int create_input_src_mixer(struct lola *chip)
650 {
651 	if (!chip->input_src_caps_mask)
652 		return 0;
653 
654 	return snd_ctl_add(chip->card,
655 			   snd_ctl_new1(&lola_input_src_mixer, chip));
656 }
657 
658 /*
659  * src gain mixer
660  */
661 static int lola_src_gain_info(struct snd_kcontrol *kcontrol,
662 			      struct snd_ctl_elem_info *uinfo)
663 {
664 	unsigned int count = (kcontrol->private_value >> 8) & 0xff;
665 
666 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
667 	uinfo->count = count;
668 	uinfo->value.integer.min = 0;
669 	uinfo->value.integer.max = 409;
670 	return 0;
671 }
672 
673 static int lola_src_gain_get(struct snd_kcontrol *kcontrol,
674 			     struct snd_ctl_elem_value *ucontrol)
675 {
676 	struct lola *chip = snd_kcontrol_chip(kcontrol);
677 	unsigned int ofs = kcontrol->private_value & 0xff;
678 	unsigned int count = (kcontrol->private_value >> 8) & 0xff;
679 	unsigned int mask, i;
680 
681 	mask = readl(&chip->mixer.array->src_gain_enable);
682 	for (i = 0; i < count; i++) {
683 		unsigned int idx = ofs + i;
684 		unsigned short val;
685 		if (!(chip->mixer.src_mask & (1 << idx)))
686 			return -EINVAL;
687 		if (mask & (1 << idx))
688 			val = readw(&chip->mixer.array->src_gain[idx]) + 1;
689 		else
690 			val = 0;
691 		ucontrol->value.integer.value[i] = val;
692 	}
693 	return 0;
694 }
695 
696 static int lola_src_gain_put(struct snd_kcontrol *kcontrol,
697 			     struct snd_ctl_elem_value *ucontrol)
698 {
699 	struct lola *chip = snd_kcontrol_chip(kcontrol);
700 	unsigned int ofs = kcontrol->private_value & 0xff;
701 	unsigned int count = (kcontrol->private_value >> 8) & 0xff;
702 	int i, err;
703 
704 	for (i = 0; i < count; i++) {
705 		unsigned int idx = ofs + i;
706 		unsigned short val = ucontrol->value.integer.value[i];
707 		if (val)
708 			val--;
709 		err = lola_mixer_set_src_gain(chip, idx, val, !!val);
710 		if (err < 0)
711 			return err;
712 	}
713 	return 0;
714 }
715 
716 /* raw value: 0 = -84dB, 336 = 0dB, 408=18dB, incremented 1 for mute */
717 static const DECLARE_TLV_DB_SCALE(lola_src_gain_tlv, -8425, 25, 1);
718 
719 static struct snd_kcontrol_new lola_src_gain_mixer = {
720 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
721 	.access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
722 		   SNDRV_CTL_ELEM_ACCESS_TLV_READ),
723 	.info = lola_src_gain_info,
724 	.get = lola_src_gain_get,
725 	.put = lola_src_gain_put,
726 	.tlv.p = lola_src_gain_tlv,
727 };
728 
729 static int create_src_gain_mixer(struct lola *chip,
730 				 int num, int ofs, char *name)
731 {
732 	lola_src_gain_mixer.name = name;
733 	lola_src_gain_mixer.private_value = ofs + (num << 8);
734 	return snd_ctl_add(chip->card,
735 			   snd_ctl_new1(&lola_src_gain_mixer, chip));
736 }
737 
738 #if 0 /* not used */
739 /*
740  * destination gain (matrix-like) mixer
741  */
742 static int lola_dest_gain_info(struct snd_kcontrol *kcontrol,
743 			       struct snd_ctl_elem_info *uinfo)
744 {
745 	unsigned int src_num = (kcontrol->private_value >> 8) & 0xff;
746 
747 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
748 	uinfo->count = src_num;
749 	uinfo->value.integer.min = 0;
750 	uinfo->value.integer.max = 433;
751 	return 0;
752 }
753 
754 static int lola_dest_gain_get(struct snd_kcontrol *kcontrol,
755 			      struct snd_ctl_elem_value *ucontrol)
756 {
757 	struct lola *chip = snd_kcontrol_chip(kcontrol);
758 	unsigned int src_ofs = kcontrol->private_value & 0xff;
759 	unsigned int src_num = (kcontrol->private_value >> 8) & 0xff;
760 	unsigned int dst_ofs = (kcontrol->private_value >> 16) & 0xff;
761 	unsigned int dst, mask, i;
762 
763 	dst = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + dst_ofs;
764 	mask = readl(&chip->mixer.array->dest_mix_gain_enable[dst]);
765 	for (i = 0; i < src_num; i++) {
766 		unsigned int src = src_ofs + i;
767 		unsigned short val;
768 		if (!(chip->mixer.src_mask & (1 << src)))
769 			return -EINVAL;
770 		if (mask & (1 << dst))
771 			val = readw(&chip->mixer.array->dest_mix_gain[dst][src]) + 1;
772 		else
773 			val = 0;
774 		ucontrol->value.integer.value[i] = val;
775 	}
776 	return 0;
777 }
778 
779 static int lola_dest_gain_put(struct snd_kcontrol *kcontrol,
780 			      struct snd_ctl_elem_value *ucontrol)
781 {
782 	struct lola *chip = snd_kcontrol_chip(kcontrol);
783 	unsigned int src_ofs = kcontrol->private_value & 0xff;
784 	unsigned int src_num = (kcontrol->private_value >> 8) & 0xff;
785 	unsigned int dst_ofs = (kcontrol->private_value >> 16) & 0xff;
786 	unsigned int dst, mask;
787 	unsigned short gains[MAX_STREAM_COUNT];
788 	int i, num;
789 
790 	mask = 0;
791 	num = 0;
792 	for (i = 0; i < src_num; i++) {
793 		unsigned short val = ucontrol->value.integer.value[i];
794 		if (val) {
795 			gains[num++] = val - 1;
796 			mask |= 1 << i;
797 		}
798 	}
799 	mask <<= src_ofs;
800 	dst = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + dst_ofs;
801 	return lola_mixer_set_dest_gains(chip, dst, mask, gains);
802 }
803 
804 static const DECLARE_TLV_DB_SCALE(lola_dest_gain_tlv, -8425, 25, 1);
805 
806 static struct snd_kcontrol_new lola_dest_gain_mixer = {
807 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
808 	.access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
809 		   SNDRV_CTL_ELEM_ACCESS_TLV_READ),
810 	.info = lola_dest_gain_info,
811 	.get = lola_dest_gain_get,
812 	.put = lola_dest_gain_put,
813 	.tlv.p = lola_dest_gain_tlv,
814 };
815 
816 static int create_dest_gain_mixer(struct lola *chip,
817 				  int src_num, int src_ofs,
818 				  int num, int ofs, char *name)
819 {
820 	lola_dest_gain_mixer.count = num;
821 	lola_dest_gain_mixer.name = name;
822 	lola_dest_gain_mixer.private_value =
823 		src_ofs + (src_num << 8) + (ofs << 16) + (num << 24);
824 	return snd_ctl_add(chip->card,
825 			  snd_ctl_new1(&lola_dest_gain_mixer, chip));
826 }
827 #endif /* not used */
828 
829 /*
830  */
831 int lola_create_mixer(struct lola *chip)
832 {
833 	int err;
834 
835 	err = create_analog_mixer(chip, PLAY, "Analog Playback Volume");
836 	if (err < 0)
837 		return err;
838 	err = create_analog_mixer(chip, CAPT, "Analog Capture Volume");
839 	if (err < 0)
840 		return err;
841 	err = create_input_src_mixer(chip);
842 	if (err < 0)
843 		return err;
844 	err = create_src_gain_mixer(chip, chip->mixer.src_phys_ins, 0,
845 				    "Digital Capture Volume");
846 	if (err < 0)
847 		return err;
848 	err = create_src_gain_mixer(chip, chip->mixer.src_stream_outs,
849 				    chip->mixer.src_stream_out_ofs,
850 				    "Digital Playback Volume");
851 	if (err < 0)
852 		return err;
853 #if 0
854 /* FIXME: buggy mixer matrix handling */
855 	err = create_dest_gain_mixer(chip,
856 				     chip->mixer.src_phys_ins, 0,
857 				     chip->mixer.dest_stream_ins, 0,
858 				     "Line Capture Volume");
859 	if (err < 0)
860 		return err;
861 	err = create_dest_gain_mixer(chip,
862 				     chip->mixer.src_stream_outs,
863 				     chip->mixer.src_stream_out_ofs,
864 				     chip->mixer.dest_stream_ins, 0,
865 				     "Stream-Loopback Capture Volume");
866 	if (err < 0)
867 		return err;
868 	err = create_dest_gain_mixer(chip,
869 				     chip->mixer.src_phys_ins, 0,
870 				     chip->mixer.dest_phys_outs,
871 				     chip->mixer.dest_phys_out_ofs,
872 				     "Line-Loopback Playback Volume");
873 	if (err < 0)
874 		return err;
875 	err = create_dest_gain_mixer(chip,
876 				     chip->mixer.src_stream_outs,
877 				     chip->mixer.src_stream_out_ofs,
878 				     chip->mixer.dest_phys_outs,
879 				     chip->mixer.dest_phys_out_ofs,
880 				     "Stream Playback Volume");
881 	if (err < 0)
882 		return err;
883 #endif /* FIXME */
884 	return init_mixer_values(chip);
885 }
886