Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 #define __NO_VERSION__
0003 /*
0004  * Driver for Digigram pcxhr compatible soundcards
0005  *
0006  * mixer callbacks
0007  *
0008  * Copyright (c) 2004 by Digigram <alsa@digigram.com>
0009  */
0010 
0011 #include <linux/time.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/init.h>
0014 #include <linux/mutex.h>
0015 #include <sound/core.h>
0016 #include "pcxhr.h"
0017 #include "pcxhr_hwdep.h"
0018 #include "pcxhr_core.h"
0019 #include <sound/control.h>
0020 #include <sound/tlv.h>
0021 #include <sound/asoundef.h>
0022 #include "pcxhr_mixer.h"
0023 #include "pcxhr_mix22.h"
0024 
0025 #define PCXHR_LINE_CAPTURE_LEVEL_MIN   0    /* -112.0 dB */
0026 #define PCXHR_LINE_CAPTURE_LEVEL_MAX   255  /* +15.5 dB */
0027 #define PCXHR_LINE_CAPTURE_ZERO_LEVEL  224  /* 0.0 dB ( 0 dBu -> 0 dBFS ) */
0028 
0029 #define PCXHR_LINE_PLAYBACK_LEVEL_MIN  0    /* -104.0 dB */
0030 #define PCXHR_LINE_PLAYBACK_LEVEL_MAX  128  /* +24.0 dB */
0031 #define PCXHR_LINE_PLAYBACK_ZERO_LEVEL 104  /* 0.0 dB ( 0 dBFS -> 0 dBu ) */
0032 
0033 static const DECLARE_TLV_DB_SCALE(db_scale_analog_capture, -11200, 50, 1550);
0034 static const DECLARE_TLV_DB_SCALE(db_scale_analog_playback, -10400, 100, 2400);
0035 
0036 static const DECLARE_TLV_DB_SCALE(db_scale_a_hr222_capture, -11150, 50, 1600);
0037 static const DECLARE_TLV_DB_SCALE(db_scale_a_hr222_playback, -2550, 50, 2400);
0038 
0039 static int pcxhr_update_analog_audio_level(struct snd_pcxhr *chip,
0040                        int is_capture, int channel)
0041 {
0042     int err, vol;
0043     struct pcxhr_rmh rmh;
0044 
0045     pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
0046     if (is_capture) {
0047         rmh.cmd[0] |= IO_NUM_REG_IN_ANA_LEVEL;
0048         rmh.cmd[2] = chip->analog_capture_volume[channel];
0049     } else {
0050         rmh.cmd[0] |= IO_NUM_REG_OUT_ANA_LEVEL;
0051         if (chip->analog_playback_active[channel])
0052             vol = chip->analog_playback_volume[channel];
0053         else
0054             vol = PCXHR_LINE_PLAYBACK_LEVEL_MIN;
0055         /* playback analog levels are inversed */
0056         rmh.cmd[2] = PCXHR_LINE_PLAYBACK_LEVEL_MAX - vol;
0057     }
0058     rmh.cmd[1]  = 1 << ((2 * chip->chip_idx) + channel);    /* audio mask */
0059     rmh.cmd_len = 3;
0060     err = pcxhr_send_msg(chip->mgr, &rmh);
0061     if (err < 0) {
0062         dev_dbg(chip->card->dev,
0063             "error update_analog_audio_level card(%d)"
0064                " is_capture(%d) err(%x)\n",
0065                chip->chip_idx, is_capture, err);
0066         return -EINVAL;
0067     }
0068     return 0;
0069 }
0070 
0071 /*
0072  * analog level control
0073  */
0074 static int pcxhr_analog_vol_info(struct snd_kcontrol *kcontrol,
0075                  struct snd_ctl_elem_info *uinfo)
0076 {
0077     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0078 
0079     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0080     uinfo->count = 2;
0081     if (kcontrol->private_value == 0) { /* playback */
0082         if (chip->mgr->is_hr_stereo) {
0083         uinfo->value.integer.min =
0084             HR222_LINE_PLAYBACK_LEVEL_MIN;  /* -25 dB */
0085         uinfo->value.integer.max =
0086             HR222_LINE_PLAYBACK_LEVEL_MAX;  /* +24 dB */
0087         } else {
0088         uinfo->value.integer.min =
0089             PCXHR_LINE_PLAYBACK_LEVEL_MIN;  /*-104 dB */
0090         uinfo->value.integer.max =
0091             PCXHR_LINE_PLAYBACK_LEVEL_MAX;  /* +24 dB */
0092         }
0093     } else {                /* capture */
0094         if (chip->mgr->is_hr_stereo) {
0095         uinfo->value.integer.min =
0096             HR222_LINE_CAPTURE_LEVEL_MIN;   /*-112 dB */
0097         uinfo->value.integer.max =
0098             HR222_LINE_CAPTURE_LEVEL_MAX;   /* +15.5 dB */
0099         } else {
0100         uinfo->value.integer.min =
0101             PCXHR_LINE_CAPTURE_LEVEL_MIN;   /*-112 dB */
0102         uinfo->value.integer.max =
0103             PCXHR_LINE_CAPTURE_LEVEL_MAX;   /* +15.5 dB */
0104         }
0105     }
0106     return 0;
0107 }
0108 
0109 static int pcxhr_analog_vol_get(struct snd_kcontrol *kcontrol,
0110                 struct snd_ctl_elem_value *ucontrol)
0111 {
0112     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0113     mutex_lock(&chip->mgr->mixer_mutex);
0114     if (kcontrol->private_value == 0) { /* playback */
0115       ucontrol->value.integer.value[0] = chip->analog_playback_volume[0];
0116       ucontrol->value.integer.value[1] = chip->analog_playback_volume[1];
0117     } else {                /* capture */
0118       ucontrol->value.integer.value[0] = chip->analog_capture_volume[0];
0119       ucontrol->value.integer.value[1] = chip->analog_capture_volume[1];
0120     }
0121     mutex_unlock(&chip->mgr->mixer_mutex);
0122     return 0;
0123 }
0124 
0125 static int pcxhr_analog_vol_put(struct snd_kcontrol *kcontrol,
0126                 struct snd_ctl_elem_value *ucontrol)
0127 {
0128     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0129     int changed = 0;
0130     int is_capture, i;
0131 
0132     mutex_lock(&chip->mgr->mixer_mutex);
0133     is_capture = (kcontrol->private_value != 0);
0134     for (i = 0; i < 2; i++) {
0135         int  new_volume = ucontrol->value.integer.value[i];
0136         int *stored_volume = is_capture ?
0137             &chip->analog_capture_volume[i] :
0138             &chip->analog_playback_volume[i];
0139         if (is_capture) {
0140             if (chip->mgr->is_hr_stereo) {
0141                 if (new_volume < HR222_LINE_CAPTURE_LEVEL_MIN ||
0142                     new_volume > HR222_LINE_CAPTURE_LEVEL_MAX)
0143                     continue;
0144             } else {
0145                 if (new_volume < PCXHR_LINE_CAPTURE_LEVEL_MIN ||
0146                     new_volume > PCXHR_LINE_CAPTURE_LEVEL_MAX)
0147                     continue;
0148             }
0149         } else {
0150             if (chip->mgr->is_hr_stereo) {
0151                 if (new_volume < HR222_LINE_PLAYBACK_LEVEL_MIN ||
0152                     new_volume > HR222_LINE_PLAYBACK_LEVEL_MAX)
0153                     continue;
0154             } else {
0155                 if (new_volume < PCXHR_LINE_PLAYBACK_LEVEL_MIN ||
0156                     new_volume > PCXHR_LINE_PLAYBACK_LEVEL_MAX)
0157                     continue;
0158             }
0159         }
0160         if (*stored_volume != new_volume) {
0161             *stored_volume = new_volume;
0162             changed = 1;
0163             if (chip->mgr->is_hr_stereo)
0164                 hr222_update_analog_audio_level(chip,
0165                                 is_capture, i);
0166             else
0167                 pcxhr_update_analog_audio_level(chip,
0168                                 is_capture, i);
0169         }
0170     }
0171     mutex_unlock(&chip->mgr->mixer_mutex);
0172     return changed;
0173 }
0174 
0175 static const struct snd_kcontrol_new pcxhr_control_analog_level = {
0176     .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
0177     .access =   (SNDRV_CTL_ELEM_ACCESS_READWRITE |
0178              SNDRV_CTL_ELEM_ACCESS_TLV_READ),
0179     /* name will be filled later */
0180     .info =     pcxhr_analog_vol_info,
0181     .get =      pcxhr_analog_vol_get,
0182     .put =      pcxhr_analog_vol_put,
0183     /* tlv will be filled later */
0184 };
0185 
0186 /* shared */
0187 
0188 #define pcxhr_sw_info       snd_ctl_boolean_stereo_info
0189 
0190 static int pcxhr_audio_sw_get(struct snd_kcontrol *kcontrol,
0191                   struct snd_ctl_elem_value *ucontrol)
0192 {
0193     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0194 
0195     mutex_lock(&chip->mgr->mixer_mutex);
0196     ucontrol->value.integer.value[0] = chip->analog_playback_active[0];
0197     ucontrol->value.integer.value[1] = chip->analog_playback_active[1];
0198     mutex_unlock(&chip->mgr->mixer_mutex);
0199     return 0;
0200 }
0201 
0202 static int pcxhr_audio_sw_put(struct snd_kcontrol *kcontrol,
0203                   struct snd_ctl_elem_value *ucontrol)
0204 {
0205     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0206     int i, changed = 0;
0207     mutex_lock(&chip->mgr->mixer_mutex);
0208     for(i = 0; i < 2; i++) {
0209         if (chip->analog_playback_active[i] !=
0210             ucontrol->value.integer.value[i]) {
0211             chip->analog_playback_active[i] =
0212                 !!ucontrol->value.integer.value[i];
0213             changed = 1;
0214             /* update playback levels */
0215             if (chip->mgr->is_hr_stereo)
0216                 hr222_update_analog_audio_level(chip, 0, i);
0217             else
0218                 pcxhr_update_analog_audio_level(chip, 0, i);
0219         }
0220     }
0221     mutex_unlock(&chip->mgr->mixer_mutex);
0222     return changed;
0223 }
0224 
0225 static const struct snd_kcontrol_new pcxhr_control_output_switch = {
0226     .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
0227     .name =     "Master Playback Switch",
0228     .info =     pcxhr_sw_info,      /* shared */
0229     .get =      pcxhr_audio_sw_get,
0230     .put =      pcxhr_audio_sw_put
0231 };
0232 
0233 
0234 #define PCXHR_DIGITAL_LEVEL_MIN     0x000   /* -110 dB */
0235 #define PCXHR_DIGITAL_LEVEL_MAX     0x1ff   /* +18 dB */
0236 #define PCXHR_DIGITAL_ZERO_LEVEL    0x1b7   /*  0 dB */
0237 
0238 static const DECLARE_TLV_DB_SCALE(db_scale_digital, -10975, 25, 1800);
0239 
0240 #define MORE_THAN_ONE_STREAM_LEVEL  0x000001
0241 #define VALID_STREAM_PAN_LEVEL_MASK 0x800000
0242 #define VALID_STREAM_LEVEL_MASK     0x400000
0243 #define VALID_STREAM_LEVEL_1_MASK   0x200000
0244 #define VALID_STREAM_LEVEL_2_MASK   0x100000
0245 
0246 static int pcxhr_update_playback_stream_level(struct snd_pcxhr* chip, int idx)
0247 {
0248     int err;
0249     struct pcxhr_rmh rmh;
0250     struct pcxhr_pipe *pipe = &chip->playback_pipe;
0251     int left, right;
0252 
0253     if (chip->digital_playback_active[idx][0])
0254         left = chip->digital_playback_volume[idx][0];
0255     else
0256         left = PCXHR_DIGITAL_LEVEL_MIN;
0257     if (chip->digital_playback_active[idx][1])
0258         right = chip->digital_playback_volume[idx][1];
0259     else
0260         right = PCXHR_DIGITAL_LEVEL_MIN;
0261 
0262     pcxhr_init_rmh(&rmh, CMD_STREAM_OUT_LEVEL_ADJUST);
0263     /* add pipe and stream mask */
0264     pcxhr_set_pipe_cmd_params(&rmh, 0, pipe->first_audio, 0, 1<<idx);
0265     /* volume left->left / right->right panoramic level */
0266     rmh.cmd[0] |= MORE_THAN_ONE_STREAM_LEVEL;
0267     rmh.cmd[2]  = VALID_STREAM_PAN_LEVEL_MASK | VALID_STREAM_LEVEL_1_MASK;
0268     rmh.cmd[2] |= (left << 10);
0269     rmh.cmd[3]  = VALID_STREAM_PAN_LEVEL_MASK | VALID_STREAM_LEVEL_2_MASK;
0270     rmh.cmd[3] |= right;
0271     rmh.cmd_len = 4;
0272 
0273     err = pcxhr_send_msg(chip->mgr, &rmh);
0274     if (err < 0) {
0275         dev_dbg(chip->card->dev, "error update_playback_stream_level "
0276                "card(%d) err(%x)\n", chip->chip_idx, err);
0277         return -EINVAL;
0278     }
0279     return 0;
0280 }
0281 
0282 #define AUDIO_IO_HAS_MUTE_LEVEL     0x400000
0283 #define AUDIO_IO_HAS_MUTE_MONITOR_1 0x200000
0284 #define VALID_AUDIO_IO_DIGITAL_LEVEL    0x000001
0285 #define VALID_AUDIO_IO_MONITOR_LEVEL    0x000002
0286 #define VALID_AUDIO_IO_MUTE_LEVEL   0x000004
0287 #define VALID_AUDIO_IO_MUTE_MONITOR_1   0x000008
0288 
0289 static int pcxhr_update_audio_pipe_level(struct snd_pcxhr *chip,
0290                      int capture, int channel)
0291 {
0292     int err;
0293     struct pcxhr_rmh rmh;
0294     struct pcxhr_pipe *pipe;
0295 
0296     if (capture)
0297         pipe = &chip->capture_pipe[0];
0298     else
0299         pipe = &chip->playback_pipe;
0300 
0301     pcxhr_init_rmh(&rmh, CMD_AUDIO_LEVEL_ADJUST);
0302     /* add channel mask */
0303     pcxhr_set_pipe_cmd_params(&rmh, capture, 0, 0,
0304                   1 << (channel + pipe->first_audio));
0305     /* TODO : if mask (3 << pipe->first_audio) is used, left and right
0306      * channel will be programmed to the same params */
0307     if (capture) {
0308         rmh.cmd[0] |= VALID_AUDIO_IO_DIGITAL_LEVEL;
0309         /* VALID_AUDIO_IO_MUTE_LEVEL not yet handled
0310          * (capture pipe level) */
0311         rmh.cmd[2] = chip->digital_capture_volume[channel];
0312     } else {
0313         rmh.cmd[0] |=   VALID_AUDIO_IO_MONITOR_LEVEL |
0314                 VALID_AUDIO_IO_MUTE_MONITOR_1;
0315         /* VALID_AUDIO_IO_DIGITAL_LEVEL and VALID_AUDIO_IO_MUTE_LEVEL
0316          * not yet handled (playback pipe level)
0317          */
0318         rmh.cmd[2] = chip->monitoring_volume[channel] << 10;
0319         if (chip->monitoring_active[channel] == 0)
0320             rmh.cmd[2] |= AUDIO_IO_HAS_MUTE_MONITOR_1;
0321     }
0322     rmh.cmd_len = 3;
0323 
0324     err = pcxhr_send_msg(chip->mgr, &rmh);
0325     if (err < 0) {
0326         dev_dbg(chip->card->dev,
0327             "error update_audio_level(%d) err=%x\n",
0328                chip->chip_idx, err);
0329         return -EINVAL;
0330     }
0331     return 0;
0332 }
0333 
0334 
0335 /* shared */
0336 static int pcxhr_digital_vol_info(struct snd_kcontrol *kcontrol,
0337                   struct snd_ctl_elem_info *uinfo)
0338 {
0339     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0340     uinfo->count = 2;
0341     uinfo->value.integer.min = PCXHR_DIGITAL_LEVEL_MIN;   /* -109.5 dB */
0342     uinfo->value.integer.max = PCXHR_DIGITAL_LEVEL_MAX;   /*   18.0 dB */
0343     return 0;
0344 }
0345 
0346 
0347 static int pcxhr_pcm_vol_get(struct snd_kcontrol *kcontrol,
0348                  struct snd_ctl_elem_value *ucontrol)
0349 {
0350     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0351     int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
0352     int *stored_volume;
0353     int is_capture = kcontrol->private_value;
0354 
0355     mutex_lock(&chip->mgr->mixer_mutex);
0356     if (is_capture)     /* digital capture */
0357         stored_volume = chip->digital_capture_volume;
0358     else            /* digital playback */
0359         stored_volume = chip->digital_playback_volume[idx];
0360     ucontrol->value.integer.value[0] = stored_volume[0];
0361     ucontrol->value.integer.value[1] = stored_volume[1];
0362     mutex_unlock(&chip->mgr->mixer_mutex);
0363     return 0;
0364 }
0365 
0366 static int pcxhr_pcm_vol_put(struct snd_kcontrol *kcontrol,
0367                  struct snd_ctl_elem_value *ucontrol)
0368 {
0369     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0370     int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
0371     int changed = 0;
0372     int is_capture = kcontrol->private_value;
0373     int *stored_volume;
0374     int i;
0375 
0376     mutex_lock(&chip->mgr->mixer_mutex);
0377     if (is_capture)     /* digital capture */
0378         stored_volume = chip->digital_capture_volume;
0379     else            /* digital playback */
0380         stored_volume = chip->digital_playback_volume[idx];
0381     for (i = 0; i < 2; i++) {
0382         int vol = ucontrol->value.integer.value[i];
0383         if (vol < PCXHR_DIGITAL_LEVEL_MIN ||
0384             vol > PCXHR_DIGITAL_LEVEL_MAX)
0385             continue;
0386         if (stored_volume[i] != vol) {
0387             stored_volume[i] = vol;
0388             changed = 1;
0389             if (is_capture) /* update capture volume */
0390                 pcxhr_update_audio_pipe_level(chip, 1, i);
0391         }
0392     }
0393     if (!is_capture && changed) /* update playback volume */
0394         pcxhr_update_playback_stream_level(chip, idx);
0395     mutex_unlock(&chip->mgr->mixer_mutex);
0396     return changed;
0397 }
0398 
0399 static const struct snd_kcontrol_new snd_pcxhr_pcm_vol =
0400 {
0401     .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
0402     .access =   (SNDRV_CTL_ELEM_ACCESS_READWRITE |
0403              SNDRV_CTL_ELEM_ACCESS_TLV_READ),
0404     /* name will be filled later */
0405     /* count will be filled later */
0406     .info =     pcxhr_digital_vol_info,     /* shared */
0407     .get =      pcxhr_pcm_vol_get,
0408     .put =      pcxhr_pcm_vol_put,
0409     .tlv = { .p = db_scale_digital },
0410 };
0411 
0412 
0413 static int pcxhr_pcm_sw_get(struct snd_kcontrol *kcontrol,
0414                 struct snd_ctl_elem_value *ucontrol)
0415 {
0416     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0417     int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
0418 
0419     mutex_lock(&chip->mgr->mixer_mutex);
0420     ucontrol->value.integer.value[0] = chip->digital_playback_active[idx][0];
0421     ucontrol->value.integer.value[1] = chip->digital_playback_active[idx][1];
0422     mutex_unlock(&chip->mgr->mixer_mutex);
0423     return 0;
0424 }
0425 
0426 static int pcxhr_pcm_sw_put(struct snd_kcontrol *kcontrol,
0427                 struct snd_ctl_elem_value *ucontrol)
0428 {
0429     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0430     int changed = 0;
0431     int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
0432     int i, j;
0433 
0434     mutex_lock(&chip->mgr->mixer_mutex);
0435     j = idx;
0436     for (i = 0; i < 2; i++) {
0437         if (chip->digital_playback_active[j][i] !=
0438             ucontrol->value.integer.value[i]) {
0439             chip->digital_playback_active[j][i] =
0440                 !!ucontrol->value.integer.value[i];
0441             changed = 1;
0442         }
0443     }
0444     if (changed)
0445         pcxhr_update_playback_stream_level(chip, idx);
0446     mutex_unlock(&chip->mgr->mixer_mutex);
0447     return changed;
0448 }
0449 
0450 static const struct snd_kcontrol_new pcxhr_control_pcm_switch = {
0451     .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
0452     .name =     "PCM Playback Switch",
0453     .count =    PCXHR_PLAYBACK_STREAMS,
0454     .info =     pcxhr_sw_info,      /* shared */
0455     .get =      pcxhr_pcm_sw_get,
0456     .put =      pcxhr_pcm_sw_put
0457 };
0458 
0459 
0460 /*
0461  * monitoring level control
0462  */
0463 
0464 static int pcxhr_monitor_vol_get(struct snd_kcontrol *kcontrol,
0465                  struct snd_ctl_elem_value *ucontrol)
0466 {
0467     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0468     mutex_lock(&chip->mgr->mixer_mutex);
0469     ucontrol->value.integer.value[0] = chip->monitoring_volume[0];
0470     ucontrol->value.integer.value[1] = chip->monitoring_volume[1];
0471     mutex_unlock(&chip->mgr->mixer_mutex);
0472     return 0;
0473 }
0474 
0475 static int pcxhr_monitor_vol_put(struct snd_kcontrol *kcontrol,
0476                  struct snd_ctl_elem_value *ucontrol)
0477 {
0478     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0479     int changed = 0;
0480     int i;
0481 
0482     mutex_lock(&chip->mgr->mixer_mutex);
0483     for (i = 0; i < 2; i++) {
0484         if (chip->monitoring_volume[i] !=
0485             ucontrol->value.integer.value[i]) {
0486             chip->monitoring_volume[i] =
0487                 ucontrol->value.integer.value[i];
0488             if (chip->monitoring_active[i])
0489                 /* update monitoring volume and mute */
0490                 /* do only when monitoring is unmuted */
0491                 pcxhr_update_audio_pipe_level(chip, 0, i);
0492             changed = 1;
0493         }
0494     }
0495     mutex_unlock(&chip->mgr->mixer_mutex);
0496     return changed;
0497 }
0498 
0499 static const struct snd_kcontrol_new pcxhr_control_monitor_vol = {
0500     .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
0501     .access =   (SNDRV_CTL_ELEM_ACCESS_READWRITE |
0502              SNDRV_CTL_ELEM_ACCESS_TLV_READ),
0503     .name =         "Monitoring Playback Volume",
0504     .info =     pcxhr_digital_vol_info,     /* shared */
0505     .get =      pcxhr_monitor_vol_get,
0506     .put =      pcxhr_monitor_vol_put,
0507     .tlv = { .p = db_scale_digital },
0508 };
0509 
0510 /*
0511  * monitoring switch control
0512  */
0513 
0514 static int pcxhr_monitor_sw_get(struct snd_kcontrol *kcontrol,
0515                 struct snd_ctl_elem_value *ucontrol)
0516 {
0517     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0518     mutex_lock(&chip->mgr->mixer_mutex);
0519     ucontrol->value.integer.value[0] = chip->monitoring_active[0];
0520     ucontrol->value.integer.value[1] = chip->monitoring_active[1];
0521     mutex_unlock(&chip->mgr->mixer_mutex);
0522     return 0;
0523 }
0524 
0525 static int pcxhr_monitor_sw_put(struct snd_kcontrol *kcontrol,
0526                 struct snd_ctl_elem_value *ucontrol)
0527 {
0528     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0529     int changed = 0;
0530     int i;
0531 
0532     mutex_lock(&chip->mgr->mixer_mutex);
0533     for (i = 0; i < 2; i++) {
0534         if (chip->monitoring_active[i] !=
0535             ucontrol->value.integer.value[i]) {
0536             chip->monitoring_active[i] =
0537                 !!ucontrol->value.integer.value[i];
0538             changed |= (1<<i); /* mask 0x01 and 0x02 */
0539         }
0540     }
0541     if (changed & 0x01)
0542         /* update left monitoring volume and mute */
0543         pcxhr_update_audio_pipe_level(chip, 0, 0);
0544     if (changed & 0x02)
0545         /* update right monitoring volume and mute */
0546         pcxhr_update_audio_pipe_level(chip, 0, 1);
0547 
0548     mutex_unlock(&chip->mgr->mixer_mutex);
0549     return (changed != 0);
0550 }
0551 
0552 static const struct snd_kcontrol_new pcxhr_control_monitor_sw = {
0553     .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
0554     .name =         "Monitoring Playback Switch",
0555     .info =         pcxhr_sw_info,      /* shared */
0556     .get =          pcxhr_monitor_sw_get,
0557     .put =          pcxhr_monitor_sw_put
0558 };
0559 
0560 
0561 
0562 /*
0563  * audio source select
0564  */
0565 #define PCXHR_SOURCE_AUDIO01_UER    0x000100
0566 #define PCXHR_SOURCE_AUDIO01_SYNC   0x000200
0567 #define PCXHR_SOURCE_AUDIO23_UER    0x000400
0568 #define PCXHR_SOURCE_AUDIO45_UER    0x001000
0569 #define PCXHR_SOURCE_AUDIO67_UER    0x040000
0570 
0571 static int pcxhr_set_audio_source(struct snd_pcxhr* chip)
0572 {
0573     struct pcxhr_rmh rmh;
0574     unsigned int mask, reg;
0575     unsigned int codec;
0576     int err, changed;
0577 
0578     switch (chip->chip_idx) {
0579     case 0 : mask = PCXHR_SOURCE_AUDIO01_UER; codec = CS8420_01_CS; break;
0580     case 1 : mask = PCXHR_SOURCE_AUDIO23_UER; codec = CS8420_23_CS; break;
0581     case 2 : mask = PCXHR_SOURCE_AUDIO45_UER; codec = CS8420_45_CS; break;
0582     case 3 : mask = PCXHR_SOURCE_AUDIO67_UER; codec = CS8420_67_CS; break;
0583     default: return -EINVAL;
0584     }
0585     if (chip->audio_capture_source != 0) {
0586         reg = mask; /* audio source from digital plug */
0587     } else {
0588         reg = 0;    /* audio source from analog plug */
0589     }
0590     /* set the input source */
0591     pcxhr_write_io_num_reg_cont(chip->mgr, mask, reg, &changed);
0592     /* resync them (otherwise channel inversion possible) */
0593     if (changed) {
0594         pcxhr_init_rmh(&rmh, CMD_RESYNC_AUDIO_INPUTS);
0595         rmh.cmd[0] |= (1 << chip->chip_idx);
0596         err = pcxhr_send_msg(chip->mgr, &rmh);
0597         if (err)
0598             return err;
0599     }
0600     if (chip->mgr->board_aes_in_192k) {
0601         int i;
0602         unsigned int src_config = 0xC0;
0603         /* update all src configs with one call */
0604         for (i = 0; (i < 4) && (i < chip->mgr->capture_chips); i++) {
0605             if (chip->mgr->chip[i]->audio_capture_source == 2)
0606                 src_config |= (1 << (3 - i));
0607         }
0608         /* set codec SRC on off */
0609         pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
0610         rmh.cmd_len = 2;
0611         rmh.cmd[0] |= IO_NUM_REG_CONFIG_SRC;
0612         rmh.cmd[1] = src_config;
0613         err = pcxhr_send_msg(chip->mgr, &rmh);
0614     } else {
0615         int use_src = 0;
0616         if (chip->audio_capture_source == 2)
0617             use_src = 1;
0618         /* set codec SRC on off */
0619         pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
0620         rmh.cmd_len = 3;
0621         rmh.cmd[0] |= IO_NUM_UER_CHIP_REG;
0622         rmh.cmd[1] = codec;
0623         rmh.cmd[2] = ((CS8420_DATA_FLOW_CTL & CHIP_SIG_AND_MAP_SPI) |
0624                   (use_src ? 0x41 : 0x54));
0625         err = pcxhr_send_msg(chip->mgr, &rmh);
0626         if (err)
0627             return err;
0628         rmh.cmd[2] = ((CS8420_CLOCK_SRC_CTL & CHIP_SIG_AND_MAP_SPI) |
0629                   (use_src ? 0x41 : 0x49));
0630         err = pcxhr_send_msg(chip->mgr, &rmh);
0631     }
0632     return err;
0633 }
0634 
0635 static int pcxhr_audio_src_info(struct snd_kcontrol *kcontrol,
0636                 struct snd_ctl_elem_info *uinfo)
0637 {
0638     static const char *texts[5] = {
0639         "Line", "Digital", "Digi+SRC", "Mic", "Line+Mic"
0640     };
0641     int i;
0642     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0643 
0644     i = 2;          /* no SRC, no Mic available */
0645     if (chip->mgr->board_has_aes1) {
0646         i = 3;      /* SRC available */
0647         if (chip->mgr->board_has_mic)
0648             i = 5;  /* Mic and MicroMix available */
0649     }
0650     return snd_ctl_enum_info(uinfo, 1, i, texts);
0651 }
0652 
0653 static int pcxhr_audio_src_get(struct snd_kcontrol *kcontrol,
0654                    struct snd_ctl_elem_value *ucontrol)
0655 {
0656     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0657     ucontrol->value.enumerated.item[0] = chip->audio_capture_source;
0658     return 0;
0659 }
0660 
0661 static int pcxhr_audio_src_put(struct snd_kcontrol *kcontrol,
0662                    struct snd_ctl_elem_value *ucontrol)
0663 {
0664     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0665     int ret = 0;
0666     int i = 2;      /* no SRC, no Mic available */
0667     if (chip->mgr->board_has_aes1) {
0668         i = 3;      /* SRC available */
0669         if (chip->mgr->board_has_mic)
0670             i = 5;  /* Mic and MicroMix available */
0671     }
0672     if (ucontrol->value.enumerated.item[0] >= i)
0673         return -EINVAL;
0674     mutex_lock(&chip->mgr->mixer_mutex);
0675     if (chip->audio_capture_source != ucontrol->value.enumerated.item[0]) {
0676         chip->audio_capture_source = ucontrol->value.enumerated.item[0];
0677         if (chip->mgr->is_hr_stereo)
0678             hr222_set_audio_source(chip);
0679         else
0680             pcxhr_set_audio_source(chip);
0681         ret = 1;
0682     }
0683     mutex_unlock(&chip->mgr->mixer_mutex);
0684     return ret;
0685 }
0686 
0687 static const struct snd_kcontrol_new pcxhr_control_audio_src = {
0688     .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
0689     .name =     "Capture Source",
0690     .info =     pcxhr_audio_src_info,
0691     .get =      pcxhr_audio_src_get,
0692     .put =      pcxhr_audio_src_put,
0693 };
0694 
0695 
0696 /*
0697  * clock type selection
0698  * enum pcxhr_clock_type {
0699  *  PCXHR_CLOCK_TYPE_INTERNAL = 0,
0700  *  PCXHR_CLOCK_TYPE_WORD_CLOCK,
0701  *  PCXHR_CLOCK_TYPE_AES_SYNC,
0702  *  PCXHR_CLOCK_TYPE_AES_1,
0703  *  PCXHR_CLOCK_TYPE_AES_2,
0704  *  PCXHR_CLOCK_TYPE_AES_3,
0705  *  PCXHR_CLOCK_TYPE_AES_4,
0706  *  PCXHR_CLOCK_TYPE_MAX = PCXHR_CLOCK_TYPE_AES_4,
0707  *  HR22_CLOCK_TYPE_INTERNAL = PCXHR_CLOCK_TYPE_INTERNAL,
0708  *  HR22_CLOCK_TYPE_AES_SYNC,
0709  *  HR22_CLOCK_TYPE_AES_1,
0710  *  HR22_CLOCK_TYPE_MAX = HR22_CLOCK_TYPE_AES_1,
0711  * };
0712  */
0713 
0714 static int pcxhr_clock_type_info(struct snd_kcontrol *kcontrol,
0715                  struct snd_ctl_elem_info *uinfo)
0716 {
0717     static const char *textsPCXHR[7] = {
0718         "Internal", "WordClock", "AES Sync",
0719         "AES 1", "AES 2", "AES 3", "AES 4"
0720     };
0721     static const char *textsHR22[3] = {
0722         "Internal", "AES Sync", "AES 1"
0723     };
0724     const char **texts;
0725     struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
0726     int clock_items = 2;    /* at least Internal and AES Sync clock */
0727     if (mgr->board_has_aes1) {
0728         clock_items += mgr->capture_chips;  /* add AES x */
0729         if (!mgr->is_hr_stereo)
0730             clock_items += 1;       /* add word clock */
0731     }
0732     if (mgr->is_hr_stereo) {
0733         texts = textsHR22;
0734         snd_BUG_ON(clock_items > (HR22_CLOCK_TYPE_MAX+1));
0735     } else {
0736         texts = textsPCXHR;
0737         snd_BUG_ON(clock_items > (PCXHR_CLOCK_TYPE_MAX+1));
0738     }
0739     return snd_ctl_enum_info(uinfo, 1, clock_items, texts);
0740 }
0741 
0742 static int pcxhr_clock_type_get(struct snd_kcontrol *kcontrol,
0743                 struct snd_ctl_elem_value *ucontrol)
0744 {
0745     struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
0746     ucontrol->value.enumerated.item[0] = mgr->use_clock_type;
0747     return 0;
0748 }
0749 
0750 static int pcxhr_clock_type_put(struct snd_kcontrol *kcontrol,
0751                 struct snd_ctl_elem_value *ucontrol)
0752 {
0753     struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
0754     int rate, ret = 0;
0755     unsigned int clock_items = 2; /* at least Internal and AES Sync clock */
0756     if (mgr->board_has_aes1) {
0757         clock_items += mgr->capture_chips;  /* add AES x */
0758         if (!mgr->is_hr_stereo)
0759             clock_items += 1;       /* add word clock */
0760     }
0761     if (ucontrol->value.enumerated.item[0] >= clock_items)
0762         return -EINVAL;
0763     mutex_lock(&mgr->mixer_mutex);
0764     if (mgr->use_clock_type != ucontrol->value.enumerated.item[0]) {
0765         mutex_lock(&mgr->setup_mutex);
0766         mgr->use_clock_type = ucontrol->value.enumerated.item[0];
0767         rate = 0;
0768         if (mgr->use_clock_type != PCXHR_CLOCK_TYPE_INTERNAL) {
0769             pcxhr_get_external_clock(mgr, mgr->use_clock_type,
0770                          &rate);
0771         } else {
0772             rate = mgr->sample_rate;
0773             if (!rate)
0774                 rate = 48000;
0775         }
0776         if (rate) {
0777             pcxhr_set_clock(mgr, rate);
0778             if (mgr->sample_rate)
0779                 mgr->sample_rate = rate;
0780         }
0781         mutex_unlock(&mgr->setup_mutex);
0782         ret = 1; /* return 1 even if the set was not done. ok ? */
0783     }
0784     mutex_unlock(&mgr->mixer_mutex);
0785     return ret;
0786 }
0787 
0788 static const struct snd_kcontrol_new pcxhr_control_clock_type = {
0789     .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
0790     .name =     "Clock Mode",
0791     .info =     pcxhr_clock_type_info,
0792     .get =      pcxhr_clock_type_get,
0793     .put =      pcxhr_clock_type_put,
0794 };
0795 
0796 /*
0797  * clock rate control
0798  * specific control that scans the sample rates on the external plugs
0799  */
0800 static int pcxhr_clock_rate_info(struct snd_kcontrol *kcontrol,
0801                  struct snd_ctl_elem_info *uinfo)
0802 {
0803     struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
0804     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0805     uinfo->count = 3 + mgr->capture_chips;
0806     uinfo->value.integer.min = 0;       /* clock not present */
0807     uinfo->value.integer.max = 192000;  /* max sample rate 192 kHz */
0808     return 0;
0809 }
0810 
0811 static int pcxhr_clock_rate_get(struct snd_kcontrol *kcontrol,
0812                 struct snd_ctl_elem_value *ucontrol)
0813 {
0814     struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
0815     int i, err, rate;
0816 
0817     mutex_lock(&mgr->mixer_mutex);
0818     for(i = 0; i < 3 + mgr->capture_chips; i++) {
0819         if (i == PCXHR_CLOCK_TYPE_INTERNAL)
0820             rate = mgr->sample_rate_real;
0821         else {
0822             err = pcxhr_get_external_clock(mgr, i, &rate);
0823             if (err)
0824                 break;
0825         }
0826         ucontrol->value.integer.value[i] = rate;
0827     }
0828     mutex_unlock(&mgr->mixer_mutex);
0829     return 0;
0830 }
0831 
0832 static const struct snd_kcontrol_new pcxhr_control_clock_rate = {
0833     .access =   SNDRV_CTL_ELEM_ACCESS_READ,
0834     .iface =    SNDRV_CTL_ELEM_IFACE_CARD,
0835     .name =     "Clock Rates",
0836     .info =     pcxhr_clock_rate_info,
0837     .get =      pcxhr_clock_rate_get,
0838 };
0839 
0840 /*
0841  * IEC958 status bits
0842  */
0843 static int pcxhr_iec958_info(struct snd_kcontrol *kcontrol,
0844                  struct snd_ctl_elem_info *uinfo)
0845 {
0846     uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
0847     uinfo->count = 1;
0848     return 0;
0849 }
0850 
0851 static int pcxhr_iec958_capture_byte(struct snd_pcxhr *chip,
0852                      int aes_idx, unsigned char *aes_bits)
0853 {
0854     int i, err;
0855     unsigned char temp;
0856     struct pcxhr_rmh rmh;
0857 
0858     pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ);
0859     rmh.cmd[0] |= IO_NUM_UER_CHIP_REG;
0860     switch (chip->chip_idx) {
0861       /* instead of CS8420_01_CS use CS8416_01_CS for AES SYNC plug */
0862     case 0: rmh.cmd[1] = CS8420_01_CS; break;
0863     case 1: rmh.cmd[1] = CS8420_23_CS; break;
0864     case 2: rmh.cmd[1] = CS8420_45_CS; break;
0865     case 3: rmh.cmd[1] = CS8420_67_CS; break;
0866     default: return -EINVAL;
0867     }
0868     if (chip->mgr->board_aes_in_192k) {
0869         switch (aes_idx) {
0870         case 0: rmh.cmd[2] = CS8416_CSB0; break;
0871         case 1: rmh.cmd[2] = CS8416_CSB1; break;
0872         case 2: rmh.cmd[2] = CS8416_CSB2; break;
0873         case 3: rmh.cmd[2] = CS8416_CSB3; break;
0874         case 4: rmh.cmd[2] = CS8416_CSB4; break;
0875         default: return -EINVAL;
0876         }
0877     } else {
0878         switch (aes_idx) {
0879           /* instead of CS8420_CSB0 use CS8416_CSBx for AES SYNC plug */
0880         case 0: rmh.cmd[2] = CS8420_CSB0; break;
0881         case 1: rmh.cmd[2] = CS8420_CSB1; break;
0882         case 2: rmh.cmd[2] = CS8420_CSB2; break;
0883         case 3: rmh.cmd[2] = CS8420_CSB3; break;
0884         case 4: rmh.cmd[2] = CS8420_CSB4; break;
0885         default: return -EINVAL;
0886         }
0887     }
0888     /* size and code the chip id for the fpga */
0889     rmh.cmd[1] &= 0x0fffff;
0890     /* chip signature + map for spi read */
0891     rmh.cmd[2] &= CHIP_SIG_AND_MAP_SPI;
0892     rmh.cmd_len = 3;
0893     err = pcxhr_send_msg(chip->mgr, &rmh);
0894     if (err)
0895         return err;
0896 
0897     if (chip->mgr->board_aes_in_192k) {
0898         temp = (unsigned char)rmh.stat[1];
0899     } else {
0900         temp = 0;
0901         /* reversed bit order (not with CS8416_01_CS) */
0902         for (i = 0; i < 8; i++) {
0903             temp <<= 1;
0904             if (rmh.stat[1] & (1 << i))
0905                 temp |= 1;
0906         }
0907     }
0908     dev_dbg(chip->card->dev, "read iec958 AES %d byte %d = 0x%x\n",
0909             chip->chip_idx, aes_idx, temp);
0910     *aes_bits = temp;
0911     return 0;
0912 }
0913 
0914 static int pcxhr_iec958_get(struct snd_kcontrol *kcontrol,
0915                 struct snd_ctl_elem_value *ucontrol)
0916 {
0917     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0918     unsigned char aes_bits;
0919     int i, err;
0920 
0921     mutex_lock(&chip->mgr->mixer_mutex);
0922     for(i = 0; i < 5; i++) {
0923         if (kcontrol->private_value == 0)   /* playback */
0924             aes_bits = chip->aes_bits[i];
0925         else {              /* capture */
0926             if (chip->mgr->is_hr_stereo)
0927                 err = hr222_iec958_capture_byte(chip, i,
0928                                 &aes_bits);
0929             else
0930                 err = pcxhr_iec958_capture_byte(chip, i,
0931                                 &aes_bits);
0932             if (err)
0933                 break;
0934         }
0935         ucontrol->value.iec958.status[i] = aes_bits;
0936     }
0937     mutex_unlock(&chip->mgr->mixer_mutex);
0938         return 0;
0939 }
0940 
0941 static int pcxhr_iec958_mask_get(struct snd_kcontrol *kcontrol,
0942                  struct snd_ctl_elem_value *ucontrol)
0943 {
0944     int i;
0945     for (i = 0; i < 5; i++)
0946         ucontrol->value.iec958.status[i] = 0xff;
0947         return 0;
0948 }
0949 
0950 static int pcxhr_iec958_update_byte(struct snd_pcxhr *chip,
0951                     int aes_idx, unsigned char aes_bits)
0952 {
0953     int i, err, cmd;
0954     unsigned char new_bits = aes_bits;
0955     unsigned char old_bits = chip->aes_bits[aes_idx];
0956     struct pcxhr_rmh rmh;
0957 
0958     for (i = 0; i < 8; i++) {
0959         if ((old_bits & 0x01) != (new_bits & 0x01)) {
0960             cmd = chip->chip_idx & 0x03;      /* chip index 0..3 */
0961             if (chip->chip_idx > 3)
0962                 /* new bit used if chip_idx>3 (PCX1222HR) */
0963                 cmd |= 1 << 22;
0964             cmd |= ((aes_idx << 3) + i) << 2; /* add bit offset */
0965             cmd |= (new_bits & 0x01) << 23;   /* add bit value */
0966             pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
0967             rmh.cmd[0] |= IO_NUM_REG_CUER;
0968             rmh.cmd[1] = cmd;
0969             rmh.cmd_len = 2;
0970             dev_dbg(chip->card->dev,
0971                 "write iec958 AES %d byte %d bit %d (cmd %x)\n",
0972                     chip->chip_idx, aes_idx, i, cmd);
0973             err = pcxhr_send_msg(chip->mgr, &rmh);
0974             if (err)
0975                 return err;
0976         }
0977         old_bits >>= 1;
0978         new_bits >>= 1;
0979     }
0980     chip->aes_bits[aes_idx] = aes_bits;
0981     return 0;
0982 }
0983 
0984 static int pcxhr_iec958_put(struct snd_kcontrol *kcontrol,
0985                 struct snd_ctl_elem_value *ucontrol)
0986 {
0987     struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
0988     int i, changed = 0;
0989 
0990     /* playback */
0991     mutex_lock(&chip->mgr->mixer_mutex);
0992     for (i = 0; i < 5; i++) {
0993         if (ucontrol->value.iec958.status[i] != chip->aes_bits[i]) {
0994             if (chip->mgr->is_hr_stereo)
0995                 hr222_iec958_update_byte(chip, i,
0996                     ucontrol->value.iec958.status[i]);
0997             else
0998                 pcxhr_iec958_update_byte(chip, i,
0999                     ucontrol->value.iec958.status[i]);
1000             changed = 1;
1001         }
1002     }
1003     mutex_unlock(&chip->mgr->mixer_mutex);
1004     return changed;
1005 }
1006 
1007 static const struct snd_kcontrol_new pcxhr_control_playback_iec958_mask = {
1008     .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1009     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1010     .name =     SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1011     .info =     pcxhr_iec958_info,
1012     .get =      pcxhr_iec958_mask_get
1013 };
1014 static const struct snd_kcontrol_new pcxhr_control_playback_iec958 = {
1015     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1016     .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1017     .info =         pcxhr_iec958_info,
1018     .get =          pcxhr_iec958_get,
1019     .put =          pcxhr_iec958_put,
1020     .private_value = 0 /* playback */
1021 };
1022 
1023 static const struct snd_kcontrol_new pcxhr_control_capture_iec958_mask = {
1024     .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1025     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1026     .name =     SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK),
1027     .info =     pcxhr_iec958_info,
1028     .get =      pcxhr_iec958_mask_get
1029 };
1030 static const struct snd_kcontrol_new pcxhr_control_capture_iec958 = {
1031     .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1032     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1033     .name =         SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1034     .info =         pcxhr_iec958_info,
1035     .get =          pcxhr_iec958_get,
1036     .private_value = 1 /* capture */
1037 };
1038 
1039 static void pcxhr_init_audio_levels(struct snd_pcxhr *chip)
1040 {
1041     int i;
1042 
1043     for (i = 0; i < 2; i++) {
1044         if (chip->nb_streams_play) {
1045             int j;
1046             /* at boot time the digital volumes are unmuted 0dB */
1047             for (j = 0; j < PCXHR_PLAYBACK_STREAMS; j++) {
1048                 chip->digital_playback_active[j][i] = 1;
1049                 chip->digital_playback_volume[j][i] =
1050                     PCXHR_DIGITAL_ZERO_LEVEL;
1051             }
1052             /* after boot, only two bits are set on the uer
1053              * interface
1054              */
1055             chip->aes_bits[0] = (IEC958_AES0_PROFESSIONAL |
1056                          IEC958_AES0_PRO_FS_48000);
1057 #ifdef CONFIG_SND_DEBUG
1058             /* analog volumes for playback
1059              * (is LEVEL_MIN after boot)
1060              */
1061             chip->analog_playback_active[i] = 1;
1062             if (chip->mgr->is_hr_stereo)
1063                 chip->analog_playback_volume[i] =
1064                     HR222_LINE_PLAYBACK_ZERO_LEVEL;
1065             else {
1066                 chip->analog_playback_volume[i] =
1067                     PCXHR_LINE_PLAYBACK_ZERO_LEVEL;
1068                 pcxhr_update_analog_audio_level(chip, 0, i);
1069             }
1070 #endif
1071             /* stereo cards need to be initialised after boot */
1072             if (chip->mgr->is_hr_stereo)
1073                 hr222_update_analog_audio_level(chip, 0, i);
1074         }
1075         if (chip->nb_streams_capt) {
1076             /* at boot time the digital volumes are unmuted 0dB */
1077             chip->digital_capture_volume[i] =
1078                 PCXHR_DIGITAL_ZERO_LEVEL;
1079             chip->analog_capture_active = 1;
1080 #ifdef CONFIG_SND_DEBUG
1081             /* analog volumes for playback
1082              * (is LEVEL_MIN after boot)
1083              */
1084             if (chip->mgr->is_hr_stereo)
1085                 chip->analog_capture_volume[i] =
1086                     HR222_LINE_CAPTURE_ZERO_LEVEL;
1087             else {
1088                 chip->analog_capture_volume[i] =
1089                     PCXHR_LINE_CAPTURE_ZERO_LEVEL;
1090                 pcxhr_update_analog_audio_level(chip, 1, i);
1091             }
1092 #endif
1093             /* stereo cards need to be initialised after boot */
1094             if (chip->mgr->is_hr_stereo)
1095                 hr222_update_analog_audio_level(chip, 1, i);
1096         }
1097     }
1098 
1099     return;
1100 }
1101 
1102 
1103 int pcxhr_create_mixer(struct pcxhr_mgr *mgr)
1104 {
1105     struct snd_pcxhr *chip;
1106     int err, i;
1107 
1108     mutex_init(&mgr->mixer_mutex); /* can be in another place */
1109 
1110     for (i = 0; i < mgr->num_cards; i++) {
1111         struct snd_kcontrol_new temp;
1112         chip = mgr->chip[i];
1113 
1114         if (chip->nb_streams_play) {
1115             /* analog output level control */
1116             temp = pcxhr_control_analog_level;
1117             temp.name = "Master Playback Volume";
1118             temp.private_value = 0; /* playback */
1119             if (mgr->is_hr_stereo)
1120                 temp.tlv.p = db_scale_a_hr222_playback;
1121             else
1122                 temp.tlv.p = db_scale_analog_playback;
1123             err = snd_ctl_add(chip->card,
1124                       snd_ctl_new1(&temp, chip));
1125             if (err < 0)
1126                 return err;
1127 
1128             /* output mute controls */
1129             err = snd_ctl_add(chip->card,
1130                 snd_ctl_new1(&pcxhr_control_output_switch,
1131                          chip));
1132             if (err < 0)
1133                 return err;
1134 
1135             temp = snd_pcxhr_pcm_vol;
1136             temp.name = "PCM Playback Volume";
1137             temp.count = PCXHR_PLAYBACK_STREAMS;
1138             temp.private_value = 0; /* playback */
1139             err = snd_ctl_add(chip->card,
1140                       snd_ctl_new1(&temp, chip));
1141             if (err < 0)
1142                 return err;
1143 
1144             err = snd_ctl_add(chip->card,
1145                 snd_ctl_new1(&pcxhr_control_pcm_switch, chip));
1146             if (err < 0)
1147                 return err;
1148 
1149             /* IEC958 controls */
1150             err = snd_ctl_add(chip->card,
1151                 snd_ctl_new1(&pcxhr_control_playback_iec958_mask,
1152                          chip));
1153             if (err < 0)
1154                 return err;
1155 
1156             err = snd_ctl_add(chip->card,
1157                 snd_ctl_new1(&pcxhr_control_playback_iec958,
1158                          chip));
1159             if (err < 0)
1160                 return err;
1161         }
1162         if (chip->nb_streams_capt) {
1163             /* analog input level control */
1164             temp = pcxhr_control_analog_level;
1165             temp.name = "Line Capture Volume";
1166             temp.private_value = 1; /* capture */
1167             if (mgr->is_hr_stereo)
1168                 temp.tlv.p = db_scale_a_hr222_capture;
1169             else
1170                 temp.tlv.p = db_scale_analog_capture;
1171 
1172             err = snd_ctl_add(chip->card,
1173                       snd_ctl_new1(&temp, chip));
1174             if (err < 0)
1175                 return err;
1176 
1177             temp = snd_pcxhr_pcm_vol;
1178             temp.name = "PCM Capture Volume";
1179             temp.count = 1;
1180             temp.private_value = 1; /* capture */
1181 
1182             err = snd_ctl_add(chip->card,
1183                       snd_ctl_new1(&temp, chip));
1184             if (err < 0)
1185                 return err;
1186 
1187             /* Audio source */
1188             err = snd_ctl_add(chip->card,
1189                 snd_ctl_new1(&pcxhr_control_audio_src, chip));
1190             if (err < 0)
1191                 return err;
1192 
1193             /* IEC958 controls */
1194             err = snd_ctl_add(chip->card,
1195                 snd_ctl_new1(&pcxhr_control_capture_iec958_mask,
1196                          chip));
1197             if (err < 0)
1198                 return err;
1199 
1200             err = snd_ctl_add(chip->card,
1201                 snd_ctl_new1(&pcxhr_control_capture_iec958,
1202                          chip));
1203             if (err < 0)
1204                 return err;
1205 
1206             if (mgr->is_hr_stereo) {
1207                 err = hr222_add_mic_controls(chip);
1208                 if (err < 0)
1209                     return err;
1210             }
1211         }
1212         /* monitoring only if playback and capture device available */
1213         if (chip->nb_streams_capt > 0 && chip->nb_streams_play > 0) {
1214             /* monitoring */
1215             err = snd_ctl_add(chip->card,
1216                 snd_ctl_new1(&pcxhr_control_monitor_vol, chip));
1217             if (err < 0)
1218                 return err;
1219 
1220             err = snd_ctl_add(chip->card,
1221                 snd_ctl_new1(&pcxhr_control_monitor_sw, chip));
1222             if (err < 0)
1223                 return err;
1224         }
1225 
1226         if (i == 0) {
1227             /* clock mode only one control per pcxhr */
1228             err = snd_ctl_add(chip->card,
1229                 snd_ctl_new1(&pcxhr_control_clock_type, mgr));
1230             if (err < 0)
1231                 return err;
1232             /* non standard control used to scan
1233              * the external clock presence/frequencies
1234              */
1235             err = snd_ctl_add(chip->card,
1236                 snd_ctl_new1(&pcxhr_control_clock_rate, mgr));
1237             if (err < 0)
1238                 return err;
1239         }
1240 
1241         /* init values for the mixer data */
1242         pcxhr_init_audio_levels(chip);
1243     }
1244 
1245     return 0;
1246 }