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