AT91: Added a generic way to setup AT91 serial ports in Kconfig
[linux-2.6/pdupreez.git] / sound / pci / pcxhr / pcxhr_mixer.c
blobaabc7bc5321e89d7c60bbdaaf0624599014aaa73
1 #define __NO_VERSION__
2 /*
3 * Driver for Digigram pcxhr compatible soundcards
5 * mixer callbacks
7 * Copyright (c) 2004 by Digigram <alsa@digigram.com>
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.
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.
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
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"
38 #define PCXHR_ANALOG_CAPTURE_LEVEL_MIN 0 /* -96.0 dB */
39 #define PCXHR_ANALOG_CAPTURE_LEVEL_MAX 255 /* +31.5 dB */
40 #define PCXHR_ANALOG_CAPTURE_ZERO_LEVEL 224 /* +16.0 dB ( +31.5 dB - fix level +15.5 dB ) */
42 #define PCXHR_ANALOG_PLAYBACK_LEVEL_MIN 0 /* -128.0 dB */
43 #define PCXHR_ANALOG_PLAYBACK_LEVEL_MAX 128 /* 0.0 dB */
44 #define PCXHR_ANALOG_PLAYBACK_ZERO_LEVEL 104 /* -24.0 dB ( 0.0 dB - fix level +24.0 dB ) */
46 static const DECLARE_TLV_DB_SCALE(db_scale_analog_capture, -9600, 50, 3150);
47 static const DECLARE_TLV_DB_SCALE(db_scale_analog_playback, -10400, 100, 2400);
49 static int pcxhr_update_analog_audio_level(struct snd_pcxhr *chip, int is_capture, int channel)
51 int err, vol;
52 struct pcxhr_rmh rmh;
54 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
55 if (is_capture) {
56 rmh.cmd[0] |= IO_NUM_REG_IN_ANA_LEVEL;
57 rmh.cmd[2] = chip->analog_capture_volume[channel];
58 } else {
59 rmh.cmd[0] |= IO_NUM_REG_OUT_ANA_LEVEL;
60 if (chip->analog_playback_active[channel])
61 vol = chip->analog_playback_volume[channel];
62 else
63 vol = PCXHR_ANALOG_PLAYBACK_LEVEL_MIN;
64 rmh.cmd[2] = PCXHR_ANALOG_PLAYBACK_LEVEL_MAX - vol; /* playback analog levels are inversed */
66 rmh.cmd[1] = 1 << ((2 * chip->chip_idx) + channel); /* audio mask */
67 rmh.cmd_len = 3;
68 err = pcxhr_send_msg(chip->mgr, &rmh);
69 if (err < 0) {
70 snd_printk(KERN_DEBUG "error update_analog_audio_level card(%d) "
71 "is_capture(%d) err(%x)\n", chip->chip_idx, is_capture, err);
72 return -EINVAL;
74 return 0;
78 * analog level control
80 static int pcxhr_analog_vol_info(struct snd_kcontrol *kcontrol,
81 struct snd_ctl_elem_info *uinfo)
83 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
84 uinfo->count = 2;
85 if (kcontrol->private_value == 0) { /* playback */
86 uinfo->value.integer.min = PCXHR_ANALOG_PLAYBACK_LEVEL_MIN; /* -128 dB */
87 uinfo->value.integer.max = PCXHR_ANALOG_PLAYBACK_LEVEL_MAX; /* 0 dB */
88 } else { /* capture */
89 uinfo->value.integer.min = PCXHR_ANALOG_CAPTURE_LEVEL_MIN; /* -96 dB */
90 uinfo->value.integer.max = PCXHR_ANALOG_CAPTURE_LEVEL_MAX; /* 31.5 dB */
92 return 0;
95 static int pcxhr_analog_vol_get(struct snd_kcontrol *kcontrol,
96 struct snd_ctl_elem_value *ucontrol)
98 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
99 mutex_lock(&chip->mgr->mixer_mutex);
100 if (kcontrol->private_value == 0) { /* playback */
101 ucontrol->value.integer.value[0] = chip->analog_playback_volume[0];
102 ucontrol->value.integer.value[1] = chip->analog_playback_volume[1];
103 } else { /* capture */
104 ucontrol->value.integer.value[0] = chip->analog_capture_volume[0];
105 ucontrol->value.integer.value[1] = chip->analog_capture_volume[1];
107 mutex_unlock(&chip->mgr->mixer_mutex);
108 return 0;
111 static int pcxhr_analog_vol_put(struct snd_kcontrol *kcontrol,
112 struct snd_ctl_elem_value *ucontrol)
114 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
115 int changed = 0;
116 int is_capture, i;
118 mutex_lock(&chip->mgr->mixer_mutex);
119 is_capture = (kcontrol->private_value != 0);
120 for (i = 0; i < 2; i++) {
121 int new_volume = ucontrol->value.integer.value[i];
122 int *stored_volume = is_capture ?
123 &chip->analog_capture_volume[i] :
124 &chip->analog_playback_volume[i];
125 if (is_capture) {
126 if (new_volume < PCXHR_ANALOG_CAPTURE_LEVEL_MIN ||
127 new_volume > PCXHR_ANALOG_CAPTURE_LEVEL_MAX)
128 continue;
129 } else {
130 if (new_volume < PCXHR_ANALOG_PLAYBACK_LEVEL_MIN ||
131 new_volume > PCXHR_ANALOG_PLAYBACK_LEVEL_MAX)
132 continue;
134 if (*stored_volume != new_volume) {
135 *stored_volume = new_volume;
136 changed = 1;
137 pcxhr_update_analog_audio_level(chip, is_capture, i);
140 mutex_unlock(&chip->mgr->mixer_mutex);
141 return changed;
144 static struct snd_kcontrol_new pcxhr_control_analog_level = {
145 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
146 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
147 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
148 /* name will be filled later */
149 .info = pcxhr_analog_vol_info,
150 .get = pcxhr_analog_vol_get,
151 .put = pcxhr_analog_vol_put,
152 /* tlv will be filled later */
155 /* shared */
156 #define pcxhr_sw_info snd_ctl_boolean_stereo_info
158 static int pcxhr_audio_sw_get(struct snd_kcontrol *kcontrol,
159 struct snd_ctl_elem_value *ucontrol)
161 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
163 mutex_lock(&chip->mgr->mixer_mutex);
164 ucontrol->value.integer.value[0] = chip->analog_playback_active[0];
165 ucontrol->value.integer.value[1] = chip->analog_playback_active[1];
166 mutex_unlock(&chip->mgr->mixer_mutex);
167 return 0;
170 static int pcxhr_audio_sw_put(struct snd_kcontrol *kcontrol,
171 struct snd_ctl_elem_value *ucontrol)
173 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
174 int i, changed = 0;
175 mutex_lock(&chip->mgr->mixer_mutex);
176 for(i = 0; i < 2; i++) {
177 if (chip->analog_playback_active[i] !=
178 ucontrol->value.integer.value[i]) {
179 chip->analog_playback_active[i] =
180 !!ucontrol->value.integer.value[i];
181 changed = 1;
182 /* update playback levels */
183 pcxhr_update_analog_audio_level(chip, 0, i);
186 mutex_unlock(&chip->mgr->mixer_mutex);
187 return changed;
190 static struct snd_kcontrol_new pcxhr_control_output_switch = {
191 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
192 .name = "Master Playback Switch",
193 .info = pcxhr_sw_info, /* shared */
194 .get = pcxhr_audio_sw_get,
195 .put = pcxhr_audio_sw_put
199 #define PCXHR_DIGITAL_LEVEL_MIN 0x000 /* -110 dB */
200 #define PCXHR_DIGITAL_LEVEL_MAX 0x1ff /* +18 dB */
201 #define PCXHR_DIGITAL_ZERO_LEVEL 0x1b7 /* 0 dB */
203 static const DECLARE_TLV_DB_SCALE(db_scale_digital, -10975, 25, 1800);
205 #define MORE_THAN_ONE_STREAM_LEVEL 0x000001
206 #define VALID_STREAM_PAN_LEVEL_MASK 0x800000
207 #define VALID_STREAM_LEVEL_MASK 0x400000
208 #define VALID_STREAM_LEVEL_1_MASK 0x200000
209 #define VALID_STREAM_LEVEL_2_MASK 0x100000
211 static int pcxhr_update_playback_stream_level(struct snd_pcxhr* chip, int idx)
213 int err;
214 struct pcxhr_rmh rmh;
215 struct pcxhr_pipe *pipe = &chip->playback_pipe;
216 int left, right;
218 if (chip->digital_playback_active[idx][0])
219 left = chip->digital_playback_volume[idx][0];
220 else
221 left = PCXHR_DIGITAL_LEVEL_MIN;
222 if (chip->digital_playback_active[idx][1])
223 right = chip->digital_playback_volume[idx][1];
224 else
225 right = PCXHR_DIGITAL_LEVEL_MIN;
227 pcxhr_init_rmh(&rmh, CMD_STREAM_OUT_LEVEL_ADJUST);
228 /* add pipe and stream mask */
229 pcxhr_set_pipe_cmd_params(&rmh, 0, pipe->first_audio, 0, 1<<idx);
230 /* volume left->left / right->right panoramic level */
231 rmh.cmd[0] |= MORE_THAN_ONE_STREAM_LEVEL;
232 rmh.cmd[2] = VALID_STREAM_PAN_LEVEL_MASK | VALID_STREAM_LEVEL_1_MASK;
233 rmh.cmd[2] |= (left << 10);
234 rmh.cmd[3] = VALID_STREAM_PAN_LEVEL_MASK | VALID_STREAM_LEVEL_2_MASK;
235 rmh.cmd[3] |= right;
236 rmh.cmd_len = 4;
238 err = pcxhr_send_msg(chip->mgr, &rmh);
239 if (err < 0) {
240 snd_printk(KERN_DEBUG "error update_playback_stream_level "
241 "card(%d) err(%x)\n", chip->chip_idx, err);
242 return -EINVAL;
244 return 0;
247 #define AUDIO_IO_HAS_MUTE_LEVEL 0x400000
248 #define AUDIO_IO_HAS_MUTE_MONITOR_1 0x200000
249 #define VALID_AUDIO_IO_DIGITAL_LEVEL 0x000001
250 #define VALID_AUDIO_IO_MONITOR_LEVEL 0x000002
251 #define VALID_AUDIO_IO_MUTE_LEVEL 0x000004
252 #define VALID_AUDIO_IO_MUTE_MONITOR_1 0x000008
254 static int pcxhr_update_audio_pipe_level(struct snd_pcxhr* chip, int capture, int channel)
256 int err;
257 struct pcxhr_rmh rmh;
258 struct pcxhr_pipe *pipe;
260 if (capture)
261 pipe = &chip->capture_pipe[0];
262 else
263 pipe = &chip->playback_pipe;
265 pcxhr_init_rmh(&rmh, CMD_AUDIO_LEVEL_ADJUST);
266 /* add channel mask */
267 pcxhr_set_pipe_cmd_params(&rmh, capture, 0, 0, 1 << (channel + pipe->first_audio));
268 /* TODO : if mask (3 << pipe->first_audio) is used, left and right channel
269 * will be programmed to the same params
271 if (capture) {
272 rmh.cmd[0] |= VALID_AUDIO_IO_DIGITAL_LEVEL;
273 /* VALID_AUDIO_IO_MUTE_LEVEL not yet handled (capture pipe level) */
274 rmh.cmd[2] = chip->digital_capture_volume[channel];
275 } else {
276 rmh.cmd[0] |= VALID_AUDIO_IO_MONITOR_LEVEL | VALID_AUDIO_IO_MUTE_MONITOR_1;
277 /* VALID_AUDIO_IO_DIGITAL_LEVEL and VALID_AUDIO_IO_MUTE_LEVEL not yet
278 * handled (playback pipe level)
280 rmh.cmd[2] = chip->monitoring_volume[channel] << 10;
281 if (chip->monitoring_active[channel] == 0)
282 rmh.cmd[2] |= AUDIO_IO_HAS_MUTE_MONITOR_1;
284 rmh.cmd_len = 3;
286 err = pcxhr_send_msg(chip->mgr, &rmh);
287 if(err<0) {
288 snd_printk(KERN_DEBUG "error update_audio_level card(%d) err(%x)\n",
289 chip->chip_idx, err);
290 return -EINVAL;
292 return 0;
296 /* shared */
297 static int pcxhr_digital_vol_info(struct snd_kcontrol *kcontrol,
298 struct snd_ctl_elem_info *uinfo)
300 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
301 uinfo->count = 2;
302 uinfo->value.integer.min = PCXHR_DIGITAL_LEVEL_MIN; /* -109.5 dB */
303 uinfo->value.integer.max = PCXHR_DIGITAL_LEVEL_MAX; /* 18.0 dB */
304 return 0;
308 static int pcxhr_pcm_vol_get(struct snd_kcontrol *kcontrol,
309 struct snd_ctl_elem_value *ucontrol)
311 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
312 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
313 int *stored_volume;
314 int is_capture = kcontrol->private_value;
316 mutex_lock(&chip->mgr->mixer_mutex);
317 if (is_capture)
318 stored_volume = chip->digital_capture_volume; /* digital capture */
319 else
320 stored_volume = chip->digital_playback_volume[idx]; /* digital playback */
321 ucontrol->value.integer.value[0] = stored_volume[0];
322 ucontrol->value.integer.value[1] = stored_volume[1];
323 mutex_unlock(&chip->mgr->mixer_mutex);
324 return 0;
327 static int pcxhr_pcm_vol_put(struct snd_kcontrol *kcontrol,
328 struct snd_ctl_elem_value *ucontrol)
330 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
331 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
332 int changed = 0;
333 int is_capture = kcontrol->private_value;
334 int *stored_volume;
335 int i;
337 mutex_lock(&chip->mgr->mixer_mutex);
338 if (is_capture) /* digital capture */
339 stored_volume = chip->digital_capture_volume;
340 else /* digital playback */
341 stored_volume = chip->digital_playback_volume[idx];
342 for (i = 0; i < 2; i++) {
343 int vol = ucontrol->value.integer.value[i];
344 if (vol < PCXHR_DIGITAL_LEVEL_MIN ||
345 vol > PCXHR_DIGITAL_LEVEL_MAX)
346 continue;
347 if (stored_volume[i] != vol) {
348 stored_volume[i] = vol;
349 changed = 1;
350 if (is_capture) /* update capture volume */
351 pcxhr_update_audio_pipe_level(chip, 1, i);
354 if (!is_capture && changed) /* update playback volume */
355 pcxhr_update_playback_stream_level(chip, idx);
356 mutex_unlock(&chip->mgr->mixer_mutex);
357 return changed;
360 static struct snd_kcontrol_new snd_pcxhr_pcm_vol =
362 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
363 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
364 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
365 /* name will be filled later */
366 /* count will be filled later */
367 .info = pcxhr_digital_vol_info, /* shared */
368 .get = pcxhr_pcm_vol_get,
369 .put = pcxhr_pcm_vol_put,
370 .tlv = { .p = db_scale_digital },
374 static int pcxhr_pcm_sw_get(struct snd_kcontrol *kcontrol,
375 struct snd_ctl_elem_value *ucontrol)
377 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
378 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
380 mutex_lock(&chip->mgr->mixer_mutex);
381 ucontrol->value.integer.value[0] = chip->digital_playback_active[idx][0];
382 ucontrol->value.integer.value[1] = chip->digital_playback_active[idx][1];
383 mutex_unlock(&chip->mgr->mixer_mutex);
384 return 0;
387 static int pcxhr_pcm_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
389 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
390 int changed = 0;
391 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
392 int i, j;
394 mutex_lock(&chip->mgr->mixer_mutex);
395 j = idx;
396 for (i = 0; i < 2; i++) {
397 if (chip->digital_playback_active[j][i] !=
398 ucontrol->value.integer.value[i]) {
399 chip->digital_playback_active[j][i] =
400 !!ucontrol->value.integer.value[i];
401 changed = 1;
404 if (changed)
405 pcxhr_update_playback_stream_level(chip, idx);
406 mutex_unlock(&chip->mgr->mixer_mutex);
407 return changed;
410 static struct snd_kcontrol_new pcxhr_control_pcm_switch = {
411 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
412 .name = "PCM Playback Switch",
413 .count = PCXHR_PLAYBACK_STREAMS,
414 .info = pcxhr_sw_info, /* shared */
415 .get = pcxhr_pcm_sw_get,
416 .put = pcxhr_pcm_sw_put
421 * monitoring level control
424 static int pcxhr_monitor_vol_get(struct snd_kcontrol *kcontrol,
425 struct snd_ctl_elem_value *ucontrol)
427 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
428 mutex_lock(&chip->mgr->mixer_mutex);
429 ucontrol->value.integer.value[0] = chip->monitoring_volume[0];
430 ucontrol->value.integer.value[1] = chip->monitoring_volume[1];
431 mutex_unlock(&chip->mgr->mixer_mutex);
432 return 0;
435 static int pcxhr_monitor_vol_put(struct snd_kcontrol *kcontrol,
436 struct snd_ctl_elem_value *ucontrol)
438 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
439 int changed = 0;
440 int i;
442 mutex_lock(&chip->mgr->mixer_mutex);
443 for (i = 0; i < 2; i++) {
444 if (chip->monitoring_volume[i] !=
445 ucontrol->value.integer.value[i]) {
446 chip->monitoring_volume[i] =
447 !!ucontrol->value.integer.value[i];
448 if(chip->monitoring_active[i])
449 /* update monitoring volume and mute */
450 /* do only when monitoring is unmuted */
451 pcxhr_update_audio_pipe_level(chip, 0, i);
452 changed = 1;
455 mutex_unlock(&chip->mgr->mixer_mutex);
456 return changed;
459 static struct snd_kcontrol_new pcxhr_control_monitor_vol = {
460 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
461 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
462 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
463 .name = "Monitoring Volume",
464 .info = pcxhr_digital_vol_info, /* shared */
465 .get = pcxhr_monitor_vol_get,
466 .put = pcxhr_monitor_vol_put,
467 .tlv = { .p = db_scale_digital },
471 * monitoring switch control
474 static int pcxhr_monitor_sw_get(struct snd_kcontrol *kcontrol,
475 struct snd_ctl_elem_value *ucontrol)
477 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
478 mutex_lock(&chip->mgr->mixer_mutex);
479 ucontrol->value.integer.value[0] = chip->monitoring_active[0];
480 ucontrol->value.integer.value[1] = chip->monitoring_active[1];
481 mutex_unlock(&chip->mgr->mixer_mutex);
482 return 0;
485 static int pcxhr_monitor_sw_put(struct snd_kcontrol *kcontrol,
486 struct snd_ctl_elem_value *ucontrol)
488 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
489 int changed = 0;
490 int i;
492 mutex_lock(&chip->mgr->mixer_mutex);
493 for (i = 0; i < 2; i++) {
494 if (chip->monitoring_active[i] !=
495 ucontrol->value.integer.value[i]) {
496 chip->monitoring_active[i] =
497 !!ucontrol->value.integer.value[i];
498 changed |= (1<<i); /* mask 0x01 and 0x02 */
501 if (changed & 0x01)
502 /* update left monitoring volume and mute */
503 pcxhr_update_audio_pipe_level(chip, 0, 0);
504 if (changed & 0x02)
505 /* update right monitoring volume and mute */
506 pcxhr_update_audio_pipe_level(chip, 0, 1);
508 mutex_unlock(&chip->mgr->mixer_mutex);
509 return (changed != 0);
512 static struct snd_kcontrol_new pcxhr_control_monitor_sw = {
513 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
514 .name = "Monitoring Switch",
515 .info = pcxhr_sw_info, /* shared */
516 .get = pcxhr_monitor_sw_get,
517 .put = pcxhr_monitor_sw_put
523 * audio source select
525 #define PCXHR_SOURCE_AUDIO01_UER 0x000100
526 #define PCXHR_SOURCE_AUDIO01_SYNC 0x000200
527 #define PCXHR_SOURCE_AUDIO23_UER 0x000400
528 #define PCXHR_SOURCE_AUDIO45_UER 0x001000
529 #define PCXHR_SOURCE_AUDIO67_UER 0x040000
531 static int pcxhr_set_audio_source(struct snd_pcxhr* chip)
533 struct pcxhr_rmh rmh;
534 unsigned int mask, reg;
535 unsigned int codec;
536 int err, use_src, changed;
538 switch (chip->chip_idx) {
539 case 0 : mask = PCXHR_SOURCE_AUDIO01_UER; codec = CS8420_01_CS; break;
540 case 1 : mask = PCXHR_SOURCE_AUDIO23_UER; codec = CS8420_23_CS; break;
541 case 2 : mask = PCXHR_SOURCE_AUDIO45_UER; codec = CS8420_45_CS; break;
542 case 3 : mask = PCXHR_SOURCE_AUDIO67_UER; codec = CS8420_67_CS; break;
543 default: return -EINVAL;
545 reg = 0; /* audio source from analog plug */
546 use_src = 0; /* do not activate codec SRC */
548 if (chip->audio_capture_source != 0) {
549 reg = mask; /* audio source from digital plug */
550 if (chip->audio_capture_source == 2)
551 use_src = 1;
553 /* set the input source */
554 pcxhr_write_io_num_reg_cont(chip->mgr, mask, reg, &changed);
555 /* resync them (otherwise channel inversion possible) */
556 if (changed) {
557 pcxhr_init_rmh(&rmh, CMD_RESYNC_AUDIO_INPUTS);
558 rmh.cmd[0] |= (1 << chip->chip_idx);
559 err = pcxhr_send_msg(chip->mgr, &rmh);
560 if (err)
561 return err;
563 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE); /* set codec SRC on off */
564 rmh.cmd_len = 3;
565 rmh.cmd[0] |= IO_NUM_UER_CHIP_REG;
566 rmh.cmd[1] = codec;
567 rmh.cmd[2] = (CS8420_DATA_FLOW_CTL & CHIP_SIG_AND_MAP_SPI) | (use_src ? 0x41 : 0x54);
568 err = pcxhr_send_msg(chip->mgr, &rmh);
569 if(err)
570 return err;
571 rmh.cmd[2] = (CS8420_CLOCK_SRC_CTL & CHIP_SIG_AND_MAP_SPI) | (use_src ? 0x41 : 0x49);
572 err = pcxhr_send_msg(chip->mgr, &rmh);
573 return err;
576 static int pcxhr_audio_src_info(struct snd_kcontrol *kcontrol,
577 struct snd_ctl_elem_info *uinfo)
579 static char *texts[3] = {"Analog", "Digital", "Digi+SRC"};
581 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
582 uinfo->count = 1;
583 uinfo->value.enumerated.items = 3;
584 if (uinfo->value.enumerated.item > 2)
585 uinfo->value.enumerated.item = 2;
586 strcpy(uinfo->value.enumerated.name,
587 texts[uinfo->value.enumerated.item]);
588 return 0;
591 static int pcxhr_audio_src_get(struct snd_kcontrol *kcontrol,
592 struct snd_ctl_elem_value *ucontrol)
594 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
595 ucontrol->value.enumerated.item[0] = chip->audio_capture_source;
596 return 0;
599 static int pcxhr_audio_src_put(struct snd_kcontrol *kcontrol,
600 struct snd_ctl_elem_value *ucontrol)
602 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
603 int ret = 0;
605 if (ucontrol->value.enumerated.item[0] >= 3)
606 return -EINVAL;
607 mutex_lock(&chip->mgr->mixer_mutex);
608 if (chip->audio_capture_source != ucontrol->value.enumerated.item[0]) {
609 chip->audio_capture_source = ucontrol->value.enumerated.item[0];
610 pcxhr_set_audio_source(chip);
611 ret = 1;
613 mutex_unlock(&chip->mgr->mixer_mutex);
614 return ret;
617 static struct snd_kcontrol_new pcxhr_control_audio_src = {
618 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
619 .name = "Capture Source",
620 .info = pcxhr_audio_src_info,
621 .get = pcxhr_audio_src_get,
622 .put = pcxhr_audio_src_put,
627 * clock type selection
628 * enum pcxhr_clock_type {
629 * PCXHR_CLOCK_TYPE_INTERNAL = 0,
630 * PCXHR_CLOCK_TYPE_WORD_CLOCK,
631 * PCXHR_CLOCK_TYPE_AES_SYNC,
632 * PCXHR_CLOCK_TYPE_AES_1,
633 * PCXHR_CLOCK_TYPE_AES_2,
634 * PCXHR_CLOCK_TYPE_AES_3,
635 * PCXHR_CLOCK_TYPE_AES_4,
636 * };
639 static int pcxhr_clock_type_info(struct snd_kcontrol *kcontrol,
640 struct snd_ctl_elem_info *uinfo)
642 static char *texts[7] = {
643 "Internal", "WordClock", "AES Sync", "AES 1", "AES 2", "AES 3", "AES 4"
645 struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
646 int clock_items = 3 + mgr->capture_chips;
648 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
649 uinfo->count = 1;
650 uinfo->value.enumerated.items = clock_items;
651 if (uinfo->value.enumerated.item >= clock_items)
652 uinfo->value.enumerated.item = clock_items-1;
653 strcpy(uinfo->value.enumerated.name,
654 texts[uinfo->value.enumerated.item]);
655 return 0;
658 static int pcxhr_clock_type_get(struct snd_kcontrol *kcontrol,
659 struct snd_ctl_elem_value *ucontrol)
661 struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
662 ucontrol->value.enumerated.item[0] = mgr->use_clock_type;
663 return 0;
666 static int pcxhr_clock_type_put(struct snd_kcontrol *kcontrol,
667 struct snd_ctl_elem_value *ucontrol)
669 struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
670 unsigned int clock_items = 3 + mgr->capture_chips;
671 int rate, ret = 0;
673 if (ucontrol->value.enumerated.item[0] >= clock_items)
674 return -EINVAL;
675 mutex_lock(&mgr->mixer_mutex);
676 if (mgr->use_clock_type != ucontrol->value.enumerated.item[0]) {
677 mutex_lock(&mgr->setup_mutex);
678 mgr->use_clock_type = ucontrol->value.enumerated.item[0];
679 if (mgr->use_clock_type)
680 pcxhr_get_external_clock(mgr, mgr->use_clock_type, &rate);
681 else
682 rate = mgr->sample_rate;
683 if (rate) {
684 pcxhr_set_clock(mgr, rate);
685 if (mgr->sample_rate)
686 mgr->sample_rate = rate;
688 mutex_unlock(&mgr->setup_mutex);
689 ret = 1; /* return 1 even if the set was not done. ok ? */
691 mutex_unlock(&mgr->mixer_mutex);
692 return ret;
695 static struct snd_kcontrol_new pcxhr_control_clock_type = {
696 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
697 .name = "Clock Mode",
698 .info = pcxhr_clock_type_info,
699 .get = pcxhr_clock_type_get,
700 .put = pcxhr_clock_type_put,
704 * clock rate control
705 * specific control that scans the sample rates on the external plugs
707 static int pcxhr_clock_rate_info(struct snd_kcontrol *kcontrol,
708 struct snd_ctl_elem_info *uinfo)
710 struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
711 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
712 uinfo->count = 3 + mgr->capture_chips;
713 uinfo->value.integer.min = 0; /* clock not present */
714 uinfo->value.integer.max = 192000; /* max sample rate 192 kHz */
715 return 0;
718 static int pcxhr_clock_rate_get(struct snd_kcontrol *kcontrol,
719 struct snd_ctl_elem_value *ucontrol)
721 struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
722 int i, err, rate;
724 mutex_lock(&mgr->mixer_mutex);
725 for(i = 0; i < 3 + mgr->capture_chips; i++) {
726 if (i == PCXHR_CLOCK_TYPE_INTERNAL)
727 rate = mgr->sample_rate_real;
728 else {
729 err = pcxhr_get_external_clock(mgr, i, &rate);
730 if (err)
731 break;
733 ucontrol->value.integer.value[i] = rate;
735 mutex_unlock(&mgr->mixer_mutex);
736 return 0;
739 static struct snd_kcontrol_new pcxhr_control_clock_rate = {
740 .access = SNDRV_CTL_ELEM_ACCESS_READ,
741 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
742 .name = "Clock Rates",
743 .info = pcxhr_clock_rate_info,
744 .get = pcxhr_clock_rate_get,
748 * IEC958 status bits
750 static int pcxhr_iec958_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
752 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
753 uinfo->count = 1;
754 return 0;
757 static int pcxhr_iec958_capture_byte(struct snd_pcxhr *chip, int aes_idx, unsigned char* aes_bits)
759 int i, err;
760 unsigned char temp;
761 struct pcxhr_rmh rmh;
763 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ);
764 rmh.cmd[0] |= IO_NUM_UER_CHIP_REG;
765 switch (chip->chip_idx) {
766 case 0: rmh.cmd[1] = CS8420_01_CS; break; /* use CS8416_01_CS for AES SYNC plug */
767 case 1: rmh.cmd[1] = CS8420_23_CS; break;
768 case 2: rmh.cmd[1] = CS8420_45_CS; break;
769 case 3: rmh.cmd[1] = CS8420_67_CS; break;
770 default: return -EINVAL;
772 switch (aes_idx) {
773 case 0: rmh.cmd[2] = CS8420_CSB0; break; /* use CS8416_CSBx for AES SYNC plug */
774 case 1: rmh.cmd[2] = CS8420_CSB1; break;
775 case 2: rmh.cmd[2] = CS8420_CSB2; break;
776 case 3: rmh.cmd[2] = CS8420_CSB3; break;
777 case 4: rmh.cmd[2] = CS8420_CSB4; break;
778 default: return -EINVAL;
780 rmh.cmd[1] &= 0x0fffff; /* size and code the chip id for the fpga */
781 rmh.cmd[2] &= CHIP_SIG_AND_MAP_SPI; /* chip signature + map for spi read */
782 rmh.cmd_len = 3;
783 err = pcxhr_send_msg(chip->mgr, &rmh);
784 if (err)
785 return err;
786 temp = 0;
787 for (i = 0; i < 8; i++) {
788 /* attention : reversed bit order (not with CS8416_01_CS) */
789 temp <<= 1;
790 if (rmh.stat[1] & (1 << i))
791 temp |= 1;
793 snd_printdd("read iec958 AES %d byte %d = 0x%x\n", chip->chip_idx, aes_idx, temp);
794 *aes_bits = temp;
795 return 0;
798 static int pcxhr_iec958_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
800 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
801 unsigned char aes_bits;
802 int i, err;
804 mutex_lock(&chip->mgr->mixer_mutex);
805 for(i = 0; i < 5; i++) {
806 if (kcontrol->private_value == 0) /* playback */
807 aes_bits = chip->aes_bits[i];
808 else { /* capture */
809 err = pcxhr_iec958_capture_byte(chip, i, &aes_bits);
810 if (err)
811 break;
813 ucontrol->value.iec958.status[i] = aes_bits;
815 mutex_unlock(&chip->mgr->mixer_mutex);
816 return 0;
819 static int pcxhr_iec958_mask_get(struct snd_kcontrol *kcontrol,
820 struct snd_ctl_elem_value *ucontrol)
822 int i;
823 for (i = 0; i < 5; i++)
824 ucontrol->value.iec958.status[i] = 0xff;
825 return 0;
828 static int pcxhr_iec958_update_byte(struct snd_pcxhr *chip, int aes_idx, unsigned char aes_bits)
830 int i, err, cmd;
831 unsigned char new_bits = aes_bits;
832 unsigned char old_bits = chip->aes_bits[aes_idx];
833 struct pcxhr_rmh rmh;
835 for (i = 0; i < 8; i++) {
836 if ((old_bits & 0x01) != (new_bits & 0x01)) {
837 cmd = chip->chip_idx & 0x03; /* chip index 0..3 */
838 if(chip->chip_idx > 3)
839 /* new bit used if chip_idx>3 (PCX1222HR) */
840 cmd |= 1 << 22;
841 cmd |= ((aes_idx << 3) + i) << 2; /* add bit offset */
842 cmd |= (new_bits & 0x01) << 23; /* add bit value */
843 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
844 rmh.cmd[0] |= IO_NUM_REG_CUER;
845 rmh.cmd[1] = cmd;
846 rmh.cmd_len = 2;
847 snd_printdd("write iec958 AES %d byte %d bit %d (cmd %x)\n",
848 chip->chip_idx, aes_idx, i, cmd);
849 err = pcxhr_send_msg(chip->mgr, &rmh);
850 if (err)
851 return err;
853 old_bits >>= 1;
854 new_bits >>= 1;
856 chip->aes_bits[aes_idx] = aes_bits;
857 return 0;
860 static int pcxhr_iec958_put(struct snd_kcontrol *kcontrol,
861 struct snd_ctl_elem_value *ucontrol)
863 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
864 int i, changed = 0;
866 /* playback */
867 mutex_lock(&chip->mgr->mixer_mutex);
868 for (i = 0; i < 5; i++) {
869 if (ucontrol->value.iec958.status[i] != chip->aes_bits[i]) {
870 pcxhr_iec958_update_byte(chip, i, ucontrol->value.iec958.status[i]);
871 changed = 1;
874 mutex_unlock(&chip->mgr->mixer_mutex);
875 return changed;
878 static struct snd_kcontrol_new pcxhr_control_playback_iec958_mask = {
879 .access = SNDRV_CTL_ELEM_ACCESS_READ,
880 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
881 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
882 .info = pcxhr_iec958_info,
883 .get = pcxhr_iec958_mask_get
885 static struct snd_kcontrol_new pcxhr_control_playback_iec958 = {
886 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
887 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
888 .info = pcxhr_iec958_info,
889 .get = pcxhr_iec958_get,
890 .put = pcxhr_iec958_put,
891 .private_value = 0 /* playback */
894 static struct snd_kcontrol_new pcxhr_control_capture_iec958_mask = {
895 .access = SNDRV_CTL_ELEM_ACCESS_READ,
896 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
897 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK),
898 .info = pcxhr_iec958_info,
899 .get = pcxhr_iec958_mask_get
901 static struct snd_kcontrol_new pcxhr_control_capture_iec958 = {
902 .access = SNDRV_CTL_ELEM_ACCESS_READ,
903 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
904 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
905 .info = pcxhr_iec958_info,
906 .get = pcxhr_iec958_get,
907 .private_value = 1 /* capture */
910 static void pcxhr_init_audio_levels(struct snd_pcxhr *chip)
912 int i;
914 for (i = 0; i < 2; i++) {
915 if (chip->nb_streams_play) {
916 int j;
917 /* at boot time the digital volumes are unmuted 0dB */
918 for (j = 0; j < PCXHR_PLAYBACK_STREAMS; j++) {
919 chip->digital_playback_active[j][i] = 1;
920 chip->digital_playback_volume[j][i] = PCXHR_DIGITAL_ZERO_LEVEL;
922 /* after boot, only two bits are set on the uer interface */
923 chip->aes_bits[0] = IEC958_AES0_PROFESSIONAL | IEC958_AES0_PRO_FS_48000;
924 /* only for test purpose, remove later */
925 #ifdef CONFIG_SND_DEBUG
926 /* analog volumes for playback (is LEVEL_MIN after boot) */
927 chip->analog_playback_active[i] = 1;
928 chip->analog_playback_volume[i] = PCXHR_ANALOG_PLAYBACK_ZERO_LEVEL;
929 pcxhr_update_analog_audio_level(chip, 0, i);
930 #endif
931 /* test end */
933 if (chip->nb_streams_capt) {
934 /* at boot time the digital volumes are unmuted 0dB */
935 chip->digital_capture_volume[i] = PCXHR_DIGITAL_ZERO_LEVEL;
936 /* only for test purpose, remove later */
937 #ifdef CONFIG_SND_DEBUG
938 /* analog volumes for playback (is LEVEL_MIN after boot) */
939 chip->analog_capture_volume[i] = PCXHR_ANALOG_CAPTURE_ZERO_LEVEL;
940 pcxhr_update_analog_audio_level(chip, 1, i);
941 #endif
942 /* test end */
946 return;
950 int pcxhr_create_mixer(struct pcxhr_mgr *mgr)
952 struct snd_pcxhr *chip;
953 int err, i;
955 mutex_init(&mgr->mixer_mutex); /* can be in another place */
957 for (i = 0; i < mgr->num_cards; i++) {
958 struct snd_kcontrol_new temp;
959 chip = mgr->chip[i];
961 if (chip->nb_streams_play) {
962 /* analog output level control */
963 temp = pcxhr_control_analog_level;
964 temp.name = "Master Playback Volume";
965 temp.private_value = 0; /* playback */
966 temp.tlv.p = db_scale_analog_playback;
967 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&temp, chip))) < 0)
968 return err;
969 /* output mute controls */
970 if ((err = snd_ctl_add(chip->card,
971 snd_ctl_new1(&pcxhr_control_output_switch,
972 chip))) < 0)
973 return err;
975 temp = snd_pcxhr_pcm_vol;
976 temp.name = "PCM Playback Volume";
977 temp.count = PCXHR_PLAYBACK_STREAMS;
978 temp.private_value = 0; /* playback */
979 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&temp, chip))) < 0)
980 return err;
982 if ((err = snd_ctl_add(chip->card,
983 snd_ctl_new1(&pcxhr_control_pcm_switch,
984 chip))) < 0)
985 return err;
987 /* IEC958 controls */
988 if ((err = snd_ctl_add(chip->card,
989 snd_ctl_new1(&pcxhr_control_playback_iec958_mask,
990 chip))) < 0)
991 return err;
992 if ((err = snd_ctl_add(chip->card,
993 snd_ctl_new1(&pcxhr_control_playback_iec958,
994 chip))) < 0)
995 return err;
997 if (chip->nb_streams_capt) {
998 /* analog input level control only on first two chips !*/
999 temp = pcxhr_control_analog_level;
1000 temp.name = "Master Capture Volume";
1001 temp.private_value = 1; /* capture */
1002 temp.tlv.p = db_scale_analog_capture;
1003 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&temp, chip))) < 0)
1004 return err;
1006 temp = snd_pcxhr_pcm_vol;
1007 temp.name = "PCM Capture Volume";
1008 temp.count = 1;
1009 temp.private_value = 1; /* capture */
1010 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&temp, chip))) < 0)
1011 return err;
1012 /* Audio source */
1013 if ((err = snd_ctl_add(chip->card,
1014 snd_ctl_new1(&pcxhr_control_audio_src,
1015 chip))) < 0)
1016 return err;
1017 /* IEC958 controls */
1018 if ((err = snd_ctl_add(chip->card,
1019 snd_ctl_new1(&pcxhr_control_capture_iec958_mask,
1020 chip))) < 0)
1021 return err;
1022 if ((err = snd_ctl_add(chip->card,
1023 snd_ctl_new1(&pcxhr_control_capture_iec958,
1024 chip))) < 0)
1025 return err;
1027 /* monitoring only if playback and capture device available */
1028 if (chip->nb_streams_capt > 0 && chip->nb_streams_play > 0) {
1029 /* monitoring */
1030 if ((err = snd_ctl_add(chip->card,
1031 snd_ctl_new1(&pcxhr_control_monitor_vol,
1032 chip))) < 0)
1033 return err;
1034 if ((err = snd_ctl_add(chip->card,
1035 snd_ctl_new1(&pcxhr_control_monitor_sw,
1036 chip))) < 0)
1037 return err;
1040 if (i == 0) {
1041 /* clock mode only one control per pcxhr */
1042 if ((err = snd_ctl_add(chip->card,
1043 snd_ctl_new1(&pcxhr_control_clock_type,
1044 mgr))) < 0)
1045 return err;
1046 /* non standard control used to scan the external clock presence/frequencies */
1047 if ((err = snd_ctl_add(chip->card,
1048 snd_ctl_new1(&pcxhr_control_clock_rate,
1049 mgr))) < 0)
1050 return err;
1053 /* init values for the mixer data */
1054 pcxhr_init_audio_levels(chip);
1057 return 0;