xref: /linux/sound/pci/pcxhr/pcxhr_mixer.c (revision c537b994505099b7197e7d3125b942ecbcc51eb6)
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 <sound/driver.h>
25 #include <linux/time.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/mutex.h>
29 #include <sound/core.h>
30 #include "pcxhr.h"
31 #include "pcxhr_hwdep.h"
32 #include "pcxhr_core.h"
33 #include <sound/control.h>
34 #include <sound/tlv.h>
35 #include <sound/asoundef.h>
36 #include "pcxhr_mixer.h"
37 
38 
39 #define PCXHR_ANALOG_CAPTURE_LEVEL_MIN   0	/* -96.0 dB */
40 #define PCXHR_ANALOG_CAPTURE_LEVEL_MAX   255	/* +31.5 dB */
41 #define PCXHR_ANALOG_CAPTURE_ZERO_LEVEL  224	/* +16.0 dB ( +31.5 dB - fix level +15.5 dB ) */
42 
43 #define PCXHR_ANALOG_PLAYBACK_LEVEL_MIN  0	/* -128.0 dB */
44 #define PCXHR_ANALOG_PLAYBACK_LEVEL_MAX  128	/*    0.0 dB */
45 #define PCXHR_ANALOG_PLAYBACK_ZERO_LEVEL 104	/*  -24.0 dB ( 0.0 dB - fix level +24.0 dB ) */
46 
47 static const DECLARE_TLV_DB_SCALE(db_scale_analog_capture, -9600, 50, 0);
48 static const DECLARE_TLV_DB_SCALE(db_scale_analog_playback, -12800, 100, 0);
49 
50 static int pcxhr_update_analog_audio_level(struct snd_pcxhr *chip, int is_capture, int channel)
51 {
52 	int err, vol;
53 	struct pcxhr_rmh rmh;
54 
55 	pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
56 	if (is_capture) {
57 		rmh.cmd[0] |= IO_NUM_REG_IN_ANA_LEVEL;
58 		rmh.cmd[2] = chip->analog_capture_volume[channel];
59 	} else {
60 		rmh.cmd[0] |= IO_NUM_REG_OUT_ANA_LEVEL;
61 		if (chip->analog_playback_active[channel])
62 			vol = chip->analog_playback_volume[channel];
63 		else
64 			vol = PCXHR_ANALOG_PLAYBACK_LEVEL_MIN;
65 		rmh.cmd[2] = PCXHR_ANALOG_PLAYBACK_LEVEL_MAX - vol;	/* playback analog levels are inversed */
66 	}
67 	rmh.cmd[1]  = 1 << ((2 * chip->chip_idx) + channel);	/* audio mask */
68 	rmh.cmd_len = 3;
69 	err = pcxhr_send_msg(chip->mgr, &rmh);
70 	if (err < 0) {
71 		snd_printk(KERN_DEBUG "error update_analog_audio_level card(%d) "
72 			   "is_capture(%d) err(%x)\n", chip->chip_idx, is_capture, err);
73 		return -EINVAL;
74 	}
75 	return 0;
76 }
77 
78 /*
79  * analog level control
80  */
81 static int pcxhr_analog_vol_info(struct snd_kcontrol *kcontrol,
82 				 struct snd_ctl_elem_info *uinfo)
83 {
84 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
85 	uinfo->count = 2;
86 	if (kcontrol->private_value == 0) {	/* playback */
87 		uinfo->value.integer.min = PCXHR_ANALOG_PLAYBACK_LEVEL_MIN;	/* -128 dB */
88 		uinfo->value.integer.max = PCXHR_ANALOG_PLAYBACK_LEVEL_MAX;	/* 0 dB */
89 	} else {				/* capture */
90 		uinfo->value.integer.min = PCXHR_ANALOG_CAPTURE_LEVEL_MIN;	/* -96 dB */
91 		uinfo->value.integer.max = PCXHR_ANALOG_CAPTURE_LEVEL_MAX;	/* 31.5 dB */
92 	}
93 	return 0;
94 }
95 
96 static int pcxhr_analog_vol_get(struct snd_kcontrol *kcontrol,
97 				struct snd_ctl_elem_value *ucontrol)
98 {
99 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
100 	mutex_lock(&chip->mgr->mixer_mutex);
101 	if (kcontrol->private_value == 0) {	/* playback */
102 		ucontrol->value.integer.value[0] = chip->analog_playback_volume[0];
103 		ucontrol->value.integer.value[1] = chip->analog_playback_volume[1];
104 	} else {				/* capture */
105 		ucontrol->value.integer.value[0] = chip->analog_capture_volume[0];
106 		ucontrol->value.integer.value[1] = chip->analog_capture_volume[1];
107 	}
108 	mutex_unlock(&chip->mgr->mixer_mutex);
109 	return 0;
110 }
111 
112 static int pcxhr_analog_vol_put(struct snd_kcontrol *kcontrol,
113 				struct snd_ctl_elem_value *ucontrol)
114 {
115 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
116 	int changed = 0;
117 	int is_capture, i;
118 
119 	mutex_lock(&chip->mgr->mixer_mutex);
120 	is_capture = (kcontrol->private_value != 0);
121 	for (i = 0; i < 2; i++) {
122 		int  new_volume = ucontrol->value.integer.value[i];
123 		int* stored_volume = is_capture ? &chip->analog_capture_volume[i] :
124 			&chip->analog_playback_volume[i];
125 		if (*stored_volume != new_volume) {
126 			*stored_volume = new_volume;
127 			changed = 1;
128 			pcxhr_update_analog_audio_level(chip, is_capture, i);
129 		}
130 	}
131 	mutex_unlock(&chip->mgr->mixer_mutex);
132 	return changed;
133 }
134 
135 static struct snd_kcontrol_new pcxhr_control_analog_level = {
136 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
137 	.access =	(SNDRV_CTL_ELEM_ACCESS_READWRITE |
138 			 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
139 	/* name will be filled later */
140 	.info =		pcxhr_analog_vol_info,
141 	.get =		pcxhr_analog_vol_get,
142 	.put =		pcxhr_analog_vol_put,
143 	/* tlv will be filled later */
144 };
145 
146 /* shared */
147 static int pcxhr_sw_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
148 {
149 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
150 	uinfo->count = 2;
151 	uinfo->value.integer.min = 0;
152 	uinfo->value.integer.max = 1;
153 	return 0;
154 }
155 
156 static int pcxhr_audio_sw_get(struct snd_kcontrol *kcontrol,
157 			      struct snd_ctl_elem_value *ucontrol)
158 {
159 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
160 
161 	mutex_lock(&chip->mgr->mixer_mutex);
162 	ucontrol->value.integer.value[0] = chip->analog_playback_active[0];
163 	ucontrol->value.integer.value[1] = chip->analog_playback_active[1];
164 	mutex_unlock(&chip->mgr->mixer_mutex);
165 	return 0;
166 }
167 
168 static int pcxhr_audio_sw_put(struct snd_kcontrol *kcontrol,
169 			      struct snd_ctl_elem_value *ucontrol)
170 {
171 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
172 	int i, changed = 0;
173 	mutex_lock(&chip->mgr->mixer_mutex);
174 	for(i = 0; i < 2; i++) {
175 		if (chip->analog_playback_active[i] != ucontrol->value.integer.value[i]) {
176 			chip->analog_playback_active[i] = ucontrol->value.integer.value[i];
177 			changed = 1;
178 			pcxhr_update_analog_audio_level(chip, 0, i);	/* update playback levels */
179 		}
180 	}
181 	mutex_unlock(&chip->mgr->mixer_mutex);
182 	return changed;
183 }
184 
185 static struct snd_kcontrol_new pcxhr_control_output_switch = {
186 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
187 	.name =		"Master Playback Switch",
188 	.info =		pcxhr_sw_info,		/* shared */
189 	.get =		pcxhr_audio_sw_get,
190 	.put =		pcxhr_audio_sw_put
191 };
192 
193 
194 #define PCXHR_DIGITAL_LEVEL_MIN		0x000	/* -110 dB */
195 #define PCXHR_DIGITAL_LEVEL_MAX		0x1ff	/* +18 dB */
196 #define PCXHR_DIGITAL_ZERO_LEVEL	0x1b7	/*  0 dB */
197 
198 static const DECLARE_TLV_DB_SCALE(db_scale_digital, -10950, 50, 0);
199 
200 #define MORE_THAN_ONE_STREAM_LEVEL	0x000001
201 #define VALID_STREAM_PAN_LEVEL_MASK	0x800000
202 #define VALID_STREAM_LEVEL_MASK		0x400000
203 #define VALID_STREAM_LEVEL_1_MASK	0x200000
204 #define VALID_STREAM_LEVEL_2_MASK	0x100000
205 
206 static int pcxhr_update_playback_stream_level(struct snd_pcxhr* chip, int idx)
207 {
208 	int err;
209 	struct pcxhr_rmh rmh;
210 	struct pcxhr_pipe *pipe = &chip->playback_pipe;
211 	int left, right;
212 
213 	if (chip->digital_playback_active[idx][0])
214 		left = chip->digital_playback_volume[idx][0];
215 	else
216 		left = PCXHR_DIGITAL_LEVEL_MIN;
217 	if (chip->digital_playback_active[idx][1])
218 		right = chip->digital_playback_volume[idx][1];
219 	else
220 		right = PCXHR_DIGITAL_LEVEL_MIN;
221 
222 	pcxhr_init_rmh(&rmh, CMD_STREAM_OUT_LEVEL_ADJUST);
223 	/* add pipe and stream mask */
224 	pcxhr_set_pipe_cmd_params(&rmh, 0, pipe->first_audio, 0, 1<<idx);
225 	/* volume left->left / right->right panoramic level */
226 	rmh.cmd[0] |= MORE_THAN_ONE_STREAM_LEVEL;
227 	rmh.cmd[2]  = VALID_STREAM_PAN_LEVEL_MASK | VALID_STREAM_LEVEL_1_MASK;
228 	rmh.cmd[2] |= (left << 10);
229 	rmh.cmd[3]  = VALID_STREAM_PAN_LEVEL_MASK | VALID_STREAM_LEVEL_2_MASK;
230 	rmh.cmd[3] |= right;
231 	rmh.cmd_len = 4;
232 
233 	err = pcxhr_send_msg(chip->mgr, &rmh);
234 	if (err < 0) {
235 		snd_printk(KERN_DEBUG "error update_playback_stream_level "
236 			   "card(%d) err(%x)\n", chip->chip_idx, err);
237 		return -EINVAL;
238 	}
239 	return 0;
240 }
241 
242 #define AUDIO_IO_HAS_MUTE_LEVEL		0x400000
243 #define AUDIO_IO_HAS_MUTE_MONITOR_1	0x200000
244 #define VALID_AUDIO_IO_DIGITAL_LEVEL	0x000001
245 #define VALID_AUDIO_IO_MONITOR_LEVEL	0x000002
246 #define VALID_AUDIO_IO_MUTE_LEVEL	0x000004
247 #define VALID_AUDIO_IO_MUTE_MONITOR_1	0x000008
248 
249 static int pcxhr_update_audio_pipe_level(struct snd_pcxhr* chip, int capture, int channel)
250 {
251 	int err;
252 	struct pcxhr_rmh rmh;
253 	struct pcxhr_pipe *pipe;
254 
255 	if (capture)
256 		pipe = &chip->capture_pipe[0];
257 	else
258 		pipe = &chip->playback_pipe;
259 
260 	pcxhr_init_rmh(&rmh, CMD_AUDIO_LEVEL_ADJUST);
261 	/* add channel mask */
262 	pcxhr_set_pipe_cmd_params(&rmh, capture, 0, 0, 1 << (channel + pipe->first_audio));
263 	/* TODO : if mask (3 << pipe->first_audio) is used, left and right channel
264 	 * will be programmed to the same params
265 	 */
266 	if (capture) {
267 		rmh.cmd[0] |= VALID_AUDIO_IO_DIGITAL_LEVEL;
268 		/* VALID_AUDIO_IO_MUTE_LEVEL not yet handled (capture pipe level) */
269 		rmh.cmd[2] = chip->digital_capture_volume[channel];
270 	} else {
271 		rmh.cmd[0] |= VALID_AUDIO_IO_MONITOR_LEVEL | VALID_AUDIO_IO_MUTE_MONITOR_1;
272 		/* VALID_AUDIO_IO_DIGITAL_LEVEL and VALID_AUDIO_IO_MUTE_LEVEL not yet
273 		 * handled (playback pipe level)
274 		 */
275 		rmh.cmd[2] = chip->monitoring_volume[channel] << 10;
276 		if (chip->monitoring_active[channel] == 0)
277 			rmh.cmd[2] |= AUDIO_IO_HAS_MUTE_MONITOR_1;
278 	}
279 	rmh.cmd_len = 3;
280 
281 	err = pcxhr_send_msg(chip->mgr, &rmh);
282 	if(err<0) {
283 		snd_printk(KERN_DEBUG "error update_audio_level card(%d) err(%x)\n",
284 			   chip->chip_idx, err);
285 		return -EINVAL;
286 	}
287 	return 0;
288 }
289 
290 
291 /* shared */
292 static int pcxhr_digital_vol_info(struct snd_kcontrol *kcontrol,
293 				  struct snd_ctl_elem_info *uinfo)
294 {
295 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
296 	uinfo->count = 2;
297 	uinfo->value.integer.min = PCXHR_DIGITAL_LEVEL_MIN;   /* -109.5 dB */
298 	uinfo->value.integer.max = PCXHR_DIGITAL_LEVEL_MAX;   /*   18.0 dB */
299 	return 0;
300 }
301 
302 
303 static int pcxhr_pcm_vol_get(struct snd_kcontrol *kcontrol,
304 			     struct snd_ctl_elem_value *ucontrol)
305 {
306 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
307 	int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);		/* index */
308 	int *stored_volume;
309 	int is_capture = kcontrol->private_value;
310 
311 	mutex_lock(&chip->mgr->mixer_mutex);
312 	if (is_capture)
313 		stored_volume = chip->digital_capture_volume;		/* digital capture */
314 	else
315 		stored_volume = chip->digital_playback_volume[idx];	/* digital playback */
316 	ucontrol->value.integer.value[0] = stored_volume[0];
317 	ucontrol->value.integer.value[1] = stored_volume[1];
318 	mutex_unlock(&chip->mgr->mixer_mutex);
319 	return 0;
320 }
321 
322 static int pcxhr_pcm_vol_put(struct snd_kcontrol *kcontrol,
323 			     struct snd_ctl_elem_value *ucontrol)
324 {
325 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
326 	int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);		/* index */
327 	int changed = 0;
328 	int is_capture = kcontrol->private_value;
329 	int *stored_volume;
330 	int i;
331 
332 	mutex_lock(&chip->mgr->mixer_mutex);
333 	if (is_capture)
334 		stored_volume = chip->digital_capture_volume;		/* digital capture */
335 	else
336 		stored_volume = chip->digital_playback_volume[idx];	/* digital playback */
337 	for (i = 0; i < 2; i++) {
338 		if (stored_volume[i] != ucontrol->value.integer.value[i]) {
339 			stored_volume[i] = ucontrol->value.integer.value[i];
340 			changed = 1;
341 			if (is_capture)	/* update capture volume */
342 				pcxhr_update_audio_pipe_level(chip, 1, i);
343 		}
344 	}
345 	if (! is_capture && changed)
346 		pcxhr_update_playback_stream_level(chip, idx);	/* update playback volume */
347 	mutex_unlock(&chip->mgr->mixer_mutex);
348 	return changed;
349 }
350 
351 static struct snd_kcontrol_new snd_pcxhr_pcm_vol =
352 {
353 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
354 	.access =	(SNDRV_CTL_ELEM_ACCESS_READWRITE |
355 			 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
356 	/* name will be filled later */
357 	/* count will be filled later */
358 	.info =		pcxhr_digital_vol_info,		/* shared */
359 	.get =		pcxhr_pcm_vol_get,
360 	.put =		pcxhr_pcm_vol_put,
361 	.tlv = { .p = db_scale_digital },
362 };
363 
364 
365 static int pcxhr_pcm_sw_get(struct snd_kcontrol *kcontrol,
366 			    struct snd_ctl_elem_value *ucontrol)
367 {
368 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
369 	int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
370 
371 	mutex_lock(&chip->mgr->mixer_mutex);
372 	ucontrol->value.integer.value[0] = chip->digital_playback_active[idx][0];
373 	ucontrol->value.integer.value[1] = chip->digital_playback_active[idx][1];
374 	mutex_unlock(&chip->mgr->mixer_mutex);
375 	return 0;
376 }
377 
378 static int pcxhr_pcm_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
379 {
380 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
381 	int changed = 0;
382 	int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
383 	int i, j;
384 
385 	mutex_lock(&chip->mgr->mixer_mutex);
386 	j = idx;
387 	for (i = 0; i < 2; i++) {
388 		if (chip->digital_playback_active[j][i] != ucontrol->value.integer.value[i]) {
389 			chip->digital_playback_active[j][i] = ucontrol->value.integer.value[i];
390 			changed = 1;
391 		}
392 	}
393 	if (changed)
394 		pcxhr_update_playback_stream_level(chip, idx);
395 	mutex_unlock(&chip->mgr->mixer_mutex);
396 	return changed;
397 }
398 
399 static struct snd_kcontrol_new pcxhr_control_pcm_switch = {
400 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
401 	.name =		"PCM Playback Switch",
402 	.count =	PCXHR_PLAYBACK_STREAMS,
403 	.info =		pcxhr_sw_info,		/* shared */
404 	.get =		pcxhr_pcm_sw_get,
405 	.put =		pcxhr_pcm_sw_put
406 };
407 
408 
409 /*
410  * monitoring level control
411  */
412 
413 static int pcxhr_monitor_vol_get(struct snd_kcontrol *kcontrol,
414 				 struct snd_ctl_elem_value *ucontrol)
415 {
416 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
417 	mutex_lock(&chip->mgr->mixer_mutex);
418 	ucontrol->value.integer.value[0] = chip->monitoring_volume[0];
419 	ucontrol->value.integer.value[1] = chip->monitoring_volume[1];
420 	mutex_unlock(&chip->mgr->mixer_mutex);
421 	return 0;
422 }
423 
424 static int pcxhr_monitor_vol_put(struct snd_kcontrol *kcontrol,
425 				 struct snd_ctl_elem_value *ucontrol)
426 {
427 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
428 	int changed = 0;
429 	int i;
430 
431 	mutex_lock(&chip->mgr->mixer_mutex);
432 	for (i = 0; i < 2; i++) {
433 		if (chip->monitoring_volume[i] != ucontrol->value.integer.value[i]) {
434 			chip->monitoring_volume[i] = ucontrol->value.integer.value[i];
435 			if(chip->monitoring_active[i])	/* do only when monitoring is unmuted */
436 				/* update monitoring volume and mute */
437 				pcxhr_update_audio_pipe_level(chip, 0, i);
438 			changed = 1;
439 		}
440 	}
441 	mutex_unlock(&chip->mgr->mixer_mutex);
442 	return changed;
443 }
444 
445 static struct snd_kcontrol_new pcxhr_control_monitor_vol = {
446 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
447 	.access =	(SNDRV_CTL_ELEM_ACCESS_READWRITE |
448 			 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
449 	.name =         "Monitoring Volume",
450 	.info =		pcxhr_digital_vol_info,		/* shared */
451 	.get =		pcxhr_monitor_vol_get,
452 	.put =		pcxhr_monitor_vol_put,
453 	.tlv = { .p = db_scale_digital },
454 };
455 
456 /*
457  * monitoring switch control
458  */
459 
460 static int pcxhr_monitor_sw_get(struct snd_kcontrol *kcontrol,
461 				struct snd_ctl_elem_value *ucontrol)
462 {
463 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
464 	mutex_lock(&chip->mgr->mixer_mutex);
465 	ucontrol->value.integer.value[0] = chip->monitoring_active[0];
466 	ucontrol->value.integer.value[1] = chip->monitoring_active[1];
467 	mutex_unlock(&chip->mgr->mixer_mutex);
468 	return 0;
469 }
470 
471 static int pcxhr_monitor_sw_put(struct snd_kcontrol *kcontrol,
472 				struct snd_ctl_elem_value *ucontrol)
473 {
474 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
475 	int changed = 0;
476 	int i;
477 
478 	mutex_lock(&chip->mgr->mixer_mutex);
479 	for (i = 0; i < 2; i++) {
480 		if (chip->monitoring_active[i] != ucontrol->value.integer.value[i]) {
481 			chip->monitoring_active[i] = ucontrol->value.integer.value[i];
482 			changed |= (1<<i); /* mask 0x01 and 0x02 */
483 		}
484 	}
485 	if(changed & 0x01)
486 		/* update left monitoring volume and mute */
487 		pcxhr_update_audio_pipe_level(chip, 0, 0);
488 	if(changed & 0x02)
489 		/* update right monitoring volume and mute */
490 		pcxhr_update_audio_pipe_level(chip, 0, 1);
491 
492 	mutex_unlock(&chip->mgr->mixer_mutex);
493 	return (changed != 0);
494 }
495 
496 static struct snd_kcontrol_new pcxhr_control_monitor_sw = {
497 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
498 	.name =         "Monitoring Switch",
499 	.info =         pcxhr_sw_info,		/* shared */
500 	.get =          pcxhr_monitor_sw_get,
501 	.put =          pcxhr_monitor_sw_put
502 };
503 
504 
505 
506 /*
507  * audio source select
508  */
509 #define PCXHR_SOURCE_AUDIO01_UER	0x000100
510 #define PCXHR_SOURCE_AUDIO01_SYNC	0x000200
511 #define PCXHR_SOURCE_AUDIO23_UER	0x000400
512 #define PCXHR_SOURCE_AUDIO45_UER	0x001000
513 #define PCXHR_SOURCE_AUDIO67_UER	0x040000
514 
515 static int pcxhr_set_audio_source(struct snd_pcxhr* chip)
516 {
517 	struct pcxhr_rmh rmh;
518 	unsigned int mask, reg;
519 	unsigned int codec;
520 	int err, use_src, changed;
521 
522 	switch (chip->chip_idx) {
523 	case 0 : mask = PCXHR_SOURCE_AUDIO01_UER; codec = CS8420_01_CS; break;
524 	case 1 : mask = PCXHR_SOURCE_AUDIO23_UER; codec = CS8420_23_CS; break;
525 	case 2 : mask = PCXHR_SOURCE_AUDIO45_UER; codec = CS8420_45_CS; break;
526 	case 3 : mask = PCXHR_SOURCE_AUDIO67_UER; codec = CS8420_67_CS; break;
527 	default: return -EINVAL;
528 	}
529 	reg = 0;	/* audio source from analog plug */
530 	use_src = 0;	/* do not activate codec SRC */
531 
532 	if (chip->audio_capture_source != 0) {
533 		reg = mask;	/* audio source from digital plug */
534 		if (chip->audio_capture_source == 2)
535 			use_src = 1;
536 	}
537 	/* set the input source */
538 	pcxhr_write_io_num_reg_cont(chip->mgr, mask, reg, &changed);
539 	/* resync them (otherwise channel inversion possible) */
540 	if (changed) {
541 		pcxhr_init_rmh(&rmh, CMD_RESYNC_AUDIO_INPUTS);
542 		rmh.cmd[0] |= (1 << chip->chip_idx);
543 		err = pcxhr_send_msg(chip->mgr, &rmh);
544 		if (err)
545 			return err;
546 	}
547 	pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);	/* set codec SRC on off */
548 	rmh.cmd_len = 3;
549 	rmh.cmd[0] |= IO_NUM_UER_CHIP_REG;
550 	rmh.cmd[1] = codec;
551 	rmh.cmd[2] = (CS8420_DATA_FLOW_CTL & CHIP_SIG_AND_MAP_SPI) | (use_src ? 0x41 : 0x54);
552 	err = pcxhr_send_msg(chip->mgr, &rmh);
553 	if(err)
554 		return err;
555 	rmh.cmd[2] = (CS8420_CLOCK_SRC_CTL & CHIP_SIG_AND_MAP_SPI) | (use_src ? 0x41 : 0x49);
556 	err = pcxhr_send_msg(chip->mgr, &rmh);
557 	return err;
558 }
559 
560 static int pcxhr_audio_src_info(struct snd_kcontrol *kcontrol,
561 				struct snd_ctl_elem_info *uinfo)
562 {
563 	static char *texts[3] = {"Analog", "Digital", "Digi+SRC"};
564 
565 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
566 	uinfo->count = 1;
567 	uinfo->value.enumerated.items = 3;
568 	if (uinfo->value.enumerated.item > 2)
569 		uinfo->value.enumerated.item = 2;
570 	strcpy(uinfo->value.enumerated.name,
571 		texts[uinfo->value.enumerated.item]);
572 	return 0;
573 }
574 
575 static int pcxhr_audio_src_get(struct snd_kcontrol *kcontrol,
576 			       struct snd_ctl_elem_value *ucontrol)
577 {
578 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
579 	ucontrol->value.enumerated.item[0] = chip->audio_capture_source;
580 	return 0;
581 }
582 
583 static int pcxhr_audio_src_put(struct snd_kcontrol *kcontrol,
584 			       struct snd_ctl_elem_value *ucontrol)
585 {
586 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
587 	int ret = 0;
588 
589 	mutex_lock(&chip->mgr->mixer_mutex);
590 	if (chip->audio_capture_source != ucontrol->value.enumerated.item[0]) {
591 		chip->audio_capture_source = ucontrol->value.enumerated.item[0];
592 		pcxhr_set_audio_source(chip);
593 		ret = 1;
594 	}
595 	mutex_unlock(&chip->mgr->mixer_mutex);
596 	return ret;
597 }
598 
599 static struct snd_kcontrol_new pcxhr_control_audio_src = {
600 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
601 	.name =		"Capture Source",
602 	.info =		pcxhr_audio_src_info,
603 	.get =		pcxhr_audio_src_get,
604 	.put =		pcxhr_audio_src_put,
605 };
606 
607 
608 /*
609  * clock type selection
610  * enum pcxhr_clock_type {
611  *		PCXHR_CLOCK_TYPE_INTERNAL = 0,
612  *		PCXHR_CLOCK_TYPE_WORD_CLOCK,
613  *		PCXHR_CLOCK_TYPE_AES_SYNC,
614  *		PCXHR_CLOCK_TYPE_AES_1,
615  *		PCXHR_CLOCK_TYPE_AES_2,
616  *		PCXHR_CLOCK_TYPE_AES_3,
617  *		PCXHR_CLOCK_TYPE_AES_4,
618  *	};
619  */
620 
621 static int pcxhr_clock_type_info(struct snd_kcontrol *kcontrol,
622 				 struct snd_ctl_elem_info *uinfo)
623 {
624 	static char *texts[7] = {
625 		"Internal", "WordClock", "AES Sync", "AES 1", "AES 2", "AES 3", "AES 4"
626 	};
627 	struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
628 	int clock_items = 3 + mgr->capture_chips;
629 
630 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
631 	uinfo->count = 1;
632 	uinfo->value.enumerated.items = clock_items;
633 	if (uinfo->value.enumerated.item >= clock_items)
634 		uinfo->value.enumerated.item = clock_items-1;
635 	strcpy(uinfo->value.enumerated.name,
636 		texts[uinfo->value.enumerated.item]);
637 	return 0;
638 }
639 
640 static int pcxhr_clock_type_get(struct snd_kcontrol *kcontrol,
641 				struct snd_ctl_elem_value *ucontrol)
642 {
643 	struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
644 	ucontrol->value.enumerated.item[0] = mgr->use_clock_type;
645 	return 0;
646 }
647 
648 static int pcxhr_clock_type_put(struct snd_kcontrol *kcontrol,
649 				struct snd_ctl_elem_value *ucontrol)
650 {
651 	struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
652 	int rate, ret = 0;
653 
654 	mutex_lock(&mgr->mixer_mutex);
655 	if (mgr->use_clock_type != ucontrol->value.enumerated.item[0]) {
656 		mutex_lock(&mgr->setup_mutex);
657 		mgr->use_clock_type = ucontrol->value.enumerated.item[0];
658 		if (mgr->use_clock_type)
659 			pcxhr_get_external_clock(mgr, mgr->use_clock_type, &rate);
660 		else
661 			rate = mgr->sample_rate;
662 		if (rate) {
663 			pcxhr_set_clock(mgr, rate);
664 			if (mgr->sample_rate)
665 				mgr->sample_rate = rate;
666 		}
667 		mutex_unlock(&mgr->setup_mutex);
668 		ret = 1;	/* return 1 even if the set was not done. ok ? */
669 	}
670 	mutex_unlock(&mgr->mixer_mutex);
671 	return ret;
672 }
673 
674 static struct snd_kcontrol_new pcxhr_control_clock_type = {
675 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
676 	.name =		"Clock Mode",
677 	.info =		pcxhr_clock_type_info,
678 	.get =		pcxhr_clock_type_get,
679 	.put =		pcxhr_clock_type_put,
680 };
681 
682 /*
683  * clock rate control
684  * specific control that scans the sample rates on the external plugs
685  */
686 static int pcxhr_clock_rate_info(struct snd_kcontrol *kcontrol,
687 				 struct snd_ctl_elem_info *uinfo)
688 {
689 	struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
690 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
691 	uinfo->count = 3 + mgr->capture_chips;
692 	uinfo->value.integer.min = 0;		/* clock not present */
693 	uinfo->value.integer.max = 192000;	/* max sample rate 192 kHz */
694 	return 0;
695 }
696 
697 static int pcxhr_clock_rate_get(struct snd_kcontrol *kcontrol,
698 				struct snd_ctl_elem_value *ucontrol)
699 {
700 	struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
701 	int i, err, rate;
702 
703 	mutex_lock(&mgr->mixer_mutex);
704 	for(i = 0; i < 3 + mgr->capture_chips; i++) {
705 		if (i == PCXHR_CLOCK_TYPE_INTERNAL)
706 			rate = mgr->sample_rate_real;
707 		else {
708 			err = pcxhr_get_external_clock(mgr, i, &rate);
709 			if (err)
710 				break;
711 		}
712 		ucontrol->value.integer.value[i] = rate;
713 	}
714 	mutex_unlock(&mgr->mixer_mutex);
715 	return 0;
716 }
717 
718 static struct snd_kcontrol_new pcxhr_control_clock_rate = {
719 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
720 	.iface =	SNDRV_CTL_ELEM_IFACE_CARD,
721 	.name =		"Clock Rates",
722 	.info =		pcxhr_clock_rate_info,
723 	.get =		pcxhr_clock_rate_get,
724 };
725 
726 /*
727  * IEC958 status bits
728  */
729 static int pcxhr_iec958_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
730 {
731 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
732 	uinfo->count = 1;
733 	return 0;
734 }
735 
736 static int pcxhr_iec958_capture_byte(struct snd_pcxhr *chip, int aes_idx, unsigned char* aes_bits)
737 {
738 	int i, err;
739 	unsigned char temp;
740 	struct pcxhr_rmh rmh;
741 
742 	pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ);
743 	rmh.cmd[0] |= IO_NUM_UER_CHIP_REG;
744 	switch (chip->chip_idx) {
745 	case 0:	rmh.cmd[1] = CS8420_01_CS; break;	/* use CS8416_01_CS for AES SYNC plug */
746 	case 1:	rmh.cmd[1] = CS8420_23_CS; break;
747 	case 2:	rmh.cmd[1] = CS8420_45_CS; break;
748 	case 3:	rmh.cmd[1] = CS8420_67_CS; break;
749 	default: return -EINVAL;
750 	}
751 	switch (aes_idx) {
752 	case 0:	rmh.cmd[2] = CS8420_CSB0; break;	/* use CS8416_CSBx for AES SYNC plug */
753 	case 1:	rmh.cmd[2] = CS8420_CSB1; break;
754 	case 2:	rmh.cmd[2] = CS8420_CSB2; break;
755 	case 3:	rmh.cmd[2] = CS8420_CSB3; break;
756 	case 4:	rmh.cmd[2] = CS8420_CSB4; break;
757 	default: return -EINVAL;
758 	}
759 	rmh.cmd[1] &= 0x0fffff;			/* size and code the chip id for the fpga */
760 	rmh.cmd[2] &= CHIP_SIG_AND_MAP_SPI;	/* chip signature + map for spi read */
761 	rmh.cmd_len = 3;
762 	err = pcxhr_send_msg(chip->mgr, &rmh);
763 	if (err)
764 		return err;
765 	temp = 0;
766 	for (i = 0; i < 8; i++) {
767 		/* attention : reversed bit order (not with CS8416_01_CS) */
768 		temp <<= 1;
769 		if (rmh.stat[1] & (1 << i))
770 			temp |= 1;
771 	}
772 	snd_printdd("read iec958 AES %d byte %d = 0x%x\n", chip->chip_idx, aes_idx, temp);
773 	*aes_bits = temp;
774 	return 0;
775 }
776 
777 static int pcxhr_iec958_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
778 {
779 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
780 	unsigned char aes_bits;
781 	int i, err;
782 
783 	mutex_lock(&chip->mgr->mixer_mutex);
784 	for(i = 0; i < 5; i++) {
785 		if (kcontrol->private_value == 0)	/* playback */
786 			aes_bits = chip->aes_bits[i];
787 		else {				/* capture */
788 			err = pcxhr_iec958_capture_byte(chip, i, &aes_bits);
789 			if (err)
790 				break;
791 		}
792 		ucontrol->value.iec958.status[i] = aes_bits;
793 	}
794 	mutex_unlock(&chip->mgr->mixer_mutex);
795         return 0;
796 }
797 
798 static int pcxhr_iec958_mask_get(struct snd_kcontrol *kcontrol,
799 				 struct snd_ctl_elem_value *ucontrol)
800 {
801 	int i;
802 	for (i = 0; i < 5; i++)
803 		ucontrol->value.iec958.status[i] = 0xff;
804         return 0;
805 }
806 
807 static int pcxhr_iec958_update_byte(struct snd_pcxhr *chip, int aes_idx, unsigned char aes_bits)
808 {
809 	int i, err, cmd;
810 	unsigned char new_bits = aes_bits;
811 	unsigned char old_bits = chip->aes_bits[aes_idx];
812 	struct pcxhr_rmh rmh;
813 
814 	for (i = 0; i < 8; i++) {
815 		if ((old_bits & 0x01) != (new_bits & 0x01)) {
816 			cmd = chip->chip_idx & 0x03;		/* chip index 0..3 */
817 			if(chip->chip_idx > 3)
818 				/* new bit used if chip_idx>3 (PCX1222HR) */
819 				cmd |= 1 << 22;
820 			cmd |= ((aes_idx << 3) + i) << 2;	/* add bit offset */
821 			cmd |= (new_bits & 0x01) << 23;		/* add bit value */
822 			pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
823 			rmh.cmd[0] |= IO_NUM_REG_CUER;
824 			rmh.cmd[1] = cmd;
825 			rmh.cmd_len = 2;
826 			snd_printdd("write iec958 AES %d byte %d bit %d (cmd %x)\n",
827 				    chip->chip_idx, aes_idx, i, cmd);
828 			err = pcxhr_send_msg(chip->mgr, &rmh);
829 			if (err)
830 				return err;
831 		}
832 		old_bits >>= 1;
833 		new_bits >>= 1;
834 	}
835 	chip->aes_bits[aes_idx] = aes_bits;
836 	return 0;
837 }
838 
839 static int pcxhr_iec958_put(struct snd_kcontrol *kcontrol,
840 			    struct snd_ctl_elem_value *ucontrol)
841 {
842 	struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
843 	int i, changed = 0;
844 
845 	/* playback */
846 	mutex_lock(&chip->mgr->mixer_mutex);
847 	for (i = 0; i < 5; i++) {
848 		if (ucontrol->value.iec958.status[i] != chip->aes_bits[i]) {
849 			pcxhr_iec958_update_byte(chip, i, ucontrol->value.iec958.status[i]);
850 			changed = 1;
851 		}
852 	}
853 	mutex_unlock(&chip->mgr->mixer_mutex);
854 	return changed;
855 }
856 
857 static struct snd_kcontrol_new pcxhr_control_playback_iec958_mask = {
858 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
859 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
860 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
861 	.info =		pcxhr_iec958_info,
862 	.get =		pcxhr_iec958_mask_get
863 };
864 static struct snd_kcontrol_new pcxhr_control_playback_iec958 = {
865 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
866 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
867 	.info =         pcxhr_iec958_info,
868 	.get =          pcxhr_iec958_get,
869 	.put =          pcxhr_iec958_put,
870 	.private_value = 0 /* playback */
871 };
872 
873 static struct snd_kcontrol_new pcxhr_control_capture_iec958_mask = {
874 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
875 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
876 	.name =		SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK),
877 	.info =		pcxhr_iec958_info,
878 	.get =		pcxhr_iec958_mask_get
879 };
880 static struct snd_kcontrol_new pcxhr_control_capture_iec958 = {
881 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
882 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
883 	.name =         SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
884 	.info =         pcxhr_iec958_info,
885 	.get =          pcxhr_iec958_get,
886 	.private_value = 1 /* capture */
887 };
888 
889 static void pcxhr_init_audio_levels(struct snd_pcxhr *chip)
890 {
891 	int i;
892 
893 	for (i = 0; i < 2; i++) {
894 		if (chip->nb_streams_play) {
895 			int j;
896 			/* at boot time the digital volumes are unmuted 0dB */
897 			for (j = 0; j < PCXHR_PLAYBACK_STREAMS; j++) {
898 				chip->digital_playback_active[j][i] = 1;
899 				chip->digital_playback_volume[j][i] = PCXHR_DIGITAL_ZERO_LEVEL;
900 			}
901 			/* after boot, only two bits are set on the uer interface */
902 			chip->aes_bits[0] = IEC958_AES0_PROFESSIONAL | IEC958_AES0_PRO_FS_48000;
903 /* only for test purpose, remove later */
904 #ifdef CONFIG_SND_DEBUG
905 			/* analog volumes for playback (is LEVEL_MIN after boot) */
906 			chip->analog_playback_active[i] = 1;
907 			chip->analog_playback_volume[i] = PCXHR_ANALOG_PLAYBACK_ZERO_LEVEL;
908 			pcxhr_update_analog_audio_level(chip, 0, i);
909 #endif
910 /* test end */
911 		}
912 		if (chip->nb_streams_capt) {
913 			/* at boot time the digital volumes are unmuted 0dB */
914 			chip->digital_capture_volume[i] = PCXHR_DIGITAL_ZERO_LEVEL;
915 /* only for test purpose, remove later */
916 #ifdef CONFIG_SND_DEBUG
917 			/* analog volumes for playback (is LEVEL_MIN after boot) */
918 			chip->analog_capture_volume[i]  = PCXHR_ANALOG_CAPTURE_ZERO_LEVEL;
919 			pcxhr_update_analog_audio_level(chip, 1, i);
920 #endif
921 /* test end */
922 		}
923 	}
924 
925 	return;
926 }
927 
928 
929 int pcxhr_create_mixer(struct pcxhr_mgr *mgr)
930 {
931 	struct snd_pcxhr *chip;
932 	int err, i;
933 
934 	mutex_init(&mgr->mixer_mutex); /* can be in another place */
935 
936 	for (i = 0; i < mgr->num_cards; i++) {
937 		struct snd_kcontrol_new temp;
938 		chip = mgr->chip[i];
939 
940 		if (chip->nb_streams_play) {
941 			/* analog output level control */
942 			temp = pcxhr_control_analog_level;
943 			temp.name = "Master Playback Volume";
944 			temp.private_value = 0; /* playback */
945 			temp.tlv.p = db_scale_analog_playback;
946 			if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&temp, chip))) < 0)
947 				return err;
948 			/* output mute controls */
949 			if ((err = snd_ctl_add(chip->card,
950 					       snd_ctl_new1(&pcxhr_control_output_switch,
951 							    chip))) < 0)
952 				return err;
953 
954 			temp = snd_pcxhr_pcm_vol;
955 			temp.name = "PCM Playback Volume";
956 			temp.count = PCXHR_PLAYBACK_STREAMS;
957 			temp.private_value = 0; /* playback */
958 			if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&temp, chip))) < 0)
959 				return err;
960 
961 			if ((err = snd_ctl_add(chip->card,
962 					       snd_ctl_new1(&pcxhr_control_pcm_switch,
963 							    chip))) < 0)
964 				return err;
965 
966 			/* IEC958 controls */
967 			if ((err = snd_ctl_add(chip->card,
968 					       snd_ctl_new1(&pcxhr_control_playback_iec958_mask,
969 							    chip))) < 0)
970 				return err;
971 			if ((err = snd_ctl_add(chip->card,
972 					       snd_ctl_new1(&pcxhr_control_playback_iec958,
973 							    chip))) < 0)
974 				return err;
975 		}
976 		if (chip->nb_streams_capt) {
977 			/* analog input level control only on first two chips !*/
978 			temp = pcxhr_control_analog_level;
979 			temp.name = "Master Capture Volume";
980 			temp.private_value = 1; /* capture */
981 			temp.tlv.p = db_scale_analog_capture;
982 			if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&temp, chip))) < 0)
983 				return err;
984 
985 			temp = snd_pcxhr_pcm_vol;
986 			temp.name = "PCM Capture Volume";
987 			temp.count = 1;
988 			temp.private_value = 1; /* capture */
989 			if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&temp, chip))) < 0)
990 				return err;
991 			/* Audio source */
992 			if ((err = snd_ctl_add(chip->card,
993 					       snd_ctl_new1(&pcxhr_control_audio_src,
994 							    chip))) < 0)
995 				return err;
996 			/* IEC958 controls */
997 			if ((err = snd_ctl_add(chip->card,
998 					       snd_ctl_new1(&pcxhr_control_capture_iec958_mask,
999 							    chip))) < 0)
1000 				return err;
1001 			if ((err = snd_ctl_add(chip->card,
1002 					       snd_ctl_new1(&pcxhr_control_capture_iec958,
1003 							    chip))) < 0)
1004 				return err;
1005 		}
1006 		/* monitoring only if playback and capture device available */
1007 		if (chip->nb_streams_capt > 0 && chip->nb_streams_play > 0) {
1008 			/* monitoring */
1009 			if ((err = snd_ctl_add(chip->card,
1010 					       snd_ctl_new1(&pcxhr_control_monitor_vol,
1011 							    chip))) < 0)
1012 				return err;
1013 			if ((err = snd_ctl_add(chip->card,
1014 					       snd_ctl_new1(&pcxhr_control_monitor_sw,
1015 							    chip))) < 0)
1016 				return err;
1017 		}
1018 
1019 		if (i == 0) {
1020 			/* clock mode only one control per pcxhr */
1021 			if ((err = snd_ctl_add(chip->card,
1022 					       snd_ctl_new1(&pcxhr_control_clock_type,
1023 							    mgr))) < 0)
1024 				return err;
1025 			/* non standard control used to scan the external clock presence/frequencies */
1026 			if ((err = snd_ctl_add(chip->card,
1027 					       snd_ctl_new1(&pcxhr_control_clock_rate,
1028 							    mgr))) < 0)
1029 				return err;
1030 		}
1031 
1032 		/* init values for the mixer data */
1033 		pcxhr_init_audio_levels(chip);
1034 	}
1035 
1036 	return 0;
1037 }
1038