blob: fec049344621ef97f922c395e8ff5693e3c48945 [file] [log] [blame]
Markus Bollingere12229b2005-12-06 13:55:26 +01001#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
Markus Bollingere12229b2005-12-06 13:55:26 +010024#include <linux/time.h>
25#include <linux/interrupt.h>
26#include <linux/init.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Markus Bollingere12229b2005-12-06 13:55:26 +010028#include <sound/core.h>
29#include "pcxhr.h"
30#include "pcxhr_hwdep.h"
31#include "pcxhr_core.h"
32#include <sound/control.h>
Takashi Iwaic6ff77f2006-08-23 19:53:02 +020033#include <sound/tlv.h>
Markus Bollingere12229b2005-12-06 13:55:26 +010034#include <sound/asoundef.h>
35#include "pcxhr_mixer.h"
Markus Bollingerc0193f32008-11-25 12:37:52 +010036#include "pcxhr_mix22.h"
Markus Bollingere12229b2005-12-06 13:55:26 +010037
Markus Bollingerc0193f32008-11-25 12:37:52 +010038#define PCXHR_LINE_CAPTURE_LEVEL_MIN 0 /* -112.0 dB */
39#define PCXHR_LINE_CAPTURE_LEVEL_MAX 255 /* +15.5 dB */
40#define PCXHR_LINE_CAPTURE_ZERO_LEVEL 224 /* 0.0 dB ( 0 dBu -> 0 dBFS ) */
Markus Bollingere12229b2005-12-06 13:55:26 +010041
Markus Bollingerc0193f32008-11-25 12:37:52 +010042#define PCXHR_LINE_PLAYBACK_LEVEL_MIN 0 /* -104.0 dB */
43#define PCXHR_LINE_PLAYBACK_LEVEL_MAX 128 /* +24.0 dB */
44#define PCXHR_LINE_PLAYBACK_ZERO_LEVEL 104 /* 0.0 dB ( 0 dBFS -> 0 dBu ) */
Markus Bollingere12229b2005-12-06 13:55:26 +010045
Markus Bollingerc0193f32008-11-25 12:37:52 +010046static const DECLARE_TLV_DB_SCALE(db_scale_analog_capture, -11200, 50, 1550);
Takashi Iwaie6382cf2007-10-10 10:06:46 +020047static const DECLARE_TLV_DB_SCALE(db_scale_analog_playback, -10400, 100, 2400);
Takashi Iwaic6ff77f2006-08-23 19:53:02 +020048
Markus Bollingerc0193f32008-11-25 12:37:52 +010049static const DECLARE_TLV_DB_SCALE(db_scale_a_hr222_capture, -11150, 50, 1600);
50static const DECLARE_TLV_DB_SCALE(db_scale_a_hr222_playback, -2550, 50, 2400);
51
52static int pcxhr_update_analog_audio_level(struct snd_pcxhr *chip,
53 int is_capture, int channel)
Markus Bollingere12229b2005-12-06 13:55:26 +010054{
55 int err, vol;
56 struct pcxhr_rmh rmh;
57
58 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
59 if (is_capture) {
60 rmh.cmd[0] |= IO_NUM_REG_IN_ANA_LEVEL;
61 rmh.cmd[2] = chip->analog_capture_volume[channel];
62 } else {
63 rmh.cmd[0] |= IO_NUM_REG_OUT_ANA_LEVEL;
64 if (chip->analog_playback_active[channel])
65 vol = chip->analog_playback_volume[channel];
66 else
Markus Bollingerc0193f32008-11-25 12:37:52 +010067 vol = PCXHR_LINE_PLAYBACK_LEVEL_MIN;
68 /* playback analog levels are inversed */
69 rmh.cmd[2] = PCXHR_LINE_PLAYBACK_LEVEL_MAX - vol;
Markus Bollingere12229b2005-12-06 13:55:26 +010070 }
71 rmh.cmd[1] = 1 << ((2 * chip->chip_idx) + channel); /* audio mask */
72 rmh.cmd_len = 3;
73 err = pcxhr_send_msg(chip->mgr, &rmh);
74 if (err < 0) {
Markus Bollingerc0193f32008-11-25 12:37:52 +010075 snd_printk(KERN_DEBUG "error update_analog_audio_level card(%d)"
76 " is_capture(%d) err(%x)\n",
77 chip->chip_idx, is_capture, err);
Markus Bollingere12229b2005-12-06 13:55:26 +010078 return -EINVAL;
79 }
80 return 0;
81}
82
83/*
84 * analog level control
85 */
86static int pcxhr_analog_vol_info(struct snd_kcontrol *kcontrol,
87 struct snd_ctl_elem_info *uinfo)
88{
Markus Bollingerc0193f32008-11-25 12:37:52 +010089 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
90
Markus Bollingere12229b2005-12-06 13:55:26 +010091 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
92 uinfo->count = 2;
93 if (kcontrol->private_value == 0) { /* playback */
Markus Bollingerc0193f32008-11-25 12:37:52 +010094 if (chip->mgr->is_hr_stereo) {
95 uinfo->value.integer.min =
96 HR222_LINE_PLAYBACK_LEVEL_MIN; /* -25 dB */
97 uinfo->value.integer.max =
98 HR222_LINE_PLAYBACK_LEVEL_MAX; /* +24 dB */
99 } else {
100 uinfo->value.integer.min =
101 PCXHR_LINE_PLAYBACK_LEVEL_MIN; /*-104 dB */
102 uinfo->value.integer.max =
103 PCXHR_LINE_PLAYBACK_LEVEL_MAX; /* +24 dB */
104 }
Markus Bollingere12229b2005-12-06 13:55:26 +0100105 } else { /* capture */
Markus Bollingerc0193f32008-11-25 12:37:52 +0100106 if (chip->mgr->is_hr_stereo) {
107 uinfo->value.integer.min =
108 HR222_LINE_CAPTURE_LEVEL_MIN; /*-112 dB */
109 uinfo->value.integer.max =
110 HR222_LINE_CAPTURE_LEVEL_MAX; /* +15.5 dB */
111 } else {
112 uinfo->value.integer.min =
113 PCXHR_LINE_CAPTURE_LEVEL_MIN; /*-112 dB */
114 uinfo->value.integer.max =
115 PCXHR_LINE_CAPTURE_LEVEL_MAX; /* +15.5 dB */
116 }
Markus Bollingere12229b2005-12-06 13:55:26 +0100117 }
118 return 0;
119}
120
121static int pcxhr_analog_vol_get(struct snd_kcontrol *kcontrol,
122 struct snd_ctl_elem_value *ucontrol)
123{
124 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
Ingo Molnar62932df2006-01-16 16:34:20 +0100125 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100126 if (kcontrol->private_value == 0) { /* playback */
Markus Bollingerc0193f32008-11-25 12:37:52 +0100127 ucontrol->value.integer.value[0] = chip->analog_playback_volume[0];
128 ucontrol->value.integer.value[1] = chip->analog_playback_volume[1];
Markus Bollingere12229b2005-12-06 13:55:26 +0100129 } else { /* capture */
Markus Bollingerc0193f32008-11-25 12:37:52 +0100130 ucontrol->value.integer.value[0] = chip->analog_capture_volume[0];
131 ucontrol->value.integer.value[1] = chip->analog_capture_volume[1];
Markus Bollingere12229b2005-12-06 13:55:26 +0100132 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100133 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100134 return 0;
135}
136
137static int pcxhr_analog_vol_put(struct snd_kcontrol *kcontrol,
138 struct snd_ctl_elem_value *ucontrol)
139{
140 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
141 int changed = 0;
142 int is_capture, i;
143
Ingo Molnar62932df2006-01-16 16:34:20 +0100144 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100145 is_capture = (kcontrol->private_value != 0);
146 for (i = 0; i < 2; i++) {
147 int new_volume = ucontrol->value.integer.value[i];
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100148 int *stored_volume = is_capture ?
149 &chip->analog_capture_volume[i] :
Markus Bollingere12229b2005-12-06 13:55:26 +0100150 &chip->analog_playback_volume[i];
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100151 if (is_capture) {
Markus Bollingerc0193f32008-11-25 12:37:52 +0100152 if (chip->mgr->is_hr_stereo) {
153 if (new_volume < HR222_LINE_CAPTURE_LEVEL_MIN ||
154 new_volume > HR222_LINE_CAPTURE_LEVEL_MAX)
155 continue;
156 } else {
157 if (new_volume < PCXHR_LINE_CAPTURE_LEVEL_MIN ||
158 new_volume > PCXHR_LINE_CAPTURE_LEVEL_MAX)
159 continue;
160 }
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100161 } else {
Markus Bollingerc0193f32008-11-25 12:37:52 +0100162 if (chip->mgr->is_hr_stereo) {
163 if (new_volume < HR222_LINE_PLAYBACK_LEVEL_MIN ||
164 new_volume > HR222_LINE_PLAYBACK_LEVEL_MAX)
165 continue;
166 } else {
167 if (new_volume < PCXHR_LINE_PLAYBACK_LEVEL_MIN ||
168 new_volume > PCXHR_LINE_PLAYBACK_LEVEL_MAX)
169 continue;
170 }
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100171 }
Markus Bollingere12229b2005-12-06 13:55:26 +0100172 if (*stored_volume != new_volume) {
173 *stored_volume = new_volume;
174 changed = 1;
Markus Bollingerc0193f32008-11-25 12:37:52 +0100175 if (chip->mgr->is_hr_stereo)
176 hr222_update_analog_audio_level(chip,
177 is_capture, i);
178 else
179 pcxhr_update_analog_audio_level(chip,
180 is_capture, i);
Markus Bollingere12229b2005-12-06 13:55:26 +0100181 }
182 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100183 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100184 return changed;
185}
186
187static struct snd_kcontrol_new pcxhr_control_analog_level = {
188 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Takashi Iwaic6ff77f2006-08-23 19:53:02 +0200189 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
190 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
Markus Bollingere12229b2005-12-06 13:55:26 +0100191 /* name will be filled later */
192 .info = pcxhr_analog_vol_info,
193 .get = pcxhr_analog_vol_get,
194 .put = pcxhr_analog_vol_put,
Takashi Iwaic6ff77f2006-08-23 19:53:02 +0200195 /* tlv will be filled later */
Markus Bollingere12229b2005-12-06 13:55:26 +0100196};
197
198/* shared */
Markus Bollingerc0193f32008-11-25 12:37:52 +0100199
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200200#define pcxhr_sw_info snd_ctl_boolean_stereo_info
Markus Bollingere12229b2005-12-06 13:55:26 +0100201
202static int pcxhr_audio_sw_get(struct snd_kcontrol *kcontrol,
203 struct snd_ctl_elem_value *ucontrol)
204{
205 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
206
Ingo Molnar62932df2006-01-16 16:34:20 +0100207 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100208 ucontrol->value.integer.value[0] = chip->analog_playback_active[0];
209 ucontrol->value.integer.value[1] = chip->analog_playback_active[1];
Ingo Molnar62932df2006-01-16 16:34:20 +0100210 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100211 return 0;
212}
213
214static int pcxhr_audio_sw_put(struct snd_kcontrol *kcontrol,
215 struct snd_ctl_elem_value *ucontrol)
216{
217 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
218 int i, changed = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +0100219 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100220 for(i = 0; i < 2; i++) {
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100221 if (chip->analog_playback_active[i] !=
222 ucontrol->value.integer.value[i]) {
223 chip->analog_playback_active[i] =
224 !!ucontrol->value.integer.value[i];
Markus Bollingere12229b2005-12-06 13:55:26 +0100225 changed = 1;
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100226 /* update playback levels */
Markus Bollingerc0193f32008-11-25 12:37:52 +0100227 if (chip->mgr->is_hr_stereo)
228 hr222_update_analog_audio_level(chip, 0, i);
229 else
230 pcxhr_update_analog_audio_level(chip, 0, i);
Markus Bollingere12229b2005-12-06 13:55:26 +0100231 }
232 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100233 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100234 return changed;
235}
236
237static struct snd_kcontrol_new pcxhr_control_output_switch = {
238 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
239 .name = "Master Playback Switch",
240 .info = pcxhr_sw_info, /* shared */
241 .get = pcxhr_audio_sw_get,
242 .put = pcxhr_audio_sw_put
243};
244
245
246#define PCXHR_DIGITAL_LEVEL_MIN 0x000 /* -110 dB */
247#define PCXHR_DIGITAL_LEVEL_MAX 0x1ff /* +18 dB */
248#define PCXHR_DIGITAL_ZERO_LEVEL 0x1b7 /* 0 dB */
249
Takashi Iwaie6382cf2007-10-10 10:06:46 +0200250static const DECLARE_TLV_DB_SCALE(db_scale_digital, -10975, 25, 1800);
Markus Bollingere12229b2005-12-06 13:55:26 +0100251
252#define MORE_THAN_ONE_STREAM_LEVEL 0x000001
253#define VALID_STREAM_PAN_LEVEL_MASK 0x800000
254#define VALID_STREAM_LEVEL_MASK 0x400000
255#define VALID_STREAM_LEVEL_1_MASK 0x200000
256#define VALID_STREAM_LEVEL_2_MASK 0x100000
257
258static int pcxhr_update_playback_stream_level(struct snd_pcxhr* chip, int idx)
259{
260 int err;
261 struct pcxhr_rmh rmh;
262 struct pcxhr_pipe *pipe = &chip->playback_pipe;
263 int left, right;
264
265 if (chip->digital_playback_active[idx][0])
266 left = chip->digital_playback_volume[idx][0];
267 else
268 left = PCXHR_DIGITAL_LEVEL_MIN;
269 if (chip->digital_playback_active[idx][1])
270 right = chip->digital_playback_volume[idx][1];
271 else
272 right = PCXHR_DIGITAL_LEVEL_MIN;
273
274 pcxhr_init_rmh(&rmh, CMD_STREAM_OUT_LEVEL_ADJUST);
275 /* add pipe and stream mask */
276 pcxhr_set_pipe_cmd_params(&rmh, 0, pipe->first_audio, 0, 1<<idx);
277 /* volume left->left / right->right panoramic level */
278 rmh.cmd[0] |= MORE_THAN_ONE_STREAM_LEVEL;
279 rmh.cmd[2] = VALID_STREAM_PAN_LEVEL_MASK | VALID_STREAM_LEVEL_1_MASK;
280 rmh.cmd[2] |= (left << 10);
281 rmh.cmd[3] = VALID_STREAM_PAN_LEVEL_MASK | VALID_STREAM_LEVEL_2_MASK;
282 rmh.cmd[3] |= right;
283 rmh.cmd_len = 4;
284
285 err = pcxhr_send_msg(chip->mgr, &rmh);
286 if (err < 0) {
287 snd_printk(KERN_DEBUG "error update_playback_stream_level "
288 "card(%d) err(%x)\n", chip->chip_idx, err);
289 return -EINVAL;
290 }
291 return 0;
292}
293
294#define AUDIO_IO_HAS_MUTE_LEVEL 0x400000
295#define AUDIO_IO_HAS_MUTE_MONITOR_1 0x200000
296#define VALID_AUDIO_IO_DIGITAL_LEVEL 0x000001
297#define VALID_AUDIO_IO_MONITOR_LEVEL 0x000002
298#define VALID_AUDIO_IO_MUTE_LEVEL 0x000004
299#define VALID_AUDIO_IO_MUTE_MONITOR_1 0x000008
300
Markus Bollingerc0193f32008-11-25 12:37:52 +0100301static int pcxhr_update_audio_pipe_level(struct snd_pcxhr *chip,
302 int capture, int channel)
Markus Bollingere12229b2005-12-06 13:55:26 +0100303{
304 int err;
305 struct pcxhr_rmh rmh;
306 struct pcxhr_pipe *pipe;
307
308 if (capture)
309 pipe = &chip->capture_pipe[0];
310 else
311 pipe = &chip->playback_pipe;
312
313 pcxhr_init_rmh(&rmh, CMD_AUDIO_LEVEL_ADJUST);
314 /* add channel mask */
Markus Bollingerc0193f32008-11-25 12:37:52 +0100315 pcxhr_set_pipe_cmd_params(&rmh, capture, 0, 0,
316 1 << (channel + pipe->first_audio));
317 /* TODO : if mask (3 << pipe->first_audio) is used, left and right
318 * channel will be programmed to the same params */
Markus Bollingere12229b2005-12-06 13:55:26 +0100319 if (capture) {
320 rmh.cmd[0] |= VALID_AUDIO_IO_DIGITAL_LEVEL;
Markus Bollingerc0193f32008-11-25 12:37:52 +0100321 /* VALID_AUDIO_IO_MUTE_LEVEL not yet handled
322 * (capture pipe level) */
Markus Bollingere12229b2005-12-06 13:55:26 +0100323 rmh.cmd[2] = chip->digital_capture_volume[channel];
324 } else {
Markus Bollingerc0193f32008-11-25 12:37:52 +0100325 rmh.cmd[0] |= VALID_AUDIO_IO_MONITOR_LEVEL |
326 VALID_AUDIO_IO_MUTE_MONITOR_1;
327 /* VALID_AUDIO_IO_DIGITAL_LEVEL and VALID_AUDIO_IO_MUTE_LEVEL
328 * not yet handled (playback pipe level)
Markus Bollingere12229b2005-12-06 13:55:26 +0100329 */
330 rmh.cmd[2] = chip->monitoring_volume[channel] << 10;
331 if (chip->monitoring_active[channel] == 0)
332 rmh.cmd[2] |= AUDIO_IO_HAS_MUTE_MONITOR_1;
333 }
334 rmh.cmd_len = 3;
335
336 err = pcxhr_send_msg(chip->mgr, &rmh);
Markus Bollingerc0193f32008-11-25 12:37:52 +0100337 if (err < 0) {
338 snd_printk(KERN_DEBUG "error update_audio_level(%d) err=%x\n",
Markus Bollingere12229b2005-12-06 13:55:26 +0100339 chip->chip_idx, err);
340 return -EINVAL;
341 }
342 return 0;
343}
344
345
346/* shared */
347static int pcxhr_digital_vol_info(struct snd_kcontrol *kcontrol,
348 struct snd_ctl_elem_info *uinfo)
349{
350 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
351 uinfo->count = 2;
352 uinfo->value.integer.min = PCXHR_DIGITAL_LEVEL_MIN; /* -109.5 dB */
353 uinfo->value.integer.max = PCXHR_DIGITAL_LEVEL_MAX; /* 18.0 dB */
354 return 0;
355}
356
357
358static int pcxhr_pcm_vol_get(struct snd_kcontrol *kcontrol,
359 struct snd_ctl_elem_value *ucontrol)
360{
361 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
Markus Bollingerc0193f32008-11-25 12:37:52 +0100362 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
Markus Bollingere12229b2005-12-06 13:55:26 +0100363 int *stored_volume;
364 int is_capture = kcontrol->private_value;
365
Ingo Molnar62932df2006-01-16 16:34:20 +0100366 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingerc0193f32008-11-25 12:37:52 +0100367 if (is_capture) /* digital capture */
368 stored_volume = chip->digital_capture_volume;
369 else /* digital playback */
370 stored_volume = chip->digital_playback_volume[idx];
Markus Bollingere12229b2005-12-06 13:55:26 +0100371 ucontrol->value.integer.value[0] = stored_volume[0];
372 ucontrol->value.integer.value[1] = stored_volume[1];
Ingo Molnar62932df2006-01-16 16:34:20 +0100373 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100374 return 0;
375}
376
377static int pcxhr_pcm_vol_put(struct snd_kcontrol *kcontrol,
378 struct snd_ctl_elem_value *ucontrol)
379{
380 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
Markus Bollingerc0193f32008-11-25 12:37:52 +0100381 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
Markus Bollingere12229b2005-12-06 13:55:26 +0100382 int changed = 0;
383 int is_capture = kcontrol->private_value;
384 int *stored_volume;
385 int i;
386
Ingo Molnar62932df2006-01-16 16:34:20 +0100387 mutex_lock(&chip->mgr->mixer_mutex);
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100388 if (is_capture) /* digital capture */
389 stored_volume = chip->digital_capture_volume;
390 else /* digital playback */
391 stored_volume = chip->digital_playback_volume[idx];
Markus Bollingere12229b2005-12-06 13:55:26 +0100392 for (i = 0; i < 2; i++) {
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100393 int vol = ucontrol->value.integer.value[i];
394 if (vol < PCXHR_DIGITAL_LEVEL_MIN ||
395 vol > PCXHR_DIGITAL_LEVEL_MAX)
396 continue;
397 if (stored_volume[i] != vol) {
398 stored_volume[i] = vol;
Markus Bollingere12229b2005-12-06 13:55:26 +0100399 changed = 1;
400 if (is_capture) /* update capture volume */
401 pcxhr_update_audio_pipe_level(chip, 1, i);
402 }
403 }
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100404 if (!is_capture && changed) /* update playback volume */
405 pcxhr_update_playback_stream_level(chip, idx);
Ingo Molnar62932df2006-01-16 16:34:20 +0100406 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100407 return changed;
408}
409
410static struct snd_kcontrol_new snd_pcxhr_pcm_vol =
411{
412 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Takashi Iwaic6ff77f2006-08-23 19:53:02 +0200413 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
414 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
Markus Bollingere12229b2005-12-06 13:55:26 +0100415 /* name will be filled later */
416 /* count will be filled later */
417 .info = pcxhr_digital_vol_info, /* shared */
418 .get = pcxhr_pcm_vol_get,
419 .put = pcxhr_pcm_vol_put,
Takashi Iwaic6ff77f2006-08-23 19:53:02 +0200420 .tlv = { .p = db_scale_digital },
Markus Bollingere12229b2005-12-06 13:55:26 +0100421};
422
423
424static int pcxhr_pcm_sw_get(struct snd_kcontrol *kcontrol,
425 struct snd_ctl_elem_value *ucontrol)
426{
427 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
428 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
429
Ingo Molnar62932df2006-01-16 16:34:20 +0100430 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100431 ucontrol->value.integer.value[0] = chip->digital_playback_active[idx][0];
432 ucontrol->value.integer.value[1] = chip->digital_playback_active[idx][1];
Ingo Molnar62932df2006-01-16 16:34:20 +0100433 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100434 return 0;
435}
436
Markus Bollingerc0193f32008-11-25 12:37:52 +0100437static int pcxhr_pcm_sw_put(struct snd_kcontrol *kcontrol,
438 struct snd_ctl_elem_value *ucontrol)
Markus Bollingere12229b2005-12-06 13:55:26 +0100439{
440 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
441 int changed = 0;
442 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
443 int i, j;
444
Ingo Molnar62932df2006-01-16 16:34:20 +0100445 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100446 j = idx;
447 for (i = 0; i < 2; i++) {
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100448 if (chip->digital_playback_active[j][i] !=
449 ucontrol->value.integer.value[i]) {
450 chip->digital_playback_active[j][i] =
451 !!ucontrol->value.integer.value[i];
Markus Bollingere12229b2005-12-06 13:55:26 +0100452 changed = 1;
453 }
454 }
455 if (changed)
456 pcxhr_update_playback_stream_level(chip, idx);
Ingo Molnar62932df2006-01-16 16:34:20 +0100457 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100458 return changed;
459}
460
461static struct snd_kcontrol_new pcxhr_control_pcm_switch = {
462 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
463 .name = "PCM Playback Switch",
464 .count = PCXHR_PLAYBACK_STREAMS,
465 .info = pcxhr_sw_info, /* shared */
466 .get = pcxhr_pcm_sw_get,
467 .put = pcxhr_pcm_sw_put
468};
469
470
471/*
472 * monitoring level control
473 */
474
475static int pcxhr_monitor_vol_get(struct snd_kcontrol *kcontrol,
476 struct snd_ctl_elem_value *ucontrol)
477{
478 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
Ingo Molnar62932df2006-01-16 16:34:20 +0100479 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100480 ucontrol->value.integer.value[0] = chip->monitoring_volume[0];
481 ucontrol->value.integer.value[1] = chip->monitoring_volume[1];
Ingo Molnar62932df2006-01-16 16:34:20 +0100482 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100483 return 0;
484}
485
486static int pcxhr_monitor_vol_put(struct snd_kcontrol *kcontrol,
487 struct snd_ctl_elem_value *ucontrol)
488{
489 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
490 int changed = 0;
491 int i;
492
Ingo Molnar62932df2006-01-16 16:34:20 +0100493 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100494 for (i = 0; i < 2; i++) {
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100495 if (chip->monitoring_volume[i] !=
496 ucontrol->value.integer.value[i]) {
497 chip->monitoring_volume[i] =
Markus Bollingerc0193f32008-11-25 12:37:52 +0100498 ucontrol->value.integer.value[i];
499 if (chip->monitoring_active[i])
Markus Bollingere12229b2005-12-06 13:55:26 +0100500 /* update monitoring volume and mute */
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100501 /* do only when monitoring is unmuted */
Markus Bollingere12229b2005-12-06 13:55:26 +0100502 pcxhr_update_audio_pipe_level(chip, 0, i);
503 changed = 1;
504 }
505 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100506 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100507 return changed;
508}
509
510static struct snd_kcontrol_new pcxhr_control_monitor_vol = {
511 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Takashi Iwaic6ff77f2006-08-23 19:53:02 +0200512 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
513 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
Markus Bollingerc0193f32008-11-25 12:37:52 +0100514 .name = "Monitoring Playback Volume",
Markus Bollingere12229b2005-12-06 13:55:26 +0100515 .info = pcxhr_digital_vol_info, /* shared */
516 .get = pcxhr_monitor_vol_get,
517 .put = pcxhr_monitor_vol_put,
Takashi Iwaic6ff77f2006-08-23 19:53:02 +0200518 .tlv = { .p = db_scale_digital },
Markus Bollingere12229b2005-12-06 13:55:26 +0100519};
520
521/*
522 * monitoring switch control
523 */
524
525static int pcxhr_monitor_sw_get(struct snd_kcontrol *kcontrol,
526 struct snd_ctl_elem_value *ucontrol)
527{
528 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
Ingo Molnar62932df2006-01-16 16:34:20 +0100529 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100530 ucontrol->value.integer.value[0] = chip->monitoring_active[0];
531 ucontrol->value.integer.value[1] = chip->monitoring_active[1];
Ingo Molnar62932df2006-01-16 16:34:20 +0100532 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100533 return 0;
534}
535
536static int pcxhr_monitor_sw_put(struct snd_kcontrol *kcontrol,
537 struct snd_ctl_elem_value *ucontrol)
538{
539 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
540 int changed = 0;
541 int i;
542
Ingo Molnar62932df2006-01-16 16:34:20 +0100543 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100544 for (i = 0; i < 2; i++) {
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100545 if (chip->monitoring_active[i] !=
546 ucontrol->value.integer.value[i]) {
547 chip->monitoring_active[i] =
548 !!ucontrol->value.integer.value[i];
Markus Bollingere12229b2005-12-06 13:55:26 +0100549 changed |= (1<<i); /* mask 0x01 and 0x02 */
550 }
551 }
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100552 if (changed & 0x01)
Markus Bollingere12229b2005-12-06 13:55:26 +0100553 /* update left monitoring volume and mute */
554 pcxhr_update_audio_pipe_level(chip, 0, 0);
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100555 if (changed & 0x02)
Markus Bollingere12229b2005-12-06 13:55:26 +0100556 /* update right monitoring volume and mute */
557 pcxhr_update_audio_pipe_level(chip, 0, 1);
558
Ingo Molnar62932df2006-01-16 16:34:20 +0100559 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100560 return (changed != 0);
561}
562
563static struct snd_kcontrol_new pcxhr_control_monitor_sw = {
564 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Markus Bollingerc0193f32008-11-25 12:37:52 +0100565 .name = "Monitoring Playback Switch",
Markus Bollingere12229b2005-12-06 13:55:26 +0100566 .info = pcxhr_sw_info, /* shared */
567 .get = pcxhr_monitor_sw_get,
568 .put = pcxhr_monitor_sw_put
569};
570
571
572
573/*
574 * audio source select
575 */
576#define PCXHR_SOURCE_AUDIO01_UER 0x000100
577#define PCXHR_SOURCE_AUDIO01_SYNC 0x000200
578#define PCXHR_SOURCE_AUDIO23_UER 0x000400
579#define PCXHR_SOURCE_AUDIO45_UER 0x001000
580#define PCXHR_SOURCE_AUDIO67_UER 0x040000
581
582static int pcxhr_set_audio_source(struct snd_pcxhr* chip)
583{
584 struct pcxhr_rmh rmh;
585 unsigned int mask, reg;
586 unsigned int codec;
Markus Bollingerc0193f32008-11-25 12:37:52 +0100587 int err, changed;
Markus Bollingere12229b2005-12-06 13:55:26 +0100588
589 switch (chip->chip_idx) {
590 case 0 : mask = PCXHR_SOURCE_AUDIO01_UER; codec = CS8420_01_CS; break;
591 case 1 : mask = PCXHR_SOURCE_AUDIO23_UER; codec = CS8420_23_CS; break;
592 case 2 : mask = PCXHR_SOURCE_AUDIO45_UER; codec = CS8420_45_CS; break;
593 case 3 : mask = PCXHR_SOURCE_AUDIO67_UER; codec = CS8420_67_CS; break;
594 default: return -EINVAL;
595 }
Markus Bollingere12229b2005-12-06 13:55:26 +0100596 if (chip->audio_capture_source != 0) {
597 reg = mask; /* audio source from digital plug */
Markus Bollingerc0193f32008-11-25 12:37:52 +0100598 } else {
599 reg = 0; /* audio source from analog plug */
Markus Bollingere12229b2005-12-06 13:55:26 +0100600 }
601 /* set the input source */
602 pcxhr_write_io_num_reg_cont(chip->mgr, mask, reg, &changed);
603 /* resync them (otherwise channel inversion possible) */
604 if (changed) {
605 pcxhr_init_rmh(&rmh, CMD_RESYNC_AUDIO_INPUTS);
606 rmh.cmd[0] |= (1 << chip->chip_idx);
607 err = pcxhr_send_msg(chip->mgr, &rmh);
608 if (err)
609 return err;
610 }
Markus Bollingerc0193f32008-11-25 12:37:52 +0100611 if (chip->mgr->board_aes_in_192k) {
612 int i;
613 unsigned int src_config = 0xC0;
614 /* update all src configs with one call */
615 for (i = 0; (i < 4) && (i < chip->mgr->capture_chips); i++) {
616 if (chip->mgr->chip[i]->audio_capture_source == 2)
617 src_config |= (1 << (3 - i));
618 }
619 /* set codec SRC on off */
620 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
621 rmh.cmd_len = 2;
622 rmh.cmd[0] |= IO_NUM_REG_CONFIG_SRC;
623 rmh.cmd[1] = src_config;
624 err = pcxhr_send_msg(chip->mgr, &rmh);
625 } else {
626 int use_src = 0;
627 if (chip->audio_capture_source == 2)
628 use_src = 1;
629 /* set codec SRC on off */
630 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
631 rmh.cmd_len = 3;
632 rmh.cmd[0] |= IO_NUM_UER_CHIP_REG;
633 rmh.cmd[1] = codec;
634 rmh.cmd[2] = ((CS8420_DATA_FLOW_CTL & CHIP_SIG_AND_MAP_SPI) |
635 (use_src ? 0x41 : 0x54));
636 err = pcxhr_send_msg(chip->mgr, &rmh);
637 if (err)
638 return err;
639 rmh.cmd[2] = ((CS8420_CLOCK_SRC_CTL & CHIP_SIG_AND_MAP_SPI) |
640 (use_src ? 0x41 : 0x49));
641 err = pcxhr_send_msg(chip->mgr, &rmh);
642 }
Markus Bollingere12229b2005-12-06 13:55:26 +0100643 return err;
644}
645
646static int pcxhr_audio_src_info(struct snd_kcontrol *kcontrol,
647 struct snd_ctl_elem_info *uinfo)
648{
Markus Bollingerc0193f32008-11-25 12:37:52 +0100649 static const char *texts[5] = {
650 "Line", "Digital", "Digi+SRC", "Mic", "Line+Mic"
651 };
652 int i;
653 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
Markus Bollingere12229b2005-12-06 13:55:26 +0100654
Markus Bollingerc0193f32008-11-25 12:37:52 +0100655 i = 2; /* no SRC, no Mic available */
656 if (chip->mgr->board_has_aes1) {
657 i = 3; /* SRC available */
658 if (chip->mgr->board_has_mic)
659 i = 5; /* Mic and MicroMix available */
660 }
Markus Bollingere12229b2005-12-06 13:55:26 +0100661 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
662 uinfo->count = 1;
Markus Bollingerc0193f32008-11-25 12:37:52 +0100663 uinfo->value.enumerated.items = i;
664 if (uinfo->value.enumerated.item > (i-1))
665 uinfo->value.enumerated.item = i-1;
Markus Bollingere12229b2005-12-06 13:55:26 +0100666 strcpy(uinfo->value.enumerated.name,
667 texts[uinfo->value.enumerated.item]);
668 return 0;
669}
670
671static int pcxhr_audio_src_get(struct snd_kcontrol *kcontrol,
672 struct snd_ctl_elem_value *ucontrol)
673{
674 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
675 ucontrol->value.enumerated.item[0] = chip->audio_capture_source;
676 return 0;
677}
678
679static int pcxhr_audio_src_put(struct snd_kcontrol *kcontrol,
680 struct snd_ctl_elem_value *ucontrol)
681{
682 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
683 int ret = 0;
Markus Bollingerc0193f32008-11-25 12:37:52 +0100684 int i = 2; /* no SRC, no Mic available */
685 if (chip->mgr->board_has_aes1) {
686 i = 3; /* SRC available */
687 if (chip->mgr->board_has_mic)
688 i = 5; /* Mic and MicroMix available */
689 }
690 if (ucontrol->value.enumerated.item[0] >= i)
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100691 return -EINVAL;
Ingo Molnar62932df2006-01-16 16:34:20 +0100692 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100693 if (chip->audio_capture_source != ucontrol->value.enumerated.item[0]) {
694 chip->audio_capture_source = ucontrol->value.enumerated.item[0];
Markus Bollingerc0193f32008-11-25 12:37:52 +0100695 if (chip->mgr->is_hr_stereo)
696 hr222_set_audio_source(chip);
697 else
698 pcxhr_set_audio_source(chip);
Markus Bollingere12229b2005-12-06 13:55:26 +0100699 ret = 1;
700 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100701 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100702 return ret;
703}
704
705static struct snd_kcontrol_new pcxhr_control_audio_src = {
706 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
707 .name = "Capture Source",
708 .info = pcxhr_audio_src_info,
709 .get = pcxhr_audio_src_get,
710 .put = pcxhr_audio_src_put,
711};
712
713
714/*
715 * clock type selection
716 * enum pcxhr_clock_type {
Markus Bollingerc0193f32008-11-25 12:37:52 +0100717 * PCXHR_CLOCK_TYPE_INTERNAL = 0,
718 * PCXHR_CLOCK_TYPE_WORD_CLOCK,
719 * PCXHR_CLOCK_TYPE_AES_SYNC,
720 * PCXHR_CLOCK_TYPE_AES_1,
721 * PCXHR_CLOCK_TYPE_AES_2,
722 * PCXHR_CLOCK_TYPE_AES_3,
723 * PCXHR_CLOCK_TYPE_AES_4,
724 * PCXHR_CLOCK_TYPE_MAX = PCXHR_CLOCK_TYPE_AES_4,
725 * HR22_CLOCK_TYPE_INTERNAL = PCXHR_CLOCK_TYPE_INTERNAL,
726 * HR22_CLOCK_TYPE_AES_SYNC,
727 * HR22_CLOCK_TYPE_AES_1,
728 * HR22_CLOCK_TYPE_MAX = HR22_CLOCK_TYPE_AES_1,
729 * };
Markus Bollingere12229b2005-12-06 13:55:26 +0100730 */
731
732static int pcxhr_clock_type_info(struct snd_kcontrol *kcontrol,
733 struct snd_ctl_elem_info *uinfo)
734{
Markus Bollingerc0193f32008-11-25 12:37:52 +0100735 static const char *textsPCXHR[7] = {
736 "Internal", "WordClock", "AES Sync",
737 "AES 1", "AES 2", "AES 3", "AES 4"
Markus Bollingere12229b2005-12-06 13:55:26 +0100738 };
Markus Bollingerc0193f32008-11-25 12:37:52 +0100739 static const char *textsHR22[3] = {
740 "Internal", "AES Sync", "AES 1"
741 };
742 const char **texts;
Markus Bollingere12229b2005-12-06 13:55:26 +0100743 struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
Markus Bollingerc0193f32008-11-25 12:37:52 +0100744 int clock_items = 2; /* at least Internal and AES Sync clock */
745 if (mgr->board_has_aes1) {
746 clock_items += mgr->capture_chips; /* add AES x */
747 if (!mgr->is_hr_stereo)
748 clock_items += 1; /* add word clock */
749 }
750 if (mgr->is_hr_stereo) {
751 texts = textsHR22;
752 snd_BUG_ON(clock_items > (HR22_CLOCK_TYPE_MAX+1));
753 } else {
754 texts = textsPCXHR;
755 snd_BUG_ON(clock_items > (PCXHR_CLOCK_TYPE_MAX+1));
756 }
Markus Bollingere12229b2005-12-06 13:55:26 +0100757 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
758 uinfo->count = 1;
759 uinfo->value.enumerated.items = clock_items;
760 if (uinfo->value.enumerated.item >= clock_items)
761 uinfo->value.enumerated.item = clock_items-1;
762 strcpy(uinfo->value.enumerated.name,
763 texts[uinfo->value.enumerated.item]);
764 return 0;
765}
766
767static int pcxhr_clock_type_get(struct snd_kcontrol *kcontrol,
768 struct snd_ctl_elem_value *ucontrol)
769{
770 struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
771 ucontrol->value.enumerated.item[0] = mgr->use_clock_type;
772 return 0;
773}
774
775static int pcxhr_clock_type_put(struct snd_kcontrol *kcontrol,
776 struct snd_ctl_elem_value *ucontrol)
777{
778 struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
779 int rate, ret = 0;
Markus Bollingerc0193f32008-11-25 12:37:52 +0100780 unsigned int clock_items = 2; /* at least Internal and AES Sync clock */
781 if (mgr->board_has_aes1) {
782 clock_items += mgr->capture_chips; /* add AES x */
783 if (!mgr->is_hr_stereo)
784 clock_items += 1; /* add word clock */
785 }
Takashi Iwai4e98d6a2007-11-15 15:58:13 +0100786 if (ucontrol->value.enumerated.item[0] >= clock_items)
787 return -EINVAL;
Ingo Molnar62932df2006-01-16 16:34:20 +0100788 mutex_lock(&mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100789 if (mgr->use_clock_type != ucontrol->value.enumerated.item[0]) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100790 mutex_lock(&mgr->setup_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100791 mgr->use_clock_type = ucontrol->value.enumerated.item[0];
Markus Bollinger55aef452009-01-23 14:45:41 +0100792 rate = 0;
793 if (mgr->use_clock_type != PCXHR_CLOCK_TYPE_INTERNAL) {
Markus Bollingerc0193f32008-11-25 12:37:52 +0100794 pcxhr_get_external_clock(mgr, mgr->use_clock_type,
795 &rate);
Markus Bollinger55aef452009-01-23 14:45:41 +0100796 } else {
Markus Bollingere12229b2005-12-06 13:55:26 +0100797 rate = mgr->sample_rate;
Markus Bollinger55aef452009-01-23 14:45:41 +0100798 if (!rate)
799 rate = 48000;
800 }
Markus Bollingere12229b2005-12-06 13:55:26 +0100801 if (rate) {
802 pcxhr_set_clock(mgr, rate);
803 if (mgr->sample_rate)
804 mgr->sample_rate = rate;
805 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100806 mutex_unlock(&mgr->setup_mutex);
Markus Bollingerc0193f32008-11-25 12:37:52 +0100807 ret = 1; /* return 1 even if the set was not done. ok ? */
Markus Bollingere12229b2005-12-06 13:55:26 +0100808 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100809 mutex_unlock(&mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100810 return ret;
811}
812
813static struct snd_kcontrol_new pcxhr_control_clock_type = {
814 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
815 .name = "Clock Mode",
816 .info = pcxhr_clock_type_info,
817 .get = pcxhr_clock_type_get,
818 .put = pcxhr_clock_type_put,
819};
820
821/*
822 * clock rate control
823 * specific control that scans the sample rates on the external plugs
824 */
825static int pcxhr_clock_rate_info(struct snd_kcontrol *kcontrol,
826 struct snd_ctl_elem_info *uinfo)
827{
828 struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
829 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
830 uinfo->count = 3 + mgr->capture_chips;
831 uinfo->value.integer.min = 0; /* clock not present */
832 uinfo->value.integer.max = 192000; /* max sample rate 192 kHz */
833 return 0;
834}
835
836static int pcxhr_clock_rate_get(struct snd_kcontrol *kcontrol,
837 struct snd_ctl_elem_value *ucontrol)
838{
839 struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
840 int i, err, rate;
841
Ingo Molnar62932df2006-01-16 16:34:20 +0100842 mutex_lock(&mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100843 for(i = 0; i < 3 + mgr->capture_chips; i++) {
844 if (i == PCXHR_CLOCK_TYPE_INTERNAL)
845 rate = mgr->sample_rate_real;
846 else {
847 err = pcxhr_get_external_clock(mgr, i, &rate);
848 if (err)
849 break;
850 }
851 ucontrol->value.integer.value[i] = rate;
852 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100853 mutex_unlock(&mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100854 return 0;
855}
856
857static struct snd_kcontrol_new pcxhr_control_clock_rate = {
858 .access = SNDRV_CTL_ELEM_ACCESS_READ,
859 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
860 .name = "Clock Rates",
861 .info = pcxhr_clock_rate_info,
862 .get = pcxhr_clock_rate_get,
863};
864
865/*
866 * IEC958 status bits
867 */
Markus Bollingerc0193f32008-11-25 12:37:52 +0100868static int pcxhr_iec958_info(struct snd_kcontrol *kcontrol,
869 struct snd_ctl_elem_info *uinfo)
Markus Bollingere12229b2005-12-06 13:55:26 +0100870{
871 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
872 uinfo->count = 1;
873 return 0;
874}
875
Markus Bollingerc0193f32008-11-25 12:37:52 +0100876static int pcxhr_iec958_capture_byte(struct snd_pcxhr *chip,
877 int aes_idx, unsigned char *aes_bits)
Markus Bollingere12229b2005-12-06 13:55:26 +0100878{
879 int i, err;
880 unsigned char temp;
881 struct pcxhr_rmh rmh;
882
883 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ);
884 rmh.cmd[0] |= IO_NUM_UER_CHIP_REG;
885 switch (chip->chip_idx) {
Markus Bollingerc0193f32008-11-25 12:37:52 +0100886 /* instead of CS8420_01_CS use CS8416_01_CS for AES SYNC plug */
887 case 0: rmh.cmd[1] = CS8420_01_CS; break;
Markus Bollingere12229b2005-12-06 13:55:26 +0100888 case 1: rmh.cmd[1] = CS8420_23_CS; break;
889 case 2: rmh.cmd[1] = CS8420_45_CS; break;
890 case 3: rmh.cmd[1] = CS8420_67_CS; break;
891 default: return -EINVAL;
892 }
Markus Bollingerc0193f32008-11-25 12:37:52 +0100893 if (chip->mgr->board_aes_in_192k) {
894 switch (aes_idx) {
895 case 0: rmh.cmd[2] = CS8416_CSB0; break;
896 case 1: rmh.cmd[2] = CS8416_CSB1; break;
897 case 2: rmh.cmd[2] = CS8416_CSB2; break;
898 case 3: rmh.cmd[2] = CS8416_CSB3; break;
899 case 4: rmh.cmd[2] = CS8416_CSB4; break;
900 default: return -EINVAL;
901 }
902 } else {
903 switch (aes_idx) {
904 /* instead of CS8420_CSB0 use CS8416_CSBx for AES SYNC plug */
905 case 0: rmh.cmd[2] = CS8420_CSB0; break;
906 case 1: rmh.cmd[2] = CS8420_CSB1; break;
907 case 2: rmh.cmd[2] = CS8420_CSB2; break;
908 case 3: rmh.cmd[2] = CS8420_CSB3; break;
909 case 4: rmh.cmd[2] = CS8420_CSB4; break;
910 default: return -EINVAL;
911 }
Markus Bollingere12229b2005-12-06 13:55:26 +0100912 }
Markus Bollingerc0193f32008-11-25 12:37:52 +0100913 /* size and code the chip id for the fpga */
914 rmh.cmd[1] &= 0x0fffff;
915 /* chip signature + map for spi read */
916 rmh.cmd[2] &= CHIP_SIG_AND_MAP_SPI;
Markus Bollingere12229b2005-12-06 13:55:26 +0100917 rmh.cmd_len = 3;
918 err = pcxhr_send_msg(chip->mgr, &rmh);
919 if (err)
920 return err;
Markus Bollingerc0193f32008-11-25 12:37:52 +0100921
922 if (chip->mgr->board_aes_in_192k) {
923 temp = (unsigned char)rmh.stat[1];
924 } else {
925 temp = 0;
926 /* reversed bit order (not with CS8416_01_CS) */
927 for (i = 0; i < 8; i++) {
928 temp <<= 1;
929 if (rmh.stat[1] & (1 << i))
930 temp |= 1;
931 }
Markus Bollingere12229b2005-12-06 13:55:26 +0100932 }
Markus Bollingerc0193f32008-11-25 12:37:52 +0100933 snd_printdd("read iec958 AES %d byte %d = 0x%x\n",
934 chip->chip_idx, aes_idx, temp);
Markus Bollingere12229b2005-12-06 13:55:26 +0100935 *aes_bits = temp;
936 return 0;
937}
938
Markus Bollingerc0193f32008-11-25 12:37:52 +0100939static int pcxhr_iec958_get(struct snd_kcontrol *kcontrol,
940 struct snd_ctl_elem_value *ucontrol)
Markus Bollingere12229b2005-12-06 13:55:26 +0100941{
942 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
943 unsigned char aes_bits;
944 int i, err;
945
Ingo Molnar62932df2006-01-16 16:34:20 +0100946 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100947 for(i = 0; i < 5; i++) {
948 if (kcontrol->private_value == 0) /* playback */
949 aes_bits = chip->aes_bits[i];
950 else { /* capture */
Markus Bollingerc0193f32008-11-25 12:37:52 +0100951 if (chip->mgr->is_hr_stereo)
952 err = hr222_iec958_capture_byte(chip, i,
953 &aes_bits);
954 else
955 err = pcxhr_iec958_capture_byte(chip, i,
956 &aes_bits);
Markus Bollingere12229b2005-12-06 13:55:26 +0100957 if (err)
958 break;
959 }
960 ucontrol->value.iec958.status[i] = aes_bits;
961 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100962 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +0100963 return 0;
964}
965
966static int pcxhr_iec958_mask_get(struct snd_kcontrol *kcontrol,
967 struct snd_ctl_elem_value *ucontrol)
968{
969 int i;
970 for (i = 0; i < 5; i++)
971 ucontrol->value.iec958.status[i] = 0xff;
972 return 0;
973}
974
Markus Bollingerc0193f32008-11-25 12:37:52 +0100975static int pcxhr_iec958_update_byte(struct snd_pcxhr *chip,
976 int aes_idx, unsigned char aes_bits)
Markus Bollingere12229b2005-12-06 13:55:26 +0100977{
978 int i, err, cmd;
979 unsigned char new_bits = aes_bits;
980 unsigned char old_bits = chip->aes_bits[aes_idx];
981 struct pcxhr_rmh rmh;
982
983 for (i = 0; i < 8; i++) {
984 if ((old_bits & 0x01) != (new_bits & 0x01)) {
Markus Bollingerc0193f32008-11-25 12:37:52 +0100985 cmd = chip->chip_idx & 0x03; /* chip index 0..3 */
986 if (chip->chip_idx > 3)
Markus Bollingere12229b2005-12-06 13:55:26 +0100987 /* new bit used if chip_idx>3 (PCX1222HR) */
988 cmd |= 1 << 22;
Markus Bollingerc0193f32008-11-25 12:37:52 +0100989 cmd |= ((aes_idx << 3) + i) << 2; /* add bit offset */
990 cmd |= (new_bits & 0x01) << 23; /* add bit value */
Markus Bollingere12229b2005-12-06 13:55:26 +0100991 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
992 rmh.cmd[0] |= IO_NUM_REG_CUER;
993 rmh.cmd[1] = cmd;
994 rmh.cmd_len = 2;
995 snd_printdd("write iec958 AES %d byte %d bit %d (cmd %x)\n",
996 chip->chip_idx, aes_idx, i, cmd);
997 err = pcxhr_send_msg(chip->mgr, &rmh);
998 if (err)
999 return err;
1000 }
1001 old_bits >>= 1;
1002 new_bits >>= 1;
1003 }
1004 chip->aes_bits[aes_idx] = aes_bits;
1005 return 0;
1006}
1007
1008static int pcxhr_iec958_put(struct snd_kcontrol *kcontrol,
1009 struct snd_ctl_elem_value *ucontrol)
1010{
1011 struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
1012 int i, changed = 0;
1013
1014 /* playback */
Ingo Molnar62932df2006-01-16 16:34:20 +01001015 mutex_lock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +01001016 for (i = 0; i < 5; i++) {
1017 if (ucontrol->value.iec958.status[i] != chip->aes_bits[i]) {
Markus Bollingerc0193f32008-11-25 12:37:52 +01001018 if (chip->mgr->is_hr_stereo)
1019 hr222_iec958_update_byte(chip, i,
1020 ucontrol->value.iec958.status[i]);
1021 else
1022 pcxhr_iec958_update_byte(chip, i,
1023 ucontrol->value.iec958.status[i]);
Markus Bollingere12229b2005-12-06 13:55:26 +01001024 changed = 1;
1025 }
1026 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001027 mutex_unlock(&chip->mgr->mixer_mutex);
Markus Bollingere12229b2005-12-06 13:55:26 +01001028 return changed;
1029}
1030
1031static struct snd_kcontrol_new pcxhr_control_playback_iec958_mask = {
1032 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1033 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1034 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1035 .info = pcxhr_iec958_info,
1036 .get = pcxhr_iec958_mask_get
1037};
1038static struct snd_kcontrol_new pcxhr_control_playback_iec958 = {
1039 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1040 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1041 .info = pcxhr_iec958_info,
1042 .get = pcxhr_iec958_get,
1043 .put = pcxhr_iec958_put,
1044 .private_value = 0 /* playback */
1045};
1046
1047static struct snd_kcontrol_new pcxhr_control_capture_iec958_mask = {
1048 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1049 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1050 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK),
1051 .info = pcxhr_iec958_info,
1052 .get = pcxhr_iec958_mask_get
1053};
1054static struct snd_kcontrol_new pcxhr_control_capture_iec958 = {
1055 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1056 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1057 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1058 .info = pcxhr_iec958_info,
1059 .get = pcxhr_iec958_get,
1060 .private_value = 1 /* capture */
1061};
1062
1063static void pcxhr_init_audio_levels(struct snd_pcxhr *chip)
1064{
1065 int i;
1066
1067 for (i = 0; i < 2; i++) {
1068 if (chip->nb_streams_play) {
1069 int j;
1070 /* at boot time the digital volumes are unmuted 0dB */
1071 for (j = 0; j < PCXHR_PLAYBACK_STREAMS; j++) {
1072 chip->digital_playback_active[j][i] = 1;
Markus Bollingerc0193f32008-11-25 12:37:52 +01001073 chip->digital_playback_volume[j][i] =
1074 PCXHR_DIGITAL_ZERO_LEVEL;
Markus Bollingere12229b2005-12-06 13:55:26 +01001075 }
Markus Bollingerc0193f32008-11-25 12:37:52 +01001076 /* after boot, only two bits are set on the uer
1077 * interface
1078 */
1079 chip->aes_bits[0] = (IEC958_AES0_PROFESSIONAL |
1080 IEC958_AES0_PRO_FS_48000);
Markus Bollingere12229b2005-12-06 13:55:26 +01001081#ifdef CONFIG_SND_DEBUG
Markus Bollingerc0193f32008-11-25 12:37:52 +01001082 /* analog volumes for playback
1083 * (is LEVEL_MIN after boot)
1084 */
Markus Bollingere12229b2005-12-06 13:55:26 +01001085 chip->analog_playback_active[i] = 1;
Markus Bollingerc0193f32008-11-25 12:37:52 +01001086 if (chip->mgr->is_hr_stereo)
1087 chip->analog_playback_volume[i] =
1088 HR222_LINE_PLAYBACK_ZERO_LEVEL;
1089 else {
1090 chip->analog_playback_volume[i] =
1091 PCXHR_LINE_PLAYBACK_ZERO_LEVEL;
1092 pcxhr_update_analog_audio_level(chip, 0, i);
1093 }
Markus Bollingere12229b2005-12-06 13:55:26 +01001094#endif
Markus Bollingerc0193f32008-11-25 12:37:52 +01001095 /* stereo cards need to be initialised after boot */
1096 if (chip->mgr->is_hr_stereo)
1097 hr222_update_analog_audio_level(chip, 0, i);
Markus Bollingere12229b2005-12-06 13:55:26 +01001098 }
1099 if (chip->nb_streams_capt) {
1100 /* at boot time the digital volumes are unmuted 0dB */
Markus Bollingerc0193f32008-11-25 12:37:52 +01001101 chip->digital_capture_volume[i] =
1102 PCXHR_DIGITAL_ZERO_LEVEL;
1103 chip->analog_capture_active = 1;
Markus Bollingere12229b2005-12-06 13:55:26 +01001104#ifdef CONFIG_SND_DEBUG
Markus Bollingerc0193f32008-11-25 12:37:52 +01001105 /* analog volumes for playback
1106 * (is LEVEL_MIN after boot)
1107 */
1108 if (chip->mgr->is_hr_stereo)
1109 chip->analog_capture_volume[i] =
1110 HR222_LINE_CAPTURE_ZERO_LEVEL;
1111 else {
1112 chip->analog_capture_volume[i] =
1113 PCXHR_LINE_CAPTURE_ZERO_LEVEL;
1114 pcxhr_update_analog_audio_level(chip, 1, i);
1115 }
Markus Bollingere12229b2005-12-06 13:55:26 +01001116#endif
Markus Bollingerc0193f32008-11-25 12:37:52 +01001117 /* stereo cards need to be initialised after boot */
1118 if (chip->mgr->is_hr_stereo)
1119 hr222_update_analog_audio_level(chip, 1, i);
Markus Bollingere12229b2005-12-06 13:55:26 +01001120 }
1121 }
1122
1123 return;
1124}
1125
1126
1127int pcxhr_create_mixer(struct pcxhr_mgr *mgr)
1128{
1129 struct snd_pcxhr *chip;
1130 int err, i;
1131
Ingo Molnar62932df2006-01-16 16:34:20 +01001132 mutex_init(&mgr->mixer_mutex); /* can be in another place */
Markus Bollingere12229b2005-12-06 13:55:26 +01001133
1134 for (i = 0; i < mgr->num_cards; i++) {
1135 struct snd_kcontrol_new temp;
1136 chip = mgr->chip[i];
1137
1138 if (chip->nb_streams_play) {
1139 /* analog output level control */
1140 temp = pcxhr_control_analog_level;
1141 temp.name = "Master Playback Volume";
1142 temp.private_value = 0; /* playback */
Markus Bollingerc0193f32008-11-25 12:37:52 +01001143 if (mgr->is_hr_stereo)
1144 temp.tlv.p = db_scale_a_hr222_playback;
1145 else
1146 temp.tlv.p = db_scale_analog_playback;
1147 err = snd_ctl_add(chip->card,
1148 snd_ctl_new1(&temp, chip));
1149 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001150 return err;
Markus Bollingerc0193f32008-11-25 12:37:52 +01001151
Markus Bollingere12229b2005-12-06 13:55:26 +01001152 /* output mute controls */
Markus Bollingerc0193f32008-11-25 12:37:52 +01001153 err = snd_ctl_add(chip->card,
1154 snd_ctl_new1(&pcxhr_control_output_switch,
1155 chip));
1156 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001157 return err;
Markus Bollingerc0193f32008-11-25 12:37:52 +01001158
Markus Bollingere12229b2005-12-06 13:55:26 +01001159 temp = snd_pcxhr_pcm_vol;
1160 temp.name = "PCM Playback Volume";
1161 temp.count = PCXHR_PLAYBACK_STREAMS;
1162 temp.private_value = 0; /* playback */
Markus Bollingerc0193f32008-11-25 12:37:52 +01001163 err = snd_ctl_add(chip->card,
1164 snd_ctl_new1(&temp, chip));
1165 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001166 return err;
1167
Markus Bollingerc0193f32008-11-25 12:37:52 +01001168 err = snd_ctl_add(chip->card,
1169 snd_ctl_new1(&pcxhr_control_pcm_switch, chip));
1170 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001171 return err;
1172
1173 /* IEC958 controls */
Markus Bollingerc0193f32008-11-25 12:37:52 +01001174 err = snd_ctl_add(chip->card,
1175 snd_ctl_new1(&pcxhr_control_playback_iec958_mask,
1176 chip));
1177 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001178 return err;
Markus Bollingerc0193f32008-11-25 12:37:52 +01001179
1180 err = snd_ctl_add(chip->card,
1181 snd_ctl_new1(&pcxhr_control_playback_iec958,
1182 chip));
1183 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001184 return err;
1185 }
1186 if (chip->nb_streams_capt) {
Markus Bollingerc0193f32008-11-25 12:37:52 +01001187 /* analog input level control */
Markus Bollingere12229b2005-12-06 13:55:26 +01001188 temp = pcxhr_control_analog_level;
Markus Bollingerc0193f32008-11-25 12:37:52 +01001189 temp.name = "Line Capture Volume";
Markus Bollingere12229b2005-12-06 13:55:26 +01001190 temp.private_value = 1; /* capture */
Markus Bollingerc0193f32008-11-25 12:37:52 +01001191 if (mgr->is_hr_stereo)
1192 temp.tlv.p = db_scale_a_hr222_capture;
1193 else
1194 temp.tlv.p = db_scale_analog_capture;
1195
1196 err = snd_ctl_add(chip->card,
1197 snd_ctl_new1(&temp, chip));
1198 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001199 return err;
1200
1201 temp = snd_pcxhr_pcm_vol;
1202 temp.name = "PCM Capture Volume";
1203 temp.count = 1;
1204 temp.private_value = 1; /* capture */
Markus Bollingerc0193f32008-11-25 12:37:52 +01001205
1206 err = snd_ctl_add(chip->card,
1207 snd_ctl_new1(&temp, chip));
1208 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001209 return err;
Markus Bollingerc0193f32008-11-25 12:37:52 +01001210
Markus Bollingere12229b2005-12-06 13:55:26 +01001211 /* Audio source */
Markus Bollingerc0193f32008-11-25 12:37:52 +01001212 err = snd_ctl_add(chip->card,
1213 snd_ctl_new1(&pcxhr_control_audio_src, chip));
1214 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001215 return err;
Markus Bollingerc0193f32008-11-25 12:37:52 +01001216
Markus Bollingere12229b2005-12-06 13:55:26 +01001217 /* IEC958 controls */
Markus Bollingerc0193f32008-11-25 12:37:52 +01001218 err = snd_ctl_add(chip->card,
1219 snd_ctl_new1(&pcxhr_control_capture_iec958_mask,
1220 chip));
1221 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001222 return err;
Markus Bollingerc0193f32008-11-25 12:37:52 +01001223
1224 err = snd_ctl_add(chip->card,
1225 snd_ctl_new1(&pcxhr_control_capture_iec958,
1226 chip));
1227 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001228 return err;
Markus Bollingerc0193f32008-11-25 12:37:52 +01001229
1230 if (mgr->is_hr_stereo) {
1231 err = hr222_add_mic_controls(chip);
1232 if (err < 0)
1233 return err;
1234 }
Markus Bollingere12229b2005-12-06 13:55:26 +01001235 }
1236 /* monitoring only if playback and capture device available */
1237 if (chip->nb_streams_capt > 0 && chip->nb_streams_play > 0) {
1238 /* monitoring */
Markus Bollingerc0193f32008-11-25 12:37:52 +01001239 err = snd_ctl_add(chip->card,
1240 snd_ctl_new1(&pcxhr_control_monitor_vol, chip));
1241 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001242 return err;
Markus Bollingerc0193f32008-11-25 12:37:52 +01001243
1244 err = snd_ctl_add(chip->card,
1245 snd_ctl_new1(&pcxhr_control_monitor_sw, chip));
1246 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001247 return err;
1248 }
1249
1250 if (i == 0) {
1251 /* clock mode only one control per pcxhr */
Markus Bollingerc0193f32008-11-25 12:37:52 +01001252 err = snd_ctl_add(chip->card,
1253 snd_ctl_new1(&pcxhr_control_clock_type, mgr));
1254 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001255 return err;
Markus Bollingerc0193f32008-11-25 12:37:52 +01001256 /* non standard control used to scan
1257 * the external clock presence/frequencies
1258 */
1259 err = snd_ctl_add(chip->card,
1260 snd_ctl_new1(&pcxhr_control_clock_rate, mgr));
1261 if (err < 0)
Markus Bollingere12229b2005-12-06 13:55:26 +01001262 return err;
1263 }
1264
1265 /* init values for the mixer data */
1266 pcxhr_init_audio_levels(chip);
1267 }
1268
1269 return 0;
1270}