xref: /openbmc/linux/sound/pci/pcxhr/pcxhr_mixer.c (revision b34e08d5)
1 #define __NO_VERSION__
2 /*
3  * Driver for Digigram pcxhr compatible soundcards
4  *
5  * mixer callbacks
6  *
7  * Copyright (c) 2004 by Digigram <alsa@digigram.com>
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  */
23 
24 #include <linux/time.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/mutex.h>
28 #include <sound/core.h>
29 #include "pcxhr.h"
30 #include "pcxhr_hwdep.h"
31 #include "pcxhr_core.h"
32 #include <sound/control.h>
33 #include <sound/tlv.h>
34 #include <sound/asoundef.h>
35 #include "pcxhr_mixer.h"
36 #include "pcxhr_mix22.h"
37 
38 #define PCXHR_LINE_CAPTURE_LEVEL_MIN   0	/* -112.0 dB */
39 #define PCXHR_LINE_CAPTURE_LEVEL_MAX   255	/* +15.5 dB */
40 #define PCXHR_LINE_CAPTURE_ZERO_LEVEL  224	/* 0.0 dB ( 0 dBu -> 0 dBFS ) */
41 
42 #define PCXHR_LINE_PLAYBACK_LEVEL_MIN  0	/* -104.0 dB */
43 #define PCXHR_LINE_PLAYBACK_LEVEL_MAX  128	/* +24.0 dB */
44 #define PCXHR_LINE_PLAYBACK_ZERO_LEVEL 104	/* 0.0 dB ( 0 dBFS -> 0 dBu ) */
45 
46 static const DECLARE_TLV_DB_SCALE(db_scale_analog_capture, -11200, 50, 1550);
47 static const DECLARE_TLV_DB_SCALE(db_scale_analog_playback, -10400, 100, 2400);
48 
49 static const DECLARE_TLV_DB_SCALE(db_scale_a_hr222_capture, -11150, 50, 1600);
50 static const DECLARE_TLV_DB_SCALE(db_scale_a_hr222_playback, -2550, 50, 2400);
51 
52 static int pcxhr_update_analog_audio_level(struct snd_pcxhr *chip,
53 					   int is_capture, int channel)
54 {
55 	int err, vol;
56 	struct pcxhr_rmh rmh;
57 
58 	pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
59 	if (is_capture) {
60 		rmh.cmd[0] |= IO_NUM_REG_IN_ANA_LEVEL;
61 		rmh.cmd[2] = chip->analog_capture_volume[channel];
62 	} else {
63 		rmh.cmd[0] |= IO_NUM_REG_OUT_ANA_LEVEL;
64 		if (chip->analog_playback_active[channel])
65 			vol = chip->analog_playback_volume[channel];
66 		else
67 			vol = PCXHR_LINE_PLAYBACK_LEVEL_MIN;
68 		/* playback analog levels are inversed */
69 		rmh.cmd[2] = PCXHR_LINE_PLAYBACK_LEVEL_MAX - vol;
70 	}
71 	rmh.cmd[1]  = 1 << ((2 * chip->chip_idx) + channel);	/* audio mask */
72 	rmh.cmd_len = 3;
73 	err = pcxhr_send_msg(chip->mgr, &rmh);
74 	if (err < 0) {
75 		dev_dbg(chip->card->dev,
76 			"error update_analog_audio_level card(%d)"
77 			   " is_capture(%d) err(%x)\n",
78 			   chip->chip_idx, is_capture, err);
79 		return -EINVAL;
80 	}
81 	return 0;
82 }
83 
84 /*
85  * analog level control
86  */
87 static int pcxhr_analog_vol_info(struct snd_kcontrol *kcontrol,
88 				 struct snd_ctl_elem_info *uinfo)
89 {
90 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
91 
92 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
93 	uinfo->count = 2;
94 	if (kcontrol->private_value == 0) {	/* playback */
95 	    if (chip->mgr->is_hr_stereo) {
96 		uinfo->value.integer.min =
97 			HR222_LINE_PLAYBACK_LEVEL_MIN;	/* -25 dB */
98 		uinfo->value.integer.max =
99 			HR222_LINE_PLAYBACK_LEVEL_MAX;	/* +24 dB */
100 	    } else {
101 		uinfo->value.integer.min =
102 			PCXHR_LINE_PLAYBACK_LEVEL_MIN;	/*-104 dB */
103 		uinfo->value.integer.max =
104 			PCXHR_LINE_PLAYBACK_LEVEL_MAX;	/* +24 dB */
105 	    }
106 	} else {				/* capture */
107 	    if (chip->mgr->is_hr_stereo) {
108 		uinfo->value.integer.min =
109 			HR222_LINE_CAPTURE_LEVEL_MIN;	/*-112 dB */
110 		uinfo->value.integer.max =
111 			HR222_LINE_CAPTURE_LEVEL_MAX;	/* +15.5 dB */
112 	    } else {
113 		uinfo->value.integer.min =
114 			PCXHR_LINE_CAPTURE_LEVEL_MIN;	/*-112 dB */
115 		uinfo->value.integer.max =
116 			PCXHR_LINE_CAPTURE_LEVEL_MAX;	/* +15.5 dB */
117 	    }
118 	}
119 	return 0;
120 }
121 
122 static int pcxhr_analog_vol_get(struct snd_kcontrol *kcontrol,
123 				struct snd_ctl_elem_value *ucontrol)
124 {
125 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
126 	mutex_lock(&chip->mgr->mixer_mutex);
127 	if (kcontrol->private_value == 0) {	/* playback */
128 	  ucontrol->value.integer.value[0] = chip->analog_playback_volume[0];
129 	  ucontrol->value.integer.value[1] = chip->analog_playback_volume[1];
130 	} else {				/* capture */
131 	  ucontrol->value.integer.value[0] = chip->analog_capture_volume[0];
132 	  ucontrol->value.integer.value[1] = chip->analog_capture_volume[1];
133 	}
134 	mutex_unlock(&chip->mgr->mixer_mutex);
135 	return 0;
136 }
137 
138 static int pcxhr_analog_vol_put(struct snd_kcontrol *kcontrol,
139 				struct snd_ctl_elem_value *ucontrol)
140 {
141 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
142 	int changed = 0;
143 	int is_capture, i;
144 
145 	mutex_lock(&chip->mgr->mixer_mutex);
146 	is_capture = (kcontrol->private_value != 0);
147 	for (i = 0; i < 2; i++) {
148 		int  new_volume = ucontrol->value.integer.value[i];
149 		int *stored_volume = is_capture ?
150 			&chip->analog_capture_volume[i] :
151 			&chip->analog_playback_volume[i];
152 		if (is_capture) {
153 			if (chip->mgr->is_hr_stereo) {
154 				if (new_volume < HR222_LINE_CAPTURE_LEVEL_MIN ||
155 				    new_volume > HR222_LINE_CAPTURE_LEVEL_MAX)
156 					continue;
157 			} else {
158 				if (new_volume < PCXHR_LINE_CAPTURE_LEVEL_MIN ||
159 				    new_volume > PCXHR_LINE_CAPTURE_LEVEL_MAX)
160 					continue;
161 			}
162 		} else {
163 			if (chip->mgr->is_hr_stereo) {
164 				if (new_volume < HR222_LINE_PLAYBACK_LEVEL_MIN ||
165 				    new_volume > HR222_LINE_PLAYBACK_LEVEL_MAX)
166 					continue;
167 			} else {
168 				if (new_volume < PCXHR_LINE_PLAYBACK_LEVEL_MIN ||
169 				    new_volume > PCXHR_LINE_PLAYBACK_LEVEL_MAX)
170 					continue;
171 			}
172 		}
173 		if (*stored_volume != new_volume) {
174 			*stored_volume = new_volume;
175 			changed = 1;
176 			if (chip->mgr->is_hr_stereo)
177 				hr222_update_analog_audio_level(chip,
178 								is_capture, i);
179 			else
180 				pcxhr_update_analog_audio_level(chip,
181 								is_capture, i);
182 		}
183 	}
184 	mutex_unlock(&chip->mgr->mixer_mutex);
185 	return changed;
186 }
187 
188 static struct snd_kcontrol_new pcxhr_control_analog_level = {
189 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
190 	.access =	(SNDRV_CTL_ELEM_ACCESS_READWRITE |
191 			 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
192 	/* name will be filled later */
193 	.info =		pcxhr_analog_vol_info,
194 	.get =		pcxhr_analog_vol_get,
195 	.put =		pcxhr_analog_vol_put,
196 	/* tlv will be filled later */
197 };
198 
199 /* shared */
200 
201 #define pcxhr_sw_info		snd_ctl_boolean_stereo_info
202 
203 static int pcxhr_audio_sw_get(struct snd_kcontrol *kcontrol,
204 			      struct snd_ctl_elem_value *ucontrol)
205 {
206 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
207 
208 	mutex_lock(&chip->mgr->mixer_mutex);
209 	ucontrol->value.integer.value[0] = chip->analog_playback_active[0];
210 	ucontrol->value.integer.value[1] = chip->analog_playback_active[1];
211 	mutex_unlock(&chip->mgr->mixer_mutex);
212 	return 0;
213 }
214 
215 static int pcxhr_audio_sw_put(struct snd_kcontrol *kcontrol,
216 			      struct snd_ctl_elem_value *ucontrol)
217 {
218 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
219 	int i, changed = 0;
220 	mutex_lock(&chip->mgr->mixer_mutex);
221 	for(i = 0; i < 2; i++) {
222 		if (chip->analog_playback_active[i] !=
223 		    ucontrol->value.integer.value[i]) {
224 			chip->analog_playback_active[i] =
225 				!!ucontrol->value.integer.value[i];
226 			changed = 1;
227 			/* update playback levels */
228 			if (chip->mgr->is_hr_stereo)
229 				hr222_update_analog_audio_level(chip, 0, i);
230 			else
231 				pcxhr_update_analog_audio_level(chip, 0, i);
232 		}
233 	}
234 	mutex_unlock(&chip->mgr->mixer_mutex);
235 	return changed;
236 }
237 
238 static struct snd_kcontrol_new pcxhr_control_output_switch = {
239 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
240 	.name =		"Master Playback Switch",
241 	.info =		pcxhr_sw_info,		/* shared */
242 	.get =		pcxhr_audio_sw_get,
243 	.put =		pcxhr_audio_sw_put
244 };
245 
246 
247 #define PCXHR_DIGITAL_LEVEL_MIN		0x000	/* -110 dB */
248 #define PCXHR_DIGITAL_LEVEL_MAX		0x1ff	/* +18 dB */
249 #define PCXHR_DIGITAL_ZERO_LEVEL	0x1b7	/*  0 dB */
250 
251 static const DECLARE_TLV_DB_SCALE(db_scale_digital, -10975, 25, 1800);
252 
253 #define MORE_THAN_ONE_STREAM_LEVEL	0x000001
254 #define VALID_STREAM_PAN_LEVEL_MASK	0x800000
255 #define VALID_STREAM_LEVEL_MASK		0x400000
256 #define VALID_STREAM_LEVEL_1_MASK	0x200000
257 #define VALID_STREAM_LEVEL_2_MASK	0x100000
258 
259 static int pcxhr_update_playback_stream_level(struct snd_pcxhr* chip, int idx)
260 {
261 	int err;
262 	struct pcxhr_rmh rmh;
263 	struct pcxhr_pipe *pipe = &chip->playback_pipe;
264 	int left, right;
265 
266 	if (chip->digital_playback_active[idx][0])
267 		left = chip->digital_playback_volume[idx][0];
268 	else
269 		left = PCXHR_DIGITAL_LEVEL_MIN;
270 	if (chip->digital_playback_active[idx][1])
271 		right = chip->digital_playback_volume[idx][1];
272 	else
273 		right = PCXHR_DIGITAL_LEVEL_MIN;
274 
275 	pcxhr_init_rmh(&rmh, CMD_STREAM_OUT_LEVEL_ADJUST);
276 	/* add pipe and stream mask */
277 	pcxhr_set_pipe_cmd_params(&rmh, 0, pipe->first_audio, 0, 1<<idx);
278 	/* volume left->left / right->right panoramic level */
279 	rmh.cmd[0] |= MORE_THAN_ONE_STREAM_LEVEL;
280 	rmh.cmd[2]  = VALID_STREAM_PAN_LEVEL_MASK | VALID_STREAM_LEVEL_1_MASK;
281 	rmh.cmd[2] |= (left << 10);
282 	rmh.cmd[3]  = VALID_STREAM_PAN_LEVEL_MASK | VALID_STREAM_LEVEL_2_MASK;
283 	rmh.cmd[3] |= right;
284 	rmh.cmd_len = 4;
285 
286 	err = pcxhr_send_msg(chip->mgr, &rmh);
287 	if (err < 0) {
288 		dev_dbg(chip->card->dev, "error update_playback_stream_level "
289 			   "card(%d) err(%x)\n", chip->chip_idx, err);
290 		return -EINVAL;
291 	}
292 	return 0;
293 }
294 
295 #define AUDIO_IO_HAS_MUTE_LEVEL		0x400000
296 #define AUDIO_IO_HAS_MUTE_MONITOR_1	0x200000
297 #define VALID_AUDIO_IO_DIGITAL_LEVEL	0x000001
298 #define VALID_AUDIO_IO_MONITOR_LEVEL	0x000002
299 #define VALID_AUDIO_IO_MUTE_LEVEL	0x000004
300 #define VALID_AUDIO_IO_MUTE_MONITOR_1	0x000008
301 
302 static int pcxhr_update_audio_pipe_level(struct snd_pcxhr *chip,
303 					 int capture, int channel)
304 {
305 	int err;
306 	struct pcxhr_rmh rmh;
307 	struct pcxhr_pipe *pipe;
308 
309 	if (capture)
310 		pipe = &chip->capture_pipe[0];
311 	else
312 		pipe = &chip->playback_pipe;
313 
314 	pcxhr_init_rmh(&rmh, CMD_AUDIO_LEVEL_ADJUST);
315 	/* add channel mask */
316 	pcxhr_set_pipe_cmd_params(&rmh, capture, 0, 0,
317 				  1 << (channel + pipe->first_audio));
318 	/* TODO : if mask (3 << pipe->first_audio) is used, left and right
319 	 * channel will be programmed to the same params */
320 	if (capture) {
321 		rmh.cmd[0] |= VALID_AUDIO_IO_DIGITAL_LEVEL;
322 		/* VALID_AUDIO_IO_MUTE_LEVEL not yet handled
323 		 * (capture pipe level) */
324 		rmh.cmd[2] = chip->digital_capture_volume[channel];
325 	} else {
326 		rmh.cmd[0] |=	VALID_AUDIO_IO_MONITOR_LEVEL |
327 				VALID_AUDIO_IO_MUTE_MONITOR_1;
328 		/* VALID_AUDIO_IO_DIGITAL_LEVEL and VALID_AUDIO_IO_MUTE_LEVEL
329 		 * not yet handled (playback pipe level)
330 		 */
331 		rmh.cmd[2] = chip->monitoring_volume[channel] << 10;
332 		if (chip->monitoring_active[channel] == 0)
333 			rmh.cmd[2] |= AUDIO_IO_HAS_MUTE_MONITOR_1;
334 	}
335 	rmh.cmd_len = 3;
336 
337 	err = pcxhr_send_msg(chip->mgr, &rmh);
338 	if (err < 0) {
339 		dev_dbg(chip->card->dev,
340 			"error update_audio_level(%d) err=%x\n",
341 			   chip->chip_idx, err);
342 		return -EINVAL;
343 	}
344 	return 0;
345 }
346 
347 
348 /* shared */
349 static int pcxhr_digital_vol_info(struct snd_kcontrol *kcontrol,
350 				  struct snd_ctl_elem_info *uinfo)
351 {
352 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
353 	uinfo->count = 2;
354 	uinfo->value.integer.min = PCXHR_DIGITAL_LEVEL_MIN;   /* -109.5 dB */
355 	uinfo->value.integer.max = PCXHR_DIGITAL_LEVEL_MAX;   /*   18.0 dB */
356 	return 0;
357 }
358 
359 
360 static int pcxhr_pcm_vol_get(struct snd_kcontrol *kcontrol,
361 			     struct snd_ctl_elem_value *ucontrol)
362 {
363 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
364 	int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);	/* index */
365 	int *stored_volume;
366 	int is_capture = kcontrol->private_value;
367 
368 	mutex_lock(&chip->mgr->mixer_mutex);
369 	if (is_capture)		/* digital capture */
370 		stored_volume = chip->digital_capture_volume;
371 	else			/* digital playback */
372 		stored_volume = chip->digital_playback_volume[idx];
373 	ucontrol->value.integer.value[0] = stored_volume[0];
374 	ucontrol->value.integer.value[1] = stored_volume[1];
375 	mutex_unlock(&chip->mgr->mixer_mutex);
376 	return 0;
377 }
378 
379 static int pcxhr_pcm_vol_put(struct snd_kcontrol *kcontrol,
380 			     struct snd_ctl_elem_value *ucontrol)
381 {
382 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
383 	int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);	/* index */
384 	int changed = 0;
385 	int is_capture = kcontrol->private_value;
386 	int *stored_volume;
387 	int i;
388 
389 	mutex_lock(&chip->mgr->mixer_mutex);
390 	if (is_capture)		/* digital capture */
391 		stored_volume = chip->digital_capture_volume;
392 	else			/* digital playback */
393 		stored_volume = chip->digital_playback_volume[idx];
394 	for (i = 0; i < 2; i++) {
395 		int vol = ucontrol->value.integer.value[i];
396 		if (vol < PCXHR_DIGITAL_LEVEL_MIN ||
397 		    vol > PCXHR_DIGITAL_LEVEL_MAX)
398 			continue;
399 		if (stored_volume[i] != vol) {
400 			stored_volume[i] = vol;
401 			changed = 1;
402 			if (is_capture)	/* update capture volume */
403 				pcxhr_update_audio_pipe_level(chip, 1, i);
404 		}
405 	}
406 	if (!is_capture && changed)	/* update playback volume */
407 		pcxhr_update_playback_stream_level(chip, idx);
408 	mutex_unlock(&chip->mgr->mixer_mutex);
409 	return changed;
410 }
411 
412 static struct snd_kcontrol_new snd_pcxhr_pcm_vol =
413 {
414 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
415 	.access =	(SNDRV_CTL_ELEM_ACCESS_READWRITE |
416 			 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
417 	/* name will be filled later */
418 	/* count will be filled later */
419 	.info =		pcxhr_digital_vol_info,		/* shared */
420 	.get =		pcxhr_pcm_vol_get,
421 	.put =		pcxhr_pcm_vol_put,
422 	.tlv = { .p = db_scale_digital },
423 };
424 
425 
426 static int pcxhr_pcm_sw_get(struct snd_kcontrol *kcontrol,
427 			    struct snd_ctl_elem_value *ucontrol)
428 {
429 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
430 	int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
431 
432 	mutex_lock(&chip->mgr->mixer_mutex);
433 	ucontrol->value.integer.value[0] = chip->digital_playback_active[idx][0];
434 	ucontrol->value.integer.value[1] = chip->digital_playback_active[idx][1];
435 	mutex_unlock(&chip->mgr->mixer_mutex);
436 	return 0;
437 }
438 
439 static int pcxhr_pcm_sw_put(struct snd_kcontrol *kcontrol,
440 			    struct snd_ctl_elem_value *ucontrol)
441 {
442 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
443 	int changed = 0;
444 	int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
445 	int i, j;
446 
447 	mutex_lock(&chip->mgr->mixer_mutex);
448 	j = idx;
449 	for (i = 0; i < 2; i++) {
450 		if (chip->digital_playback_active[j][i] !=
451 		    ucontrol->value.integer.value[i]) {
452 			chip->digital_playback_active[j][i] =
453 				!!ucontrol->value.integer.value[i];
454 			changed = 1;
455 		}
456 	}
457 	if (changed)
458 		pcxhr_update_playback_stream_level(chip, idx);
459 	mutex_unlock(&chip->mgr->mixer_mutex);
460 	return changed;
461 }
462 
463 static struct snd_kcontrol_new pcxhr_control_pcm_switch = {
464 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
465 	.name =		"PCM Playback Switch",
466 	.count =	PCXHR_PLAYBACK_STREAMS,
467 	.info =		pcxhr_sw_info,		/* shared */
468 	.get =		pcxhr_pcm_sw_get,
469 	.put =		pcxhr_pcm_sw_put
470 };
471 
472 
473 /*
474  * monitoring level control
475  */
476 
477 static int pcxhr_monitor_vol_get(struct snd_kcontrol *kcontrol,
478 				 struct snd_ctl_elem_value *ucontrol)
479 {
480 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
481 	mutex_lock(&chip->mgr->mixer_mutex);
482 	ucontrol->value.integer.value[0] = chip->monitoring_volume[0];
483 	ucontrol->value.integer.value[1] = chip->monitoring_volume[1];
484 	mutex_unlock(&chip->mgr->mixer_mutex);
485 	return 0;
486 }
487 
488 static int pcxhr_monitor_vol_put(struct snd_kcontrol *kcontrol,
489 				 struct snd_ctl_elem_value *ucontrol)
490 {
491 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
492 	int changed = 0;
493 	int i;
494 
495 	mutex_lock(&chip->mgr->mixer_mutex);
496 	for (i = 0; i < 2; i++) {
497 		if (chip->monitoring_volume[i] !=
498 		    ucontrol->value.integer.value[i]) {
499 			chip->monitoring_volume[i] =
500 				ucontrol->value.integer.value[i];
501 			if (chip->monitoring_active[i])
502 				/* update monitoring volume and mute */
503 				/* do only when monitoring is unmuted */
504 				pcxhr_update_audio_pipe_level(chip, 0, i);
505 			changed = 1;
506 		}
507 	}
508 	mutex_unlock(&chip->mgr->mixer_mutex);
509 	return changed;
510 }
511 
512 static struct snd_kcontrol_new pcxhr_control_monitor_vol = {
513 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
514 	.access =	(SNDRV_CTL_ELEM_ACCESS_READWRITE |
515 			 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
516 	.name =         "Monitoring Playback Volume",
517 	.info =		pcxhr_digital_vol_info,		/* shared */
518 	.get =		pcxhr_monitor_vol_get,
519 	.put =		pcxhr_monitor_vol_put,
520 	.tlv = { .p = db_scale_digital },
521 };
522 
523 /*
524  * monitoring switch control
525  */
526 
527 static int pcxhr_monitor_sw_get(struct snd_kcontrol *kcontrol,
528 				struct snd_ctl_elem_value *ucontrol)
529 {
530 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
531 	mutex_lock(&chip->mgr->mixer_mutex);
532 	ucontrol->value.integer.value[0] = chip->monitoring_active[0];
533 	ucontrol->value.integer.value[1] = chip->monitoring_active[1];
534 	mutex_unlock(&chip->mgr->mixer_mutex);
535 	return 0;
536 }
537 
538 static int pcxhr_monitor_sw_put(struct snd_kcontrol *kcontrol,
539 				struct snd_ctl_elem_value *ucontrol)
540 {
541 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
542 	int changed = 0;
543 	int i;
544 
545 	mutex_lock(&chip->mgr->mixer_mutex);
546 	for (i = 0; i < 2; i++) {
547 		if (chip->monitoring_active[i] !=
548 		    ucontrol->value.integer.value[i]) {
549 			chip->monitoring_active[i] =
550 				!!ucontrol->value.integer.value[i];
551 			changed |= (1<<i); /* mask 0x01 and 0x02 */
552 		}
553 	}
554 	if (changed & 0x01)
555 		/* update left monitoring volume and mute */
556 		pcxhr_update_audio_pipe_level(chip, 0, 0);
557 	if (changed & 0x02)
558 		/* update right monitoring volume and mute */
559 		pcxhr_update_audio_pipe_level(chip, 0, 1);
560 
561 	mutex_unlock(&chip->mgr->mixer_mutex);
562 	return (changed != 0);
563 }
564 
565 static struct snd_kcontrol_new pcxhr_control_monitor_sw = {
566 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
567 	.name =         "Monitoring Playback Switch",
568 	.info =         pcxhr_sw_info,		/* shared */
569 	.get =          pcxhr_monitor_sw_get,
570 	.put =          pcxhr_monitor_sw_put
571 };
572 
573 
574 
575 /*
576  * audio source select
577  */
578 #define PCXHR_SOURCE_AUDIO01_UER	0x000100
579 #define PCXHR_SOURCE_AUDIO01_SYNC	0x000200
580 #define PCXHR_SOURCE_AUDIO23_UER	0x000400
581 #define PCXHR_SOURCE_AUDIO45_UER	0x001000
582 #define PCXHR_SOURCE_AUDIO67_UER	0x040000
583 
584 static int pcxhr_set_audio_source(struct snd_pcxhr* chip)
585 {
586 	struct pcxhr_rmh rmh;
587 	unsigned int mask, reg;
588 	unsigned int codec;
589 	int err, changed;
590 
591 	switch (chip->chip_idx) {
592 	case 0 : mask = PCXHR_SOURCE_AUDIO01_UER; codec = CS8420_01_CS; break;
593 	case 1 : mask = PCXHR_SOURCE_AUDIO23_UER; codec = CS8420_23_CS; break;
594 	case 2 : mask = PCXHR_SOURCE_AUDIO45_UER; codec = CS8420_45_CS; break;
595 	case 3 : mask = PCXHR_SOURCE_AUDIO67_UER; codec = CS8420_67_CS; break;
596 	default: return -EINVAL;
597 	}
598 	if (chip->audio_capture_source != 0) {
599 		reg = mask;	/* audio source from digital plug */
600 	} else {
601 		reg = 0;	/* audio source from analog plug */
602 	}
603 	/* set the input source */
604 	pcxhr_write_io_num_reg_cont(chip->mgr, mask, reg, &changed);
605 	/* resync them (otherwise channel inversion possible) */
606 	if (changed) {
607 		pcxhr_init_rmh(&rmh, CMD_RESYNC_AUDIO_INPUTS);
608 		rmh.cmd[0] |= (1 << chip->chip_idx);
609 		err = pcxhr_send_msg(chip->mgr, &rmh);
610 		if (err)
611 			return err;
612 	}
613 	if (chip->mgr->board_aes_in_192k) {
614 		int i;
615 		unsigned int src_config = 0xC0;
616 		/* update all src configs with one call */
617 		for (i = 0; (i < 4) && (i < chip->mgr->capture_chips); i++) {
618 			if (chip->mgr->chip[i]->audio_capture_source == 2)
619 				src_config |= (1 << (3 - i));
620 		}
621 		/* set codec SRC on off */
622 		pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
623 		rmh.cmd_len = 2;
624 		rmh.cmd[0] |= IO_NUM_REG_CONFIG_SRC;
625 		rmh.cmd[1] = src_config;
626 		err = pcxhr_send_msg(chip->mgr, &rmh);
627 	} else {
628 		int use_src = 0;
629 		if (chip->audio_capture_source == 2)
630 			use_src = 1;
631 		/* set codec SRC on off */
632 		pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
633 		rmh.cmd_len = 3;
634 		rmh.cmd[0] |= IO_NUM_UER_CHIP_REG;
635 		rmh.cmd[1] = codec;
636 		rmh.cmd[2] = ((CS8420_DATA_FLOW_CTL & CHIP_SIG_AND_MAP_SPI) |
637 			      (use_src ? 0x41 : 0x54));
638 		err = pcxhr_send_msg(chip->mgr, &rmh);
639 		if (err)
640 			return err;
641 		rmh.cmd[2] = ((CS8420_CLOCK_SRC_CTL & CHIP_SIG_AND_MAP_SPI) |
642 			      (use_src ? 0x41 : 0x49));
643 		err = pcxhr_send_msg(chip->mgr, &rmh);
644 	}
645 	return err;
646 }
647 
648 static int pcxhr_audio_src_info(struct snd_kcontrol *kcontrol,
649 				struct snd_ctl_elem_info *uinfo)
650 {
651 	static const char *texts[5] = {
652 		"Line", "Digital", "Digi+SRC", "Mic", "Line+Mic"
653 	};
654 	int i;
655 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
656 
657 	i = 2;			/* no SRC, no Mic available */
658 	if (chip->mgr->board_has_aes1) {
659 		i = 3;		/* SRC available */
660 		if (chip->mgr->board_has_mic)
661 			i = 5;	/* Mic and MicroMix available */
662 	}
663 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
664 	uinfo->count = 1;
665 	uinfo->value.enumerated.items = i;
666 	if (uinfo->value.enumerated.item > (i-1))
667 		uinfo->value.enumerated.item = i-1;
668 	strcpy(uinfo->value.enumerated.name,
669 		texts[uinfo->value.enumerated.item]);
670 	return 0;
671 }
672 
673 static int pcxhr_audio_src_get(struct snd_kcontrol *kcontrol,
674 			       struct snd_ctl_elem_value *ucontrol)
675 {
676 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
677 	ucontrol->value.enumerated.item[0] = chip->audio_capture_source;
678 	return 0;
679 }
680 
681 static int pcxhr_audio_src_put(struct snd_kcontrol *kcontrol,
682 			       struct snd_ctl_elem_value *ucontrol)
683 {
684 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
685 	int ret = 0;
686 	int i = 2;		/* no SRC, no Mic available */
687 	if (chip->mgr->board_has_aes1) {
688 		i = 3;		/* SRC available */
689 		if (chip->mgr->board_has_mic)
690 			i = 5;	/* Mic and MicroMix available */
691 	}
692 	if (ucontrol->value.enumerated.item[0] >= i)
693 		return -EINVAL;
694 	mutex_lock(&chip->mgr->mixer_mutex);
695 	if (chip->audio_capture_source != ucontrol->value.enumerated.item[0]) {
696 		chip->audio_capture_source = ucontrol->value.enumerated.item[0];
697 		if (chip->mgr->is_hr_stereo)
698 			hr222_set_audio_source(chip);
699 		else
700 			pcxhr_set_audio_source(chip);
701 		ret = 1;
702 	}
703 	mutex_unlock(&chip->mgr->mixer_mutex);
704 	return ret;
705 }
706 
707 static struct snd_kcontrol_new pcxhr_control_audio_src = {
708 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
709 	.name =		"Capture Source",
710 	.info =		pcxhr_audio_src_info,
711 	.get =		pcxhr_audio_src_get,
712 	.put =		pcxhr_audio_src_put,
713 };
714 
715 
716 /*
717  * clock type selection
718  * enum pcxhr_clock_type {
719  *	PCXHR_CLOCK_TYPE_INTERNAL = 0,
720  *	PCXHR_CLOCK_TYPE_WORD_CLOCK,
721  *	PCXHR_CLOCK_TYPE_AES_SYNC,
722  *	PCXHR_CLOCK_TYPE_AES_1,
723  *	PCXHR_CLOCK_TYPE_AES_2,
724  *	PCXHR_CLOCK_TYPE_AES_3,
725  *	PCXHR_CLOCK_TYPE_AES_4,
726  *	PCXHR_CLOCK_TYPE_MAX = PCXHR_CLOCK_TYPE_AES_4,
727  *	HR22_CLOCK_TYPE_INTERNAL = PCXHR_CLOCK_TYPE_INTERNAL,
728  *	HR22_CLOCK_TYPE_AES_SYNC,
729  *	HR22_CLOCK_TYPE_AES_1,
730  *	HR22_CLOCK_TYPE_MAX = HR22_CLOCK_TYPE_AES_1,
731  * };
732  */
733 
734 static int pcxhr_clock_type_info(struct snd_kcontrol *kcontrol,
735 				 struct snd_ctl_elem_info *uinfo)
736 {
737 	static const char *textsPCXHR[7] = {
738 		"Internal", "WordClock", "AES Sync",
739 		"AES 1", "AES 2", "AES 3", "AES 4"
740 	};
741 	static const char *textsHR22[3] = {
742 		"Internal", "AES Sync", "AES 1"
743 	};
744 	const char **texts;
745 	struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
746 	int clock_items = 2;	/* at least Internal and AES Sync clock */
747 	if (mgr->board_has_aes1) {
748 		clock_items += mgr->capture_chips;	/* add AES x */
749 		if (!mgr->is_hr_stereo)
750 			clock_items += 1;		/* add word clock */
751 	}
752 	if (mgr->is_hr_stereo) {
753 		texts = textsHR22;
754 		snd_BUG_ON(clock_items > (HR22_CLOCK_TYPE_MAX+1));
755 	} else {
756 		texts = textsPCXHR;
757 		snd_BUG_ON(clock_items > (PCXHR_CLOCK_TYPE_MAX+1));
758 	}
759 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
760 	uinfo->count = 1;
761 	uinfo->value.enumerated.items = clock_items;
762 	if (uinfo->value.enumerated.item >= clock_items)
763 		uinfo->value.enumerated.item = clock_items-1;
764 	strcpy(uinfo->value.enumerated.name,
765 		texts[uinfo->value.enumerated.item]);
766 	return 0;
767 }
768 
769 static int pcxhr_clock_type_get(struct snd_kcontrol *kcontrol,
770 				struct snd_ctl_elem_value *ucontrol)
771 {
772 	struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
773 	ucontrol->value.enumerated.item[0] = mgr->use_clock_type;
774 	return 0;
775 }
776 
777 static int pcxhr_clock_type_put(struct snd_kcontrol *kcontrol,
778 				struct snd_ctl_elem_value *ucontrol)
779 {
780 	struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
781 	int rate, ret = 0;
782 	unsigned int clock_items = 2; /* at least Internal and AES Sync clock */
783 	if (mgr->board_has_aes1) {
784 		clock_items += mgr->capture_chips;	/* add AES x */
785 		if (!mgr->is_hr_stereo)
786 			clock_items += 1;		/* add word clock */
787 	}
788 	if (ucontrol->value.enumerated.item[0] >= clock_items)
789 		return -EINVAL;
790 	mutex_lock(&mgr->mixer_mutex);
791 	if (mgr->use_clock_type != ucontrol->value.enumerated.item[0]) {
792 		mutex_lock(&mgr->setup_mutex);
793 		mgr->use_clock_type = ucontrol->value.enumerated.item[0];
794 		rate = 0;
795 		if (mgr->use_clock_type != PCXHR_CLOCK_TYPE_INTERNAL) {
796 			pcxhr_get_external_clock(mgr, mgr->use_clock_type,
797 						 &rate);
798 		} else {
799 			rate = mgr->sample_rate;
800 			if (!rate)
801 				rate = 48000;
802 		}
803 		if (rate) {
804 			pcxhr_set_clock(mgr, rate);
805 			if (mgr->sample_rate)
806 				mgr->sample_rate = rate;
807 		}
808 		mutex_unlock(&mgr->setup_mutex);
809 		ret = 1; /* return 1 even if the set was not done. ok ? */
810 	}
811 	mutex_unlock(&mgr->mixer_mutex);
812 	return ret;
813 }
814 
815 static struct snd_kcontrol_new pcxhr_control_clock_type = {
816 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
817 	.name =		"Clock Mode",
818 	.info =		pcxhr_clock_type_info,
819 	.get =		pcxhr_clock_type_get,
820 	.put =		pcxhr_clock_type_put,
821 };
822 
823 /*
824  * clock rate control
825  * specific control that scans the sample rates on the external plugs
826  */
827 static int pcxhr_clock_rate_info(struct snd_kcontrol *kcontrol,
828 				 struct snd_ctl_elem_info *uinfo)
829 {
830 	struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
831 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
832 	uinfo->count = 3 + mgr->capture_chips;
833 	uinfo->value.integer.min = 0;		/* clock not present */
834 	uinfo->value.integer.max = 192000;	/* max sample rate 192 kHz */
835 	return 0;
836 }
837 
838 static int pcxhr_clock_rate_get(struct snd_kcontrol *kcontrol,
839 				struct snd_ctl_elem_value *ucontrol)
840 {
841 	struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
842 	int i, err, rate;
843 
844 	mutex_lock(&mgr->mixer_mutex);
845 	for(i = 0; i < 3 + mgr->capture_chips; i++) {
846 		if (i == PCXHR_CLOCK_TYPE_INTERNAL)
847 			rate = mgr->sample_rate_real;
848 		else {
849 			err = pcxhr_get_external_clock(mgr, i, &rate);
850 			if (err)
851 				break;
852 		}
853 		ucontrol->value.integer.value[i] = rate;
854 	}
855 	mutex_unlock(&mgr->mixer_mutex);
856 	return 0;
857 }
858 
859 static struct snd_kcontrol_new pcxhr_control_clock_rate = {
860 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
861 	.iface =	SNDRV_CTL_ELEM_IFACE_CARD,
862 	.name =		"Clock Rates",
863 	.info =		pcxhr_clock_rate_info,
864 	.get =		pcxhr_clock_rate_get,
865 };
866 
867 /*
868  * IEC958 status bits
869  */
870 static int pcxhr_iec958_info(struct snd_kcontrol *kcontrol,
871 			     struct snd_ctl_elem_info *uinfo)
872 {
873 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
874 	uinfo->count = 1;
875 	return 0;
876 }
877 
878 static int pcxhr_iec958_capture_byte(struct snd_pcxhr *chip,
879 				     int aes_idx, unsigned char *aes_bits)
880 {
881 	int i, err;
882 	unsigned char temp;
883 	struct pcxhr_rmh rmh;
884 
885 	pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ);
886 	rmh.cmd[0] |= IO_NUM_UER_CHIP_REG;
887 	switch (chip->chip_idx) {
888 	  /* instead of CS8420_01_CS use CS8416_01_CS for AES SYNC plug */
889 	case 0:	rmh.cmd[1] = CS8420_01_CS; break;
890 	case 1:	rmh.cmd[1] = CS8420_23_CS; break;
891 	case 2:	rmh.cmd[1] = CS8420_45_CS; break;
892 	case 3:	rmh.cmd[1] = CS8420_67_CS; break;
893 	default: return -EINVAL;
894 	}
895 	if (chip->mgr->board_aes_in_192k) {
896 		switch (aes_idx) {
897 		case 0:	rmh.cmd[2] = CS8416_CSB0; break;
898 		case 1:	rmh.cmd[2] = CS8416_CSB1; break;
899 		case 2:	rmh.cmd[2] = CS8416_CSB2; break;
900 		case 3:	rmh.cmd[2] = CS8416_CSB3; break;
901 		case 4:	rmh.cmd[2] = CS8416_CSB4; break;
902 		default: return -EINVAL;
903 		}
904 	} else {
905 		switch (aes_idx) {
906 		  /* instead of CS8420_CSB0 use CS8416_CSBx for AES SYNC plug */
907 		case 0:	rmh.cmd[2] = CS8420_CSB0; break;
908 		case 1:	rmh.cmd[2] = CS8420_CSB1; break;
909 		case 2:	rmh.cmd[2] = CS8420_CSB2; break;
910 		case 3:	rmh.cmd[2] = CS8420_CSB3; break;
911 		case 4:	rmh.cmd[2] = CS8420_CSB4; break;
912 		default: return -EINVAL;
913 		}
914 	}
915 	/* size and code the chip id for the fpga */
916 	rmh.cmd[1] &= 0x0fffff;
917 	/* chip signature + map for spi read */
918 	rmh.cmd[2] &= CHIP_SIG_AND_MAP_SPI;
919 	rmh.cmd_len = 3;
920 	err = pcxhr_send_msg(chip->mgr, &rmh);
921 	if (err)
922 		return err;
923 
924 	if (chip->mgr->board_aes_in_192k) {
925 		temp = (unsigned char)rmh.stat[1];
926 	} else {
927 		temp = 0;
928 		/* reversed bit order (not with CS8416_01_CS) */
929 		for (i = 0; i < 8; i++) {
930 			temp <<= 1;
931 			if (rmh.stat[1] & (1 << i))
932 				temp |= 1;
933 		}
934 	}
935 	dev_dbg(chip->card->dev, "read iec958 AES %d byte %d = 0x%x\n",
936 		    chip->chip_idx, aes_idx, temp);
937 	*aes_bits = temp;
938 	return 0;
939 }
940 
941 static int pcxhr_iec958_get(struct snd_kcontrol *kcontrol,
942 			    struct snd_ctl_elem_value *ucontrol)
943 {
944 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
945 	unsigned char aes_bits;
946 	int i, err;
947 
948 	mutex_lock(&chip->mgr->mixer_mutex);
949 	for(i = 0; i < 5; i++) {
950 		if (kcontrol->private_value == 0)	/* playback */
951 			aes_bits = chip->aes_bits[i];
952 		else {				/* capture */
953 			if (chip->mgr->is_hr_stereo)
954 				err = hr222_iec958_capture_byte(chip, i,
955 								&aes_bits);
956 			else
957 				err = pcxhr_iec958_capture_byte(chip, i,
958 								&aes_bits);
959 			if (err)
960 				break;
961 		}
962 		ucontrol->value.iec958.status[i] = aes_bits;
963 	}
964 	mutex_unlock(&chip->mgr->mixer_mutex);
965         return 0;
966 }
967 
968 static int pcxhr_iec958_mask_get(struct snd_kcontrol *kcontrol,
969 				 struct snd_ctl_elem_value *ucontrol)
970 {
971 	int i;
972 	for (i = 0; i < 5; i++)
973 		ucontrol->value.iec958.status[i] = 0xff;
974         return 0;
975 }
976 
977 static int pcxhr_iec958_update_byte(struct snd_pcxhr *chip,
978 				    int aes_idx, unsigned char aes_bits)
979 {
980 	int i, err, cmd;
981 	unsigned char new_bits = aes_bits;
982 	unsigned char old_bits = chip->aes_bits[aes_idx];
983 	struct pcxhr_rmh rmh;
984 
985 	for (i = 0; i < 8; i++) {
986 		if ((old_bits & 0x01) != (new_bits & 0x01)) {
987 			cmd = chip->chip_idx & 0x03;      /* chip index 0..3 */
988 			if (chip->chip_idx > 3)
989 				/* new bit used if chip_idx>3 (PCX1222HR) */
990 				cmd |= 1 << 22;
991 			cmd |= ((aes_idx << 3) + i) << 2; /* add bit offset */
992 			cmd |= (new_bits & 0x01) << 23;   /* add bit value */
993 			pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
994 			rmh.cmd[0] |= IO_NUM_REG_CUER;
995 			rmh.cmd[1] = cmd;
996 			rmh.cmd_len = 2;
997 			dev_dbg(chip->card->dev,
998 				"write iec958 AES %d byte %d bit %d (cmd %x)\n",
999 				    chip->chip_idx, aes_idx, i, cmd);
1000 			err = pcxhr_send_msg(chip->mgr, &rmh);
1001 			if (err)
1002 				return err;
1003 		}
1004 		old_bits >>= 1;
1005 		new_bits >>= 1;
1006 	}
1007 	chip->aes_bits[aes_idx] = aes_bits;
1008 	return 0;
1009 }
1010 
1011 static int pcxhr_iec958_put(struct snd_kcontrol *kcontrol,
1012 			    struct snd_ctl_elem_value *ucontrol)
1013 {
1014 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
1015 	int i, changed = 0;
1016 
1017 	/* playback */
1018 	mutex_lock(&chip->mgr->mixer_mutex);
1019 	for (i = 0; i < 5; i++) {
1020 		if (ucontrol->value.iec958.status[i] != chip->aes_bits[i]) {
1021 			if (chip->mgr->is_hr_stereo)
1022 				hr222_iec958_update_byte(chip, i,
1023 					ucontrol->value.iec958.status[i]);
1024 			else
1025 				pcxhr_iec958_update_byte(chip, i,
1026 					ucontrol->value.iec958.status[i]);
1027 			changed = 1;
1028 		}
1029 	}
1030 	mutex_unlock(&chip->mgr->mixer_mutex);
1031 	return changed;
1032 }
1033 
1034 static struct snd_kcontrol_new pcxhr_control_playback_iec958_mask = {
1035 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1036 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1037 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1038 	.info =		pcxhr_iec958_info,
1039 	.get =		pcxhr_iec958_mask_get
1040 };
1041 static struct snd_kcontrol_new pcxhr_control_playback_iec958 = {
1042 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1043 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1044 	.info =         pcxhr_iec958_info,
1045 	.get =          pcxhr_iec958_get,
1046 	.put =          pcxhr_iec958_put,
1047 	.private_value = 0 /* playback */
1048 };
1049 
1050 static struct snd_kcontrol_new pcxhr_control_capture_iec958_mask = {
1051 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1052 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1053 	.name =		SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK),
1054 	.info =		pcxhr_iec958_info,
1055 	.get =		pcxhr_iec958_mask_get
1056 };
1057 static struct snd_kcontrol_new pcxhr_control_capture_iec958 = {
1058 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1059 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1060 	.name =         SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1061 	.info =         pcxhr_iec958_info,
1062 	.get =          pcxhr_iec958_get,
1063 	.private_value = 1 /* capture */
1064 };
1065 
1066 static void pcxhr_init_audio_levels(struct snd_pcxhr *chip)
1067 {
1068 	int i;
1069 
1070 	for (i = 0; i < 2; i++) {
1071 		if (chip->nb_streams_play) {
1072 			int j;
1073 			/* at boot time the digital volumes are unmuted 0dB */
1074 			for (j = 0; j < PCXHR_PLAYBACK_STREAMS; j++) {
1075 				chip->digital_playback_active[j][i] = 1;
1076 				chip->digital_playback_volume[j][i] =
1077 					PCXHR_DIGITAL_ZERO_LEVEL;
1078 			}
1079 			/* after boot, only two bits are set on the uer
1080 			 * interface
1081 			 */
1082 			chip->aes_bits[0] = (IEC958_AES0_PROFESSIONAL |
1083 					     IEC958_AES0_PRO_FS_48000);
1084 #ifdef CONFIG_SND_DEBUG
1085 			/* analog volumes for playback
1086 			 * (is LEVEL_MIN after boot)
1087 			 */
1088 			chip->analog_playback_active[i] = 1;
1089 			if (chip->mgr->is_hr_stereo)
1090 				chip->analog_playback_volume[i] =
1091 					HR222_LINE_PLAYBACK_ZERO_LEVEL;
1092 			else {
1093 				chip->analog_playback_volume[i] =
1094 					PCXHR_LINE_PLAYBACK_ZERO_LEVEL;
1095 				pcxhr_update_analog_audio_level(chip, 0, i);
1096 			}
1097 #endif
1098 			/* stereo cards need to be initialised after boot */
1099 			if (chip->mgr->is_hr_stereo)
1100 				hr222_update_analog_audio_level(chip, 0, i);
1101 		}
1102 		if (chip->nb_streams_capt) {
1103 			/* at boot time the digital volumes are unmuted 0dB */
1104 			chip->digital_capture_volume[i] =
1105 				PCXHR_DIGITAL_ZERO_LEVEL;
1106 			chip->analog_capture_active = 1;
1107 #ifdef CONFIG_SND_DEBUG
1108 			/* analog volumes for playback
1109 			 * (is LEVEL_MIN after boot)
1110 			 */
1111 			if (chip->mgr->is_hr_stereo)
1112 				chip->analog_capture_volume[i] =
1113 					HR222_LINE_CAPTURE_ZERO_LEVEL;
1114 			else {
1115 				chip->analog_capture_volume[i] =
1116 					PCXHR_LINE_CAPTURE_ZERO_LEVEL;
1117 				pcxhr_update_analog_audio_level(chip, 1, i);
1118 			}
1119 #endif
1120 			/* stereo cards need to be initialised after boot */
1121 			if (chip->mgr->is_hr_stereo)
1122 				hr222_update_analog_audio_level(chip, 1, i);
1123 		}
1124 	}
1125 
1126 	return;
1127 }
1128 
1129 
1130 int pcxhr_create_mixer(struct pcxhr_mgr *mgr)
1131 {
1132 	struct snd_pcxhr *chip;
1133 	int err, i;
1134 
1135 	mutex_init(&mgr->mixer_mutex); /* can be in another place */
1136 
1137 	for (i = 0; i < mgr->num_cards; i++) {
1138 		struct snd_kcontrol_new temp;
1139 		chip = mgr->chip[i];
1140 
1141 		if (chip->nb_streams_play) {
1142 			/* analog output level control */
1143 			temp = pcxhr_control_analog_level;
1144 			temp.name = "Master Playback Volume";
1145 			temp.private_value = 0; /* playback */
1146 			if (mgr->is_hr_stereo)
1147 				temp.tlv.p = db_scale_a_hr222_playback;
1148 			else
1149 				temp.tlv.p = db_scale_analog_playback;
1150 			err = snd_ctl_add(chip->card,
1151 					  snd_ctl_new1(&temp, chip));
1152 			if (err < 0)
1153 				return err;
1154 
1155 			/* output mute controls */
1156 			err = snd_ctl_add(chip->card,
1157 				snd_ctl_new1(&pcxhr_control_output_switch,
1158 					     chip));
1159 			if (err < 0)
1160 				return err;
1161 
1162 			temp = snd_pcxhr_pcm_vol;
1163 			temp.name = "PCM Playback Volume";
1164 			temp.count = PCXHR_PLAYBACK_STREAMS;
1165 			temp.private_value = 0; /* playback */
1166 			err = snd_ctl_add(chip->card,
1167 					  snd_ctl_new1(&temp, chip));
1168 			if (err < 0)
1169 				return err;
1170 
1171 			err = snd_ctl_add(chip->card,
1172 				snd_ctl_new1(&pcxhr_control_pcm_switch, chip));
1173 			if (err < 0)
1174 				return err;
1175 
1176 			/* IEC958 controls */
1177 			err = snd_ctl_add(chip->card,
1178 				snd_ctl_new1(&pcxhr_control_playback_iec958_mask,
1179 					     chip));
1180 			if (err < 0)
1181 				return err;
1182 
1183 			err = snd_ctl_add(chip->card,
1184 				snd_ctl_new1(&pcxhr_control_playback_iec958,
1185 					     chip));
1186 			if (err < 0)
1187 				return err;
1188 		}
1189 		if (chip->nb_streams_capt) {
1190 			/* analog input level control */
1191 			temp = pcxhr_control_analog_level;
1192 			temp.name = "Line Capture Volume";
1193 			temp.private_value = 1; /* capture */
1194 			if (mgr->is_hr_stereo)
1195 				temp.tlv.p = db_scale_a_hr222_capture;
1196 			else
1197 				temp.tlv.p = db_scale_analog_capture;
1198 
1199 			err = snd_ctl_add(chip->card,
1200 					  snd_ctl_new1(&temp, chip));
1201 			if (err < 0)
1202 				return err;
1203 
1204 			temp = snd_pcxhr_pcm_vol;
1205 			temp.name = "PCM Capture Volume";
1206 			temp.count = 1;
1207 			temp.private_value = 1; /* capture */
1208 
1209 			err = snd_ctl_add(chip->card,
1210 					  snd_ctl_new1(&temp, chip));
1211 			if (err < 0)
1212 				return err;
1213 
1214 			/* Audio source */
1215 			err = snd_ctl_add(chip->card,
1216 				snd_ctl_new1(&pcxhr_control_audio_src, chip));
1217 			if (err < 0)
1218 				return err;
1219 
1220 			/* IEC958 controls */
1221 			err = snd_ctl_add(chip->card,
1222 				snd_ctl_new1(&pcxhr_control_capture_iec958_mask,
1223 					     chip));
1224 			if (err < 0)
1225 				return err;
1226 
1227 			err = snd_ctl_add(chip->card,
1228 				snd_ctl_new1(&pcxhr_control_capture_iec958,
1229 					     chip));
1230 			if (err < 0)
1231 				return err;
1232 
1233 			if (mgr->is_hr_stereo) {
1234 				err = hr222_add_mic_controls(chip);
1235 				if (err < 0)
1236 					return err;
1237 			}
1238 		}
1239 		/* monitoring only if playback and capture device available */
1240 		if (chip->nb_streams_capt > 0 && chip->nb_streams_play > 0) {
1241 			/* monitoring */
1242 			err = snd_ctl_add(chip->card,
1243 				snd_ctl_new1(&pcxhr_control_monitor_vol, chip));
1244 			if (err < 0)
1245 				return err;
1246 
1247 			err = snd_ctl_add(chip->card,
1248 				snd_ctl_new1(&pcxhr_control_monitor_sw, chip));
1249 			if (err < 0)
1250 				return err;
1251 		}
1252 
1253 		if (i == 0) {
1254 			/* clock mode only one control per pcxhr */
1255 			err = snd_ctl_add(chip->card,
1256 				snd_ctl_new1(&pcxhr_control_clock_type, mgr));
1257 			if (err < 0)
1258 				return err;
1259 			/* non standard control used to scan
1260 			 * the external clock presence/frequencies
1261 			 */
1262 			err = snd_ctl_add(chip->card,
1263 				snd_ctl_new1(&pcxhr_control_clock_rate, mgr));
1264 			if (err < 0)
1265 				return err;
1266 		}
1267 
1268 		/* init values for the mixer data */
1269 		pcxhr_init_audio_levels(chip);
1270 	}
1271 
1272 	return 0;
1273 }
1274