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