3 * Driver for Digigram pcxhr compatible soundcards
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>
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)
54 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
56 rmh.cmd[0] |= IO_NUM_REG_IN_ANA_LEVEL;
57 rmh.cmd[2] = chip->analog_capture_volume[channel];
59 rmh.cmd[0] |= IO_NUM_REG_OUT_ANA_LEVEL;
60 if (chip->analog_playback_active[channel])
61 vol = chip->analog_playback_volume[channel];
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 */
68 err = pcxhr_send_msg(chip->mgr, &rmh);
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);
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;
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 */
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);
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);
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];
126 if (new_volume < PCXHR_ANALOG_CAPTURE_LEVEL_MIN ||
127 new_volume > PCXHR_ANALOG_CAPTURE_LEVEL_MAX)
130 if (new_volume < PCXHR_ANALOG_PLAYBACK_LEVEL_MIN ||
131 new_volume > PCXHR_ANALOG_PLAYBACK_LEVEL_MAX)
134 if (*stored_volume != new_volume) {
135 *stored_volume = new_volume;
137 pcxhr_update_analog_audio_level(chip, is_capture, i);
140 mutex_unlock(&chip->mgr->mixer_mutex);
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 */
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);
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);
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];
182 /* update playback levels */
183 pcxhr_update_analog_audio_level(chip, 0, i);
186 mutex_unlock(&chip->mgr->mixer_mutex);
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)
214 struct pcxhr_rmh rmh;
215 struct pcxhr_pipe *pipe = &chip->playback_pipe;
218 if (chip->digital_playback_active[idx][0])
219 left = chip->digital_playback_volume[idx][0];
221 left = PCXHR_DIGITAL_LEVEL_MIN;
222 if (chip->digital_playback_active[idx][1])
223 right = chip->digital_playback_volume[idx][1];
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;
238 err = pcxhr_send_msg(chip->mgr, &rmh);
240 snd_printk(KERN_DEBUG "error update_playback_stream_level "
241 "card(%d) err(%x)\n", chip->chip_idx, err);
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)
257 struct pcxhr_rmh rmh;
258 struct pcxhr_pipe *pipe;
261 pipe = &chip->capture_pipe[0];
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
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];
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;
286 err = pcxhr_send_msg(chip->mgr, &rmh);
288 snd_printk(KERN_DEBUG "error update_audio_level card(%d) err(%x)\n",
289 chip->chip_idx, err);
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;
302 uinfo->value.integer.min = PCXHR_DIGITAL_LEVEL_MIN; /* -109.5 dB */
303 uinfo->value.integer.max = PCXHR_DIGITAL_LEVEL_MAX; /* 18.0 dB */
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 */
314 int is_capture = kcontrol->private_value;
316 mutex_lock(&chip->mgr->mixer_mutex);
318 stored_volume = chip->digital_capture_volume; /* digital capture */
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);
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 */
333 int is_capture = kcontrol->private_value;
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)
347 if (stored_volume[i] != vol) {
348 stored_volume[i] = vol;
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);
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);
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);
391 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
394 mutex_lock(&chip->mgr->mixer_mutex);
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];
405 pcxhr_update_playback_stream_level(chip, idx);
406 mutex_unlock(&chip->mgr->mixer_mutex);
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);
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);
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);
455 mutex_unlock(&chip->mgr->mixer_mutex);
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);
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);
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 */
502 /* update left monitoring volume and mute */
503 pcxhr_update_audio_pipe_level(chip, 0, 0);
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;
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)
553 /* set the input source */
554 pcxhr_write_io_num_reg_cont(chip->mgr, mask, reg, &changed);
555 /* resync them (otherwise channel inversion possible) */
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);
563 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE); /* set codec SRC on off */
565 rmh.cmd[0] |= IO_NUM_UER_CHIP_REG;
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);
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);
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;
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]);
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;
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);
605 if (ucontrol->value.enumerated.item[0] >= 3)
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);
613 mutex_unlock(&chip->mgr->mixer_mutex);
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,
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;
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]);
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;
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;
673 if (ucontrol->value.enumerated.item[0] >= clock_items)
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);
682 rate = mgr->sample_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);
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,
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 */
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);
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;
729 err = pcxhr_get_external_clock(mgr, i, &rate);
733 ucontrol->value.integer.value[i] = rate;
735 mutex_unlock(&mgr->mixer_mutex);
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,
750 static int pcxhr_iec958_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
752 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
757 static int pcxhr_iec958_capture_byte(struct snd_pcxhr *chip, int aes_idx, unsigned char* aes_bits)
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;
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 */
783 err = pcxhr_send_msg(chip->mgr, &rmh);
787 for (i = 0; i < 8; i++) {
788 /* attention : reversed bit order (not with CS8416_01_CS) */
790 if (rmh.stat[1] & (1 << i))
793 snd_printdd("read iec958 AES %d byte %d = 0x%x\n", chip->chip_idx, aes_idx, temp);
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;
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];
809 err = pcxhr_iec958_capture_byte(chip, i, &aes_bits);
813 ucontrol->value.iec958.status[i] = aes_bits;
815 mutex_unlock(&chip->mgr->mixer_mutex);
819 static int pcxhr_iec958_mask_get(struct snd_kcontrol *kcontrol,
820 struct snd_ctl_elem_value *ucontrol)
823 for (i = 0; i < 5; i++)
824 ucontrol->value.iec958.status[i] = 0xff;
828 static int pcxhr_iec958_update_byte(struct snd_pcxhr *chip, int aes_idx, unsigned char aes_bits)
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) */
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;
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);
856 chip->aes_bits[aes_idx] = aes_bits;
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);
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]);
874 mutex_unlock(&chip->mgr->mixer_mutex);
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)
914 for (i = 0; i < 2; i++) {
915 if (chip->nb_streams_play) {
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);
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);
950 int pcxhr_create_mixer(struct pcxhr_mgr *mgr)
952 struct snd_pcxhr *chip;
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;
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)
969 /* output mute controls */
970 if ((err = snd_ctl_add(chip->card,
971 snd_ctl_new1(&pcxhr_control_output_switch,
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)
982 if ((err = snd_ctl_add(chip->card,
983 snd_ctl_new1(&pcxhr_control_pcm_switch,
987 /* IEC958 controls */
988 if ((err = snd_ctl_add(chip->card,
989 snd_ctl_new1(&pcxhr_control_playback_iec958_mask,
992 if ((err = snd_ctl_add(chip->card,
993 snd_ctl_new1(&pcxhr_control_playback_iec958,
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)
1006 temp = snd_pcxhr_pcm_vol;
1007 temp.name = "PCM Capture Volume";
1009 temp.private_value = 1; /* capture */
1010 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&temp, chip))) < 0)
1013 if ((err = snd_ctl_add(chip->card,
1014 snd_ctl_new1(&pcxhr_control_audio_src,
1017 /* IEC958 controls */
1018 if ((err = snd_ctl_add(chip->card,
1019 snd_ctl_new1(&pcxhr_control_capture_iec958_mask,
1022 if ((err = snd_ctl_add(chip->card,
1023 snd_ctl_new1(&pcxhr_control_capture_iec958,
1027 /* monitoring only if playback and capture device available */
1028 if (chip->nb_streams_capt > 0 && chip->nb_streams_play > 0) {
1030 if ((err = snd_ctl_add(chip->card,
1031 snd_ctl_new1(&pcxhr_control_monitor_vol,
1034 if ((err = snd_ctl_add(chip->card,
1035 snd_ctl_new1(&pcxhr_control_monitor_sw,
1041 /* clock mode only one control per pcxhr */
1042 if ((err = snd_ctl_add(chip->card,
1043 snd_ctl_new1(&pcxhr_control_clock_type,
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,
1053 /* init values for the mixer data */
1054 pcxhr_init_audio_levels(chip);