ALSA: hda - Fix input pin initialization for STAC/IDT codecs
[linux-2.6] / sound / pci / pcxhr / pcxhr_mixer.c
1 #define __NO_VERSION__
2 /*
3  * Driver for Digigram pcxhr compatible soundcards
4  *
5  * mixer callbacks
6  *
7  * Copyright (c) 2004 by Digigram <alsa@digigram.com>
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  */
23
24 #include <linux/time.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/mutex.h>
28 #include <sound/core.h>
29 #include "pcxhr.h"
30 #include "pcxhr_hwdep.h"
31 #include "pcxhr_core.h"
32 #include <sound/control.h>
33 #include <sound/tlv.h>
34 #include <sound/asoundef.h>
35 #include "pcxhr_mixer.h"
36
37
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 ) */
41
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 ) */
45
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);
48
49 static int pcxhr_update_analog_audio_level(struct snd_pcxhr *chip, int is_capture, int channel)
50 {
51         int err, vol;
52         struct pcxhr_rmh rmh;
53
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 */
65         }
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;
73         }
74         return 0;
75 }
76
77 /*
78  * analog level control
79  */
80 static int pcxhr_analog_vol_info(struct snd_kcontrol *kcontrol,
81                                  struct snd_ctl_elem_info *uinfo)
82 {
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 */
91         }
92         return 0;
93 }
94
95 static int pcxhr_analog_vol_get(struct snd_kcontrol *kcontrol,
96                                 struct snd_ctl_elem_value *ucontrol)
97 {
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];
106         }
107         mutex_unlock(&chip->mgr->mixer_mutex);
108         return 0;
109 }
110
111 static int pcxhr_analog_vol_put(struct snd_kcontrol *kcontrol,
112                                 struct snd_ctl_elem_value *ucontrol)
113 {
114         struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
115         int changed = 0;
116         int is_capture, i;
117
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;
133                 }
134                 if (*stored_volume != new_volume) {
135                         *stored_volume = new_volume;
136                         changed = 1;
137                         pcxhr_update_analog_audio_level(chip, is_capture, i);
138                 }
139         }
140         mutex_unlock(&chip->mgr->mixer_mutex);
141         return changed;
142 }
143
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 */
153 };
154
155 /* shared */
156 #define pcxhr_sw_info           snd_ctl_boolean_stereo_info
157
158 static int pcxhr_audio_sw_get(struct snd_kcontrol *kcontrol,
159                               struct snd_ctl_elem_value *ucontrol)
160 {
161         struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
162
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;
168 }
169
170 static int pcxhr_audio_sw_put(struct snd_kcontrol *kcontrol,
171                               struct snd_ctl_elem_value *ucontrol)
172 {
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);
184                 }
185         }
186         mutex_unlock(&chip->mgr->mixer_mutex);
187         return changed;
188 }
189
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
196 };
197
198
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 */
202
203 static const DECLARE_TLV_DB_SCALE(db_scale_digital, -10975, 25, 1800);
204
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
210
211 static int pcxhr_update_playback_stream_level(struct snd_pcxhr* chip, int idx)
212 {
213         int err;
214         struct pcxhr_rmh rmh;
215         struct pcxhr_pipe *pipe = &chip->playback_pipe;
216         int left, right;
217
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;
226
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;
237
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;
243         }
244         return 0;
245 }
246
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
253
254 static int pcxhr_update_audio_pipe_level(struct snd_pcxhr* chip, int capture, int channel)
255 {
256         int err;
257         struct pcxhr_rmh rmh;
258         struct pcxhr_pipe *pipe;
259
260         if (capture)
261                 pipe = &chip->capture_pipe[0];
262         else
263                 pipe = &chip->playback_pipe;
264
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
270          */
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)
279                  */
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;
283         }
284         rmh.cmd_len = 3;
285
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;
291         }
292         return 0;
293 }
294
295
296 /* shared */
297 static int pcxhr_digital_vol_info(struct snd_kcontrol *kcontrol,
298                                   struct snd_ctl_elem_info *uinfo)
299 {
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;
305 }
306
307
308 static int pcxhr_pcm_vol_get(struct snd_kcontrol *kcontrol,
309                              struct snd_ctl_elem_value *ucontrol)
310 {
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;
315
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;
325 }
326
327 static int pcxhr_pcm_vol_put(struct snd_kcontrol *kcontrol,
328                              struct snd_ctl_elem_value *ucontrol)
329 {
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;
336
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);
352                 }
353         }
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;
358 }
359
360 static struct snd_kcontrol_new snd_pcxhr_pcm_vol =
361 {
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 },
371 };
372
373
374 static int pcxhr_pcm_sw_get(struct snd_kcontrol *kcontrol,
375                             struct snd_ctl_elem_value *ucontrol)
376 {
377         struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
378         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
379
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;
385 }
386
387 static int pcxhr_pcm_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
388 {
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;
393
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;
402                 }
403         }
404         if (changed)
405                 pcxhr_update_playback_stream_level(chip, idx);
406         mutex_unlock(&chip->mgr->mixer_mutex);
407         return changed;
408 }
409
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
417 };
418
419
420 /*
421  * monitoring level control
422  */
423
424 static int pcxhr_monitor_vol_get(struct snd_kcontrol *kcontrol,
425                                  struct snd_ctl_elem_value *ucontrol)
426 {
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;
433 }
434
435 static int pcxhr_monitor_vol_put(struct snd_kcontrol *kcontrol,
436                                  struct snd_ctl_elem_value *ucontrol)
437 {
438         struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
439         int changed = 0;
440         int i;
441
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;
453                 }
454         }
455         mutex_unlock(&chip->mgr->mixer_mutex);
456         return changed;
457 }
458
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 },
468 };
469
470 /*
471  * monitoring switch control
472  */
473
474 static int pcxhr_monitor_sw_get(struct snd_kcontrol *kcontrol,
475                                 struct snd_ctl_elem_value *ucontrol)
476 {
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;
483 }
484
485 static int pcxhr_monitor_sw_put(struct snd_kcontrol *kcontrol,
486                                 struct snd_ctl_elem_value *ucontrol)
487 {
488         struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
489         int changed = 0;
490         int i;
491
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 */
499                 }
500         }
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);
507
508         mutex_unlock(&chip->mgr->mixer_mutex);
509         return (changed != 0);
510 }
511
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
518 };
519
520
521
522 /*
523  * audio source select
524  */
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
530
531 static int pcxhr_set_audio_source(struct snd_pcxhr* chip)
532 {
533         struct pcxhr_rmh rmh;
534         unsigned int mask, reg;
535         unsigned int codec;
536         int err, use_src, changed;
537
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;
544         }
545         reg = 0;        /* audio source from analog plug */
546         use_src = 0;    /* do not activate codec SRC */
547
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;
552         }
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;
562         }
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;
574 }
575
576 static int pcxhr_audio_src_info(struct snd_kcontrol *kcontrol,
577                                 struct snd_ctl_elem_info *uinfo)
578 {
579         static char *texts[3] = {"Analog", "Digital", "Digi+SRC"};
580
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;
589 }
590
591 static int pcxhr_audio_src_get(struct snd_kcontrol *kcontrol,
592                                struct snd_ctl_elem_value *ucontrol)
593 {
594         struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
595         ucontrol->value.enumerated.item[0] = chip->audio_capture_source;
596         return 0;
597 }
598
599 static int pcxhr_audio_src_put(struct snd_kcontrol *kcontrol,
600                                struct snd_ctl_elem_value *ucontrol)
601 {
602         struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
603         int ret = 0;
604
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;
612         }
613         mutex_unlock(&chip->mgr->mixer_mutex);
614         return ret;
615 }
616
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,
623 };
624
625
626 /*
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  *      };
637  */
638
639 static int pcxhr_clock_type_info(struct snd_kcontrol *kcontrol,
640                                  struct snd_ctl_elem_info *uinfo)
641 {
642         static char *texts[7] = {
643                 "Internal", "WordClock", "AES Sync", "AES 1", "AES 2", "AES 3", "AES 4"
644         };
645         struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
646         int clock_items = 3 + mgr->capture_chips;
647
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;
656 }
657
658 static int pcxhr_clock_type_get(struct snd_kcontrol *kcontrol,
659                                 struct snd_ctl_elem_value *ucontrol)
660 {
661         struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
662         ucontrol->value.enumerated.item[0] = mgr->use_clock_type;
663         return 0;
664 }
665
666 static int pcxhr_clock_type_put(struct snd_kcontrol *kcontrol,
667                                 struct snd_ctl_elem_value *ucontrol)
668 {
669         struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
670         unsigned int clock_items = 3 + mgr->capture_chips;
671         int rate, ret = 0;
672
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;
687                 }
688                 mutex_unlock(&mgr->setup_mutex);
689                 ret = 1;        /* return 1 even if the set was not done. ok ? */
690         }
691         mutex_unlock(&mgr->mixer_mutex);
692         return ret;
693 }
694
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,
701 };
702
703 /*
704  * clock rate control
705  * specific control that scans the sample rates on the external plugs
706  */
707 static int pcxhr_clock_rate_info(struct snd_kcontrol *kcontrol,
708                                  struct snd_ctl_elem_info *uinfo)
709 {
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;
716 }
717
718 static int pcxhr_clock_rate_get(struct snd_kcontrol *kcontrol,
719                                 struct snd_ctl_elem_value *ucontrol)
720 {
721         struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
722         int i, err, rate;
723
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;
732                 }
733                 ucontrol->value.integer.value[i] = rate;
734         }
735         mutex_unlock(&mgr->mixer_mutex);
736         return 0;
737 }
738
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,
745 };
746
747 /*
748  * IEC958 status bits
749  */
750 static int pcxhr_iec958_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
751 {
752         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
753         uinfo->count = 1;
754         return 0;
755 }
756
757 static int pcxhr_iec958_capture_byte(struct snd_pcxhr *chip, int aes_idx, unsigned char* aes_bits)
758 {
759         int i, err;
760         unsigned char temp;
761         struct pcxhr_rmh rmh;
762
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;
771         }
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;
779         }
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;
792         }
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;
796 }
797
798 static int pcxhr_iec958_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
799 {
800         struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
801         unsigned char aes_bits;
802         int i, err;
803
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;
812                 }
813                 ucontrol->value.iec958.status[i] = aes_bits;
814         }
815         mutex_unlock(&chip->mgr->mixer_mutex);
816         return 0;
817 }
818
819 static int pcxhr_iec958_mask_get(struct snd_kcontrol *kcontrol,
820                                  struct snd_ctl_elem_value *ucontrol)
821 {
822         int i;
823         for (i = 0; i < 5; i++)
824                 ucontrol->value.iec958.status[i] = 0xff;
825         return 0;
826 }
827
828 static int pcxhr_iec958_update_byte(struct snd_pcxhr *chip, int aes_idx, unsigned char aes_bits)
829 {
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;
834
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;
852                 }
853                 old_bits >>= 1;
854                 new_bits >>= 1;
855         }
856         chip->aes_bits[aes_idx] = aes_bits;
857         return 0;
858 }
859
860 static int pcxhr_iec958_put(struct snd_kcontrol *kcontrol,
861                             struct snd_ctl_elem_value *ucontrol)
862 {
863         struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
864         int i, changed = 0;
865
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;
872                 }
873         }
874         mutex_unlock(&chip->mgr->mixer_mutex);
875         return changed;
876 }
877
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
884 };
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 */
892 };
893
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
900 };
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 */
908 };
909
910 static void pcxhr_init_audio_levels(struct snd_pcxhr *chip)
911 {
912         int i;
913
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;
921                         }
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 */
932                 }
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 */
943                 }
944         }
945
946         return;
947 }
948
949
950 int pcxhr_create_mixer(struct pcxhr_mgr *mgr)
951 {
952         struct snd_pcxhr *chip;
953         int err, i;
954
955         mutex_init(&mgr->mixer_mutex); /* can be in another place */
956
957         for (i = 0; i < mgr->num_cards; i++) {
958                 struct snd_kcontrol_new temp;
959                 chip = mgr->chip[i];
960
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;
974                         
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;
981
982                         if ((err = snd_ctl_add(chip->card,
983                                                snd_ctl_new1(&pcxhr_control_pcm_switch,
984                                                             chip))) < 0)
985                                 return err;
986
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;
996                 }
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;
1005
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;
1026                 }
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;
1038                 }
1039
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;
1051                 }
1052
1053                 /* init values for the mixer data */
1054                 pcxhr_init_audio_levels(chip);
1055         }
1056
1057         return 0;
1058 }