blob: d327c36fbddfb73fead535daf0d148178e41118b [file] [log] [blame]
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001/*
2 * C-Media CMI8788 driver - mixer code
3 *
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2.
9 *
10 * This driver is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this driver; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Clemens Ladischd0ce9942007-12-23 19:50:57 +010020#include <linux/mutex.h>
21#include <sound/ac97_codec.h>
22#include <sound/asoundef.h>
23#include <sound/control.h>
24#include <sound/tlv.h>
25#include "oxygen.h"
Clemens Ladisch878ac3e2008-01-21 08:50:19 +010026#include "cm9780.h"
Clemens Ladischd0ce9942007-12-23 19:50:57 +010027
28static int dac_volume_info(struct snd_kcontrol *ctl,
29 struct snd_ctl_elem_info *info)
30{
Clemens Ladisch976cd622008-01-25 08:37:49 +010031 struct oxygen *chip = ctl->private_data;
32
Clemens Ladischd0ce9942007-12-23 19:50:57 +010033 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Clemens Ladisch1f4d7be2011-01-10 15:59:38 +010034 info->count = chip->model.dac_channels_mixer;
Clemens Ladisch9bd6a732008-09-22 08:55:19 +020035 info->value.integer.min = chip->model.dac_volume_min;
36 info->value.integer.max = chip->model.dac_volume_max;
Clemens Ladischd0ce9942007-12-23 19:50:57 +010037 return 0;
38}
39
40static int dac_volume_get(struct snd_kcontrol *ctl,
41 struct snd_ctl_elem_value *value)
42{
43 struct oxygen *chip = ctl->private_data;
44 unsigned int i;
45
46 mutex_lock(&chip->mutex);
Clemens Ladisch1f4d7be2011-01-10 15:59:38 +010047 for (i = 0; i < chip->model.dac_channels_mixer; ++i)
Clemens Ladischd0ce9942007-12-23 19:50:57 +010048 value->value.integer.value[i] = chip->dac_volume[i];
49 mutex_unlock(&chip->mutex);
50 return 0;
51}
52
53static int dac_volume_put(struct snd_kcontrol *ctl,
54 struct snd_ctl_elem_value *value)
55{
56 struct oxygen *chip = ctl->private_data;
57 unsigned int i;
58 int changed;
59
60 changed = 0;
61 mutex_lock(&chip->mutex);
Clemens Ladisch1f4d7be2011-01-10 15:59:38 +010062 for (i = 0; i < chip->model.dac_channels_mixer; ++i)
Clemens Ladischd0ce9942007-12-23 19:50:57 +010063 if (value->value.integer.value[i] != chip->dac_volume[i]) {
64 chip->dac_volume[i] = value->value.integer.value[i];
65 changed = 1;
66 }
67 if (changed)
Clemens Ladisch9bd6a732008-09-22 08:55:19 +020068 chip->model.update_dac_volume(chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +010069 mutex_unlock(&chip->mutex);
70 return changed;
71}
72
73static int dac_mute_get(struct snd_kcontrol *ctl,
74 struct snd_ctl_elem_value *value)
75{
76 struct oxygen *chip = ctl->private_data;
77
78 mutex_lock(&chip->mutex);
79 value->value.integer.value[0] = !chip->dac_mute;
80 mutex_unlock(&chip->mutex);
81 return 0;
82}
83
84static int dac_mute_put(struct snd_kcontrol *ctl,
85 struct snd_ctl_elem_value *value)
86{
87 struct oxygen *chip = ctl->private_data;
88 int changed;
89
90 mutex_lock(&chip->mutex);
91 changed = !value->value.integer.value[0] != chip->dac_mute;
92 if (changed) {
93 chip->dac_mute = !value->value.integer.value[0];
Clemens Ladisch9bd6a732008-09-22 08:55:19 +020094 chip->model.update_dac_mute(chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +010095 }
96 mutex_unlock(&chip->mutex);
97 return changed;
98}
99
Clemens Ladisch66410bf2011-01-10 16:20:29 +0100100static unsigned int upmix_item_count(struct oxygen *chip)
101{
102 if (chip->model.dac_channels_pcm < 8)
103 return 2;
104 else if (chip->model.update_center_lfe_mix)
105 return 5;
106 else
107 return 3;
108}
109
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100110static int upmix_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
111{
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200112 static const char *const names[5] = {
113 "Front",
114 "Front+Surround",
115 "Front+Surround+Back",
116 "Front+Surround+Center/LFE",
117 "Front+Surround+Center/LFE+Back",
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100118 };
Clemens Ladisch976cd622008-01-25 08:37:49 +0100119 struct oxygen *chip = ctl->private_data;
Clemens Ladisch66410bf2011-01-10 16:20:29 +0100120 unsigned int count = upmix_item_count(chip);
Clemens Ladisch976cd622008-01-25 08:37:49 +0100121
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100122 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
123 info->count = 1;
Clemens Ladisch976cd622008-01-25 08:37:49 +0100124 info->value.enumerated.items = count;
125 if (info->value.enumerated.item >= count)
126 info->value.enumerated.item = count - 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100127 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
128 return 0;
129}
130
131static int upmix_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
132{
133 struct oxygen *chip = ctl->private_data;
134
135 mutex_lock(&chip->mutex);
136 value->value.enumerated.item[0] = chip->dac_routing;
137 mutex_unlock(&chip->mutex);
138 return 0;
139}
140
141void oxygen_update_dac_routing(struct oxygen *chip)
142{
Clemens Ladischc9946b22008-01-21 08:44:24 +0100143 /* DAC 0: front, DAC 1: surround, DAC 2: center/LFE, DAC 3: back */
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200144 static const unsigned int reg_values[5] = {
Clemens Ladischc9946b22008-01-21 08:44:24 +0100145 /* stereo -> front */
146 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
147 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
148 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
149 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
150 /* stereo -> front+surround */
151 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
152 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
153 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
154 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
155 /* stereo -> front+surround+back */
156 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
157 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
158 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
159 (0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200160 /* stereo -> front+surround+center/LFE */
161 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
162 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
163 (0 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
164 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
165 /* stereo -> front+surround+center/LFE+back */
166 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
167 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
168 (0 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
169 (0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100170 };
Clemens Ladisch7113e952008-01-14 08:55:03 +0100171 u8 channels;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100172 unsigned int reg_value;
173
Clemens Ladisch7113e952008-01-14 08:55:03 +0100174 channels = oxygen_read8(chip, OXYGEN_PLAY_CHANNELS) &
175 OXYGEN_PLAY_CHANNELS_MASK;
176 if (channels == OXYGEN_PLAY_CHANNELS_2)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100177 reg_value = reg_values[chip->dac_routing];
Clemens Ladisch7113e952008-01-14 08:55:03 +0100178 else if (channels == OXYGEN_PLAY_CHANNELS_8)
Clemens Ladischc9946b22008-01-21 08:44:24 +0100179 /* in 7.1 mode, "rear" channels go to the "back" jack */
180 reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
181 (3 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
182 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
183 (1 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100184 else
Clemens Ladischc9946b22008-01-21 08:44:24 +0100185 reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
186 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
187 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
188 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT);
189 oxygen_write16_masked(chip, OXYGEN_PLAY_ROUTING, reg_value,
190 OXYGEN_PLAY_DAC0_SOURCE_MASK |
191 OXYGEN_PLAY_DAC1_SOURCE_MASK |
192 OXYGEN_PLAY_DAC2_SOURCE_MASK |
193 OXYGEN_PLAY_DAC3_SOURCE_MASK);
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200194 if (chip->model.update_center_lfe_mix)
195 chip->model.update_center_lfe_mix(chip, chip->dac_routing > 2);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100196}
197
198static int upmix_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
199{
200 struct oxygen *chip = ctl->private_data;
Clemens Ladisch66410bf2011-01-10 16:20:29 +0100201 unsigned int count = upmix_item_count(chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100202 int changed;
203
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200204 if (value->value.enumerated.item[0] >= count)
205 return -EINVAL;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100206 mutex_lock(&chip->mutex);
207 changed = value->value.enumerated.item[0] != chip->dac_routing;
208 if (changed) {
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200209 chip->dac_routing = value->value.enumerated.item[0];
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100210 oxygen_update_dac_routing(chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100211 }
212 mutex_unlock(&chip->mutex);
213 return changed;
214}
215
216static int spdif_switch_get(struct snd_kcontrol *ctl,
217 struct snd_ctl_elem_value *value)
218{
219 struct oxygen *chip = ctl->private_data;
220
221 mutex_lock(&chip->mutex);
222 value->value.integer.value[0] = chip->spdif_playback_enable;
223 mutex_unlock(&chip->mutex);
224 return 0;
225}
226
227static unsigned int oxygen_spdif_rate(unsigned int oxygen_rate)
228{
229 switch (oxygen_rate) {
230 case OXYGEN_RATE_32000:
231 return IEC958_AES3_CON_FS_32000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
232 case OXYGEN_RATE_44100:
233 return IEC958_AES3_CON_FS_44100 << OXYGEN_SPDIF_CS_RATE_SHIFT;
234 default: /* OXYGEN_RATE_48000 */
235 return IEC958_AES3_CON_FS_48000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
236 case OXYGEN_RATE_64000:
237 return 0xb << OXYGEN_SPDIF_CS_RATE_SHIFT;
238 case OXYGEN_RATE_88200:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200239 return IEC958_AES3_CON_FS_88200 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100240 case OXYGEN_RATE_96000:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200241 return IEC958_AES3_CON_FS_96000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100242 case OXYGEN_RATE_176400:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200243 return IEC958_AES3_CON_FS_176400 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100244 case OXYGEN_RATE_192000:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200245 return IEC958_AES3_CON_FS_192000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100246 }
247}
248
249void oxygen_update_spdif_source(struct oxygen *chip)
250{
251 u32 old_control, new_control;
252 u16 old_routing, new_routing;
253 unsigned int oxygen_rate;
254
255 old_control = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
256 old_routing = oxygen_read16(chip, OXYGEN_PLAY_ROUTING);
257 if (chip->pcm_active & (1 << PCM_SPDIF)) {
258 new_control = old_control | OXYGEN_SPDIF_OUT_ENABLE;
Clemens Ladischc9946b22008-01-21 08:44:24 +0100259 new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
260 | OXYGEN_PLAY_SPDIF_SPDIF;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100261 oxygen_rate = (old_control >> OXYGEN_SPDIF_OUT_RATE_SHIFT)
262 & OXYGEN_I2S_RATE_MASK;
263 /* S/PDIF rate was already set by the caller */
264 } else if ((chip->pcm_active & (1 << PCM_MULTICH)) &&
265 chip->spdif_playback_enable) {
Clemens Ladischc9946b22008-01-21 08:44:24 +0100266 new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
267 | OXYGEN_PLAY_SPDIF_MULTICH_01;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100268 oxygen_rate = oxygen_read16(chip, OXYGEN_I2S_MULTICH_FORMAT)
269 & OXYGEN_I2S_RATE_MASK;
270 new_control = (old_control & ~OXYGEN_SPDIF_OUT_RATE_MASK) |
271 (oxygen_rate << OXYGEN_SPDIF_OUT_RATE_SHIFT) |
272 OXYGEN_SPDIF_OUT_ENABLE;
273 } else {
274 new_control = old_control & ~OXYGEN_SPDIF_OUT_ENABLE;
275 new_routing = old_routing;
276 oxygen_rate = OXYGEN_RATE_44100;
277 }
278 if (old_routing != new_routing) {
279 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL,
280 new_control & ~OXYGEN_SPDIF_OUT_ENABLE);
281 oxygen_write16(chip, OXYGEN_PLAY_ROUTING, new_routing);
282 }
283 if (new_control & OXYGEN_SPDIF_OUT_ENABLE)
284 oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS,
285 oxygen_spdif_rate(oxygen_rate) |
286 ((chip->pcm_active & (1 << PCM_SPDIF)) ?
287 chip->spdif_pcm_bits : chip->spdif_bits));
288 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, new_control);
289}
290
291static int spdif_switch_put(struct snd_kcontrol *ctl,
292 struct snd_ctl_elem_value *value)
293{
294 struct oxygen *chip = ctl->private_data;
295 int changed;
296
297 mutex_lock(&chip->mutex);
298 changed = value->value.integer.value[0] != chip->spdif_playback_enable;
299 if (changed) {
300 chip->spdif_playback_enable = !!value->value.integer.value[0];
301 spin_lock_irq(&chip->reg_lock);
302 oxygen_update_spdif_source(chip);
303 spin_unlock_irq(&chip->reg_lock);
304 }
305 mutex_unlock(&chip->mutex);
306 return changed;
307}
308
309static int spdif_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
310{
311 info->type = SNDRV_CTL_ELEM_TYPE_IEC958;
312 info->count = 1;
313 return 0;
314}
315
316static void oxygen_to_iec958(u32 bits, struct snd_ctl_elem_value *value)
317{
318 value->value.iec958.status[0] =
319 bits & (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C |
320 OXYGEN_SPDIF_PREEMPHASIS);
321 value->value.iec958.status[1] = /* category and original */
322 bits >> OXYGEN_SPDIF_CATEGORY_SHIFT;
323}
324
325static u32 iec958_to_oxygen(struct snd_ctl_elem_value *value)
326{
327 u32 bits;
328
329 bits = value->value.iec958.status[0] &
330 (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C |
331 OXYGEN_SPDIF_PREEMPHASIS);
332 bits |= value->value.iec958.status[1] << OXYGEN_SPDIF_CATEGORY_SHIFT;
333 if (bits & OXYGEN_SPDIF_NONAUDIO)
334 bits |= OXYGEN_SPDIF_V;
335 return bits;
336}
337
338static inline void write_spdif_bits(struct oxygen *chip, u32 bits)
339{
340 oxygen_write32_masked(chip, OXYGEN_SPDIF_OUTPUT_BITS, bits,
341 OXYGEN_SPDIF_NONAUDIO |
342 OXYGEN_SPDIF_C |
343 OXYGEN_SPDIF_PREEMPHASIS |
344 OXYGEN_SPDIF_CATEGORY_MASK |
345 OXYGEN_SPDIF_ORIGINAL |
346 OXYGEN_SPDIF_V);
347}
348
349static int spdif_default_get(struct snd_kcontrol *ctl,
350 struct snd_ctl_elem_value *value)
351{
352 struct oxygen *chip = ctl->private_data;
353
354 mutex_lock(&chip->mutex);
355 oxygen_to_iec958(chip->spdif_bits, value);
356 mutex_unlock(&chip->mutex);
357 return 0;
358}
359
360static int spdif_default_put(struct snd_kcontrol *ctl,
361 struct snd_ctl_elem_value *value)
362{
363 struct oxygen *chip = ctl->private_data;
364 u32 new_bits;
365 int changed;
366
367 new_bits = iec958_to_oxygen(value);
368 mutex_lock(&chip->mutex);
369 changed = new_bits != chip->spdif_bits;
370 if (changed) {
371 chip->spdif_bits = new_bits;
372 if (!(chip->pcm_active & (1 << PCM_SPDIF)))
373 write_spdif_bits(chip, new_bits);
374 }
375 mutex_unlock(&chip->mutex);
376 return changed;
377}
378
379static int spdif_mask_get(struct snd_kcontrol *ctl,
380 struct snd_ctl_elem_value *value)
381{
382 value->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
383 IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS;
384 value->value.iec958.status[1] =
385 IEC958_AES1_CON_CATEGORY | IEC958_AES1_CON_ORIGINAL;
386 return 0;
387}
388
389static int spdif_pcm_get(struct snd_kcontrol *ctl,
390 struct snd_ctl_elem_value *value)
391{
392 struct oxygen *chip = ctl->private_data;
393
394 mutex_lock(&chip->mutex);
395 oxygen_to_iec958(chip->spdif_pcm_bits, value);
396 mutex_unlock(&chip->mutex);
397 return 0;
398}
399
400static int spdif_pcm_put(struct snd_kcontrol *ctl,
401 struct snd_ctl_elem_value *value)
402{
403 struct oxygen *chip = ctl->private_data;
404 u32 new_bits;
405 int changed;
406
407 new_bits = iec958_to_oxygen(value);
408 mutex_lock(&chip->mutex);
409 changed = new_bits != chip->spdif_pcm_bits;
410 if (changed) {
411 chip->spdif_pcm_bits = new_bits;
412 if (chip->pcm_active & (1 << PCM_SPDIF))
413 write_spdif_bits(chip, new_bits);
414 }
415 mutex_unlock(&chip->mutex);
416 return changed;
417}
418
419static int spdif_input_mask_get(struct snd_kcontrol *ctl,
420 struct snd_ctl_elem_value *value)
421{
422 value->value.iec958.status[0] = 0xff;
423 value->value.iec958.status[1] = 0xff;
424 value->value.iec958.status[2] = 0xff;
425 value->value.iec958.status[3] = 0xff;
426 return 0;
427}
428
429static int spdif_input_default_get(struct snd_kcontrol *ctl,
430 struct snd_ctl_elem_value *value)
431{
432 struct oxygen *chip = ctl->private_data;
433 u32 bits;
434
435 bits = oxygen_read32(chip, OXYGEN_SPDIF_INPUT_BITS);
436 value->value.iec958.status[0] = bits;
437 value->value.iec958.status[1] = bits >> 8;
438 value->value.iec958.status[2] = bits >> 16;
439 value->value.iec958.status[3] = bits >> 24;
440 return 0;
441}
442
Clemens Ladisch02f21c92008-01-22 08:36:03 +0100443static int spdif_loopback_get(struct snd_kcontrol *ctl,
444 struct snd_ctl_elem_value *value)
445{
446 struct oxygen *chip = ctl->private_data;
447
448 value->value.integer.value[0] =
449 !!(oxygen_read32(chip, OXYGEN_SPDIF_CONTROL)
450 & OXYGEN_SPDIF_LOOPBACK);
451 return 0;
452}
453
454static int spdif_loopback_put(struct snd_kcontrol *ctl,
455 struct snd_ctl_elem_value *value)
456{
457 struct oxygen *chip = ctl->private_data;
458 u32 oldreg, newreg;
459 int changed;
460
461 spin_lock_irq(&chip->reg_lock);
462 oldreg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
463 if (value->value.integer.value[0])
464 newreg = oldreg | OXYGEN_SPDIF_LOOPBACK;
465 else
466 newreg = oldreg & ~OXYGEN_SPDIF_LOOPBACK;
467 changed = newreg != oldreg;
468 if (changed)
469 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, newreg);
470 spin_unlock_irq(&chip->reg_lock);
471 return changed;
472}
473
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100474static int monitor_volume_info(struct snd_kcontrol *ctl,
475 struct snd_ctl_elem_info *info)
476{
477 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
478 info->count = 1;
479 info->value.integer.min = 0;
480 info->value.integer.max = 1;
481 return 0;
482}
483
484static int monitor_get(struct snd_kcontrol *ctl,
485 struct snd_ctl_elem_value *value)
486{
487 struct oxygen *chip = ctl->private_data;
488 u8 bit = ctl->private_value;
489 int invert = ctl->private_value & (1 << 8);
490
491 value->value.integer.value[0] =
492 !!invert ^ !!(oxygen_read8(chip, OXYGEN_ADC_MONITOR) & bit);
493 return 0;
494}
495
496static int monitor_put(struct snd_kcontrol *ctl,
497 struct snd_ctl_elem_value *value)
498{
499 struct oxygen *chip = ctl->private_data;
500 u8 bit = ctl->private_value;
501 int invert = ctl->private_value & (1 << 8);
502 u8 oldreg, newreg;
503 int changed;
504
505 spin_lock_irq(&chip->reg_lock);
506 oldreg = oxygen_read8(chip, OXYGEN_ADC_MONITOR);
507 if ((!!value->value.integer.value[0] ^ !!invert) != 0)
508 newreg = oldreg | bit;
509 else
510 newreg = oldreg & ~bit;
511 changed = newreg != oldreg;
512 if (changed)
513 oxygen_write8(chip, OXYGEN_ADC_MONITOR, newreg);
514 spin_unlock_irq(&chip->reg_lock);
515 return changed;
516}
517
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100518static int ac97_switch_get(struct snd_kcontrol *ctl,
519 struct snd_ctl_elem_value *value)
520{
521 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100522 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100523 unsigned int index = ctl->private_value & 0xff;
524 unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
525 int invert = ctl->private_value & (1 << 16);
526 u16 reg;
527
528 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100529 reg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100530 mutex_unlock(&chip->mutex);
531 if (!(reg & (1 << bitnr)) ^ !invert)
532 value->value.integer.value[0] = 1;
533 else
534 value->value.integer.value[0] = 0;
535 return 0;
536}
537
Clemens Ladische97f7992008-04-01 10:02:18 +0200538static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
539{
Clemens Ladisch3d839e52008-08-26 11:06:26 +0200540 unsigned int priv_idx;
Clemens Ladische97f7992008-04-01 10:02:18 +0200541 u16 value;
542
Clemens Ladisch3d839e52008-08-26 11:06:26 +0200543 if (!chip->controls[control])
544 return;
545 priv_idx = chip->controls[control]->private_value & 0xff;
Clemens Ladische97f7992008-04-01 10:02:18 +0200546 value = oxygen_read_ac97(chip, 0, priv_idx);
547 if (!(value & 0x8000)) {
548 oxygen_write_ac97(chip, 0, priv_idx, value | 0x8000);
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200549 if (chip->model.ac97_switch)
550 chip->model.ac97_switch(chip, priv_idx, 0x8000);
Clemens Ladische97f7992008-04-01 10:02:18 +0200551 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
552 &chip->controls[control]->id);
553 }
554}
555
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100556static int ac97_switch_put(struct snd_kcontrol *ctl,
557 struct snd_ctl_elem_value *value)
558{
559 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100560 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100561 unsigned int index = ctl->private_value & 0xff;
562 unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
563 int invert = ctl->private_value & (1 << 16);
564 u16 oldreg, newreg;
565 int change;
566
567 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100568 oldreg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100569 newreg = oldreg;
570 if (!value->value.integer.value[0] ^ !invert)
571 newreg |= 1 << bitnr;
572 else
573 newreg &= ~(1 << bitnr);
574 change = newreg != oldreg;
575 if (change) {
Clemens Ladischa3601562008-01-28 08:35:20 +0100576 oxygen_write_ac97(chip, codec, index, newreg);
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200577 if (codec == 0 && chip->model.ac97_switch)
578 chip->model.ac97_switch(chip, index, newreg & 0x8000);
Clemens Ladische97f7992008-04-01 10:02:18 +0200579 if (index == AC97_LINE) {
580 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
581 newreg & 0x8000 ?
582 CM9780_GPO0 : 0, CM9780_GPO0);
583 if (!(newreg & 0x8000)) {
584 mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
585 mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
586 mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
587 }
588 } else if ((index == AC97_MIC || index == AC97_CD ||
589 index == AC97_VIDEO || index == AC97_AUX) &&
590 bitnr == 15 && !(newreg & 0x8000)) {
591 mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
592 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
593 CM9780_GPO0, CM9780_GPO0);
594 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100595 }
596 mutex_unlock(&chip->mutex);
597 return change;
598}
599
600static int ac97_volume_info(struct snd_kcontrol *ctl,
601 struct snd_ctl_elem_info *info)
602{
Clemens Ladisch14744d72009-06-25 14:28:49 +0200603 int stereo = (ctl->private_value >> 16) & 1;
604
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100605 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Clemens Ladisch14744d72009-06-25 14:28:49 +0200606 info->count = stereo ? 2 : 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100607 info->value.integer.min = 0;
608 info->value.integer.max = 0x1f;
609 return 0;
610}
611
612static int ac97_volume_get(struct snd_kcontrol *ctl,
613 struct snd_ctl_elem_value *value)
614{
615 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100616 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladisch14744d72009-06-25 14:28:49 +0200617 int stereo = (ctl->private_value >> 16) & 1;
Clemens Ladischa3601562008-01-28 08:35:20 +0100618 unsigned int index = ctl->private_value & 0xff;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100619 u16 reg;
620
621 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100622 reg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100623 mutex_unlock(&chip->mutex);
624 value->value.integer.value[0] = 31 - (reg & 0x1f);
Clemens Ladisch14744d72009-06-25 14:28:49 +0200625 if (stereo)
626 value->value.integer.value[1] = 31 - ((reg >> 8) & 0x1f);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100627 return 0;
628}
629
630static int ac97_volume_put(struct snd_kcontrol *ctl,
631 struct snd_ctl_elem_value *value)
632{
633 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100634 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladisch14744d72009-06-25 14:28:49 +0200635 int stereo = (ctl->private_value >> 16) & 1;
Clemens Ladischa3601562008-01-28 08:35:20 +0100636 unsigned int index = ctl->private_value & 0xff;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100637 u16 oldreg, newreg;
638 int change;
639
640 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100641 oldreg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100642 newreg = oldreg;
643 newreg = (newreg & ~0x1f) |
644 (31 - (value->value.integer.value[0] & 0x1f));
Clemens Ladisch14744d72009-06-25 14:28:49 +0200645 if (stereo)
646 newreg = (newreg & ~0x1f00) |
647 ((31 - (value->value.integer.value[1] & 0x1f)) << 8);
648 else
649 newreg = (newreg & ~0x1f00) | ((newreg & 0x1f) << 8);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100650 change = newreg != oldreg;
651 if (change)
Clemens Ladischa3601562008-01-28 08:35:20 +0100652 oxygen_write_ac97(chip, codec, index, newreg);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100653 mutex_unlock(&chip->mutex);
654 return change;
655}
656
Clemens Ladische96f38f2010-12-02 11:39:34 +0100657static int mic_fmic_source_info(struct snd_kcontrol *ctl,
658 struct snd_ctl_elem_info *info)
659{
660 static const char *const names[] = { "Mic Jack", "Front Panel" };
661
662 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
663 info->count = 1;
664 info->value.enumerated.items = 2;
665 info->value.enumerated.item &= 1;
666 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
667 return 0;
668}
669
670static int mic_fmic_source_get(struct snd_kcontrol *ctl,
671 struct snd_ctl_elem_value *value)
672{
673 struct oxygen *chip = ctl->private_data;
674
675 mutex_lock(&chip->mutex);
676 value->value.enumerated.item[0] =
677 !!(oxygen_read_ac97(chip, 0, CM9780_JACK) & CM9780_FMIC2MIC);
678 mutex_unlock(&chip->mutex);
679 return 0;
680}
681
682static int mic_fmic_source_put(struct snd_kcontrol *ctl,
683 struct snd_ctl_elem_value *value)
684{
685 struct oxygen *chip = ctl->private_data;
686 u16 oldreg, newreg;
687 int change;
688
689 mutex_lock(&chip->mutex);
690 oldreg = oxygen_read_ac97(chip, 0, CM9780_JACK);
691 if (value->value.enumerated.item[0])
692 newreg = oldreg | CM9780_FMIC2MIC;
693 else
694 newreg = oldreg & ~CM9780_FMIC2MIC;
695 change = newreg != oldreg;
696 if (change)
697 oxygen_write_ac97(chip, 0, CM9780_JACK, newreg);
698 mutex_unlock(&chip->mutex);
699 return change;
700}
701
Clemens Ladischa3601562008-01-28 08:35:20 +0100702static int ac97_fp_rec_volume_info(struct snd_kcontrol *ctl,
703 struct snd_ctl_elem_info *info)
704{
705 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
706 info->count = 2;
707 info->value.integer.min = 0;
708 info->value.integer.max = 7;
709 return 0;
710}
711
712static int ac97_fp_rec_volume_get(struct snd_kcontrol *ctl,
713 struct snd_ctl_elem_value *value)
714{
715 struct oxygen *chip = ctl->private_data;
716 u16 reg;
717
718 mutex_lock(&chip->mutex);
719 reg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
720 mutex_unlock(&chip->mutex);
721 value->value.integer.value[0] = reg & 7;
722 value->value.integer.value[1] = (reg >> 8) & 7;
723 return 0;
724}
725
726static int ac97_fp_rec_volume_put(struct snd_kcontrol *ctl,
727 struct snd_ctl_elem_value *value)
728{
729 struct oxygen *chip = ctl->private_data;
730 u16 oldreg, newreg;
731 int change;
732
733 mutex_lock(&chip->mutex);
734 oldreg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
735 newreg = oldreg & ~0x0707;
736 newreg = newreg | (value->value.integer.value[0] & 7);
737 newreg = newreg | ((value->value.integer.value[0] & 7) << 8);
738 change = newreg != oldreg;
739 if (change)
740 oxygen_write_ac97(chip, 1, AC97_REC_GAIN, newreg);
741 mutex_unlock(&chip->mutex);
742 return change;
743}
744
745#define AC97_SWITCH(xname, codec, index, bitnr, invert) { \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100746 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
747 .name = xname, \
748 .info = snd_ctl_boolean_mono_info, \
749 .get = ac97_switch_get, \
750 .put = ac97_switch_put, \
Clemens Ladischa3601562008-01-28 08:35:20 +0100751 .private_value = ((codec) << 24) | ((invert) << 16) | \
752 ((bitnr) << 8) | (index), \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100753 }
Clemens Ladisch14744d72009-06-25 14:28:49 +0200754#define AC97_VOLUME(xname, codec, index, stereo) { \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100755 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
756 .name = xname, \
Clemens Ladischa3601562008-01-28 08:35:20 +0100757 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
758 SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100759 .info = ac97_volume_info, \
760 .get = ac97_volume_get, \
761 .put = ac97_volume_put, \
762 .tlv = { .p = ac97_db_scale, }, \
Clemens Ladisch14744d72009-06-25 14:28:49 +0200763 .private_value = ((codec) << 24) | ((stereo) << 16) | (index), \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100764 }
765
Clemens Ladisch9a0b3792010-10-04 13:25:13 +0200766static DECLARE_TLV_DB_SCALE(monitor_db_scale, -600, 600, 0);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100767static DECLARE_TLV_DB_SCALE(ac97_db_scale, -3450, 150, 0);
Clemens Ladischa3601562008-01-28 08:35:20 +0100768static DECLARE_TLV_DB_SCALE(ac97_rec_db_scale, 0, 150, 0);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100769
770static const struct snd_kcontrol_new controls[] = {
771 {
772 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladischfb920b72008-01-15 08:39:06 +0100773 .name = "Master Playback Volume",
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100774 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100775 .info = dac_volume_info,
776 .get = dac_volume_get,
777 .put = dac_volume_put,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100778 },
779 {
780 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladischfb920b72008-01-15 08:39:06 +0100781 .name = "Master Playback Switch",
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100782 .info = snd_ctl_boolean_mono_info,
783 .get = dac_mute_get,
784 .put = dac_mute_put,
785 },
786 {
787 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
788 .name = "Stereo Upmixing",
789 .info = upmix_info,
790 .get = upmix_get,
791 .put = upmix_put,
792 },
793 {
794 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
795 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
796 .info = snd_ctl_boolean_mono_info,
797 .get = spdif_switch_get,
798 .put = spdif_switch_put,
799 },
800 {
801 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
802 .device = 1,
803 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
804 .info = spdif_info,
805 .get = spdif_default_get,
806 .put = spdif_default_put,
807 },
808 {
809 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
810 .device = 1,
811 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
812 .access = SNDRV_CTL_ELEM_ACCESS_READ,
813 .info = spdif_info,
814 .get = spdif_mask_get,
815 },
816 {
817 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
818 .device = 1,
819 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
820 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
821 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
822 .info = spdif_info,
823 .get = spdif_pcm_get,
824 .put = spdif_pcm_put,
825 },
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +0200826};
827
828static const struct snd_kcontrol_new spdif_input_controls[] = {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100829 {
830 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
831 .device = 1,
832 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK),
833 .access = SNDRV_CTL_ELEM_ACCESS_READ,
834 .info = spdif_info,
835 .get = spdif_input_mask_get,
836 },
837 {
838 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
839 .device = 1,
840 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
841 .access = SNDRV_CTL_ELEM_ACCESS_READ,
842 .info = spdif_info,
843 .get = spdif_input_default_get,
844 },
Clemens Ladisch02f21c92008-01-22 08:36:03 +0100845 {
846 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
847 .name = SNDRV_CTL_NAME_IEC958("Loopback ", NONE, SWITCH),
848 .info = snd_ctl_boolean_mono_info,
849 .get = spdif_loopback_get,
850 .put = spdif_loopback_put,
851 },
Clemens Ladisch31c77642008-01-16 08:28:17 +0100852};
853
Clemens Ladischf009ad92008-03-19 08:19:41 +0100854static const struct {
855 unsigned int pcm_dev;
856 struct snd_kcontrol_new controls[2];
857} monitor_controls[] = {
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100858 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100859 .pcm_dev = CAPTURE_0_FROM_I2S_1,
860 .controls = {
861 {
862 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200863 .name = "Analog Input Monitor Playback Switch",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100864 .info = snd_ctl_boolean_mono_info,
865 .get = monitor_get,
866 .put = monitor_put,
867 .private_value = OXYGEN_ADC_MONITOR_A,
868 },
869 {
870 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200871 .name = "Analog Input Monitor Playback Volume",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100872 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
873 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
874 .info = monitor_volume_info,
875 .get = monitor_get,
876 .put = monitor_put,
877 .private_value = OXYGEN_ADC_MONITOR_A_HALF_VOL
878 | (1 << 8),
879 .tlv = { .p = monitor_db_scale, },
880 },
881 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100882 },
883 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100884 .pcm_dev = CAPTURE_0_FROM_I2S_2,
885 .controls = {
886 {
887 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200888 .name = "Analog Input Monitor Playback Switch",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100889 .info = snd_ctl_boolean_mono_info,
890 .get = monitor_get,
891 .put = monitor_put,
892 .private_value = OXYGEN_ADC_MONITOR_B,
893 },
894 {
895 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200896 .name = "Analog Input Monitor Playback Volume",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100897 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
898 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
899 .info = monitor_volume_info,
900 .get = monitor_get,
901 .put = monitor_put,
902 .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
903 | (1 << 8),
904 .tlv = { .p = monitor_db_scale, },
905 },
906 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100907 },
908 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100909 .pcm_dev = CAPTURE_2_FROM_I2S_2,
910 .controls = {
911 {
912 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200913 .name = "Analog Input Monitor Playback Switch",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100914 .index = 1,
915 .info = snd_ctl_boolean_mono_info,
916 .get = monitor_get,
917 .put = monitor_put,
918 .private_value = OXYGEN_ADC_MONITOR_B,
919 },
920 {
921 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200922 .name = "Analog Input Monitor Playback Volume",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100923 .index = 1,
924 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
925 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
926 .info = monitor_volume_info,
927 .get = monitor_get,
928 .put = monitor_put,
929 .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
930 | (1 << 8),
931 .tlv = { .p = monitor_db_scale, },
932 },
933 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100934 },
935 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100936 .pcm_dev = CAPTURE_1_FROM_SPDIF,
937 .controls = {
938 {
939 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200940 .name = "Digital Input Monitor Playback Switch",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100941 .info = snd_ctl_boolean_mono_info,
942 .get = monitor_get,
943 .put = monitor_put,
944 .private_value = OXYGEN_ADC_MONITOR_C,
945 },
946 {
947 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200948 .name = "Digital Input Monitor Playback Volume",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100949 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
950 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
951 .info = monitor_volume_info,
952 .get = monitor_get,
953 .put = monitor_put,
954 .private_value = OXYGEN_ADC_MONITOR_C_HALF_VOL
955 | (1 << 8),
956 .tlv = { .p = monitor_db_scale, },
957 },
958 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100959 },
960};
961
Clemens Ladisch31c77642008-01-16 08:28:17 +0100962static const struct snd_kcontrol_new ac97_controls[] = {
Clemens Ladisch14744d72009-06-25 14:28:49 +0200963 AC97_VOLUME("Mic Capture Volume", 0, AC97_MIC, 0),
Clemens Ladischa3601562008-01-28 08:35:20 +0100964 AC97_SWITCH("Mic Capture Switch", 0, AC97_MIC, 15, 1),
965 AC97_SWITCH("Mic Boost (+20dB)", 0, AC97_MIC, 6, 0),
Clemens Ladische96f38f2010-12-02 11:39:34 +0100966 {
967 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
968 .name = "Mic Source Capture Enum",
969 .info = mic_fmic_source_info,
970 .get = mic_fmic_source_get,
971 .put = mic_fmic_source_put,
972 },
Clemens Ladischa3601562008-01-28 08:35:20 +0100973 AC97_SWITCH("Line Capture Switch", 0, AC97_LINE, 15, 1),
Clemens Ladisch14744d72009-06-25 14:28:49 +0200974 AC97_VOLUME("CD Capture Volume", 0, AC97_CD, 1),
Clemens Ladischa3601562008-01-28 08:35:20 +0100975 AC97_SWITCH("CD Capture Switch", 0, AC97_CD, 15, 1),
Clemens Ladisch14744d72009-06-25 14:28:49 +0200976 AC97_VOLUME("Aux Capture Volume", 0, AC97_AUX, 1),
Clemens Ladischa3601562008-01-28 08:35:20 +0100977 AC97_SWITCH("Aux Capture Switch", 0, AC97_AUX, 15, 1),
978};
979
980static const struct snd_kcontrol_new ac97_fp_controls[] = {
Clemens Ladisch14744d72009-06-25 14:28:49 +0200981 AC97_VOLUME("Front Panel Playback Volume", 1, AC97_HEADPHONE, 1),
Clemens Ladischa3601562008-01-28 08:35:20 +0100982 AC97_SWITCH("Front Panel Playback Switch", 1, AC97_HEADPHONE, 15, 1),
983 {
984 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
985 .name = "Front Panel Capture Volume",
986 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
987 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
988 .info = ac97_fp_rec_volume_info,
989 .get = ac97_fp_rec_volume_get,
990 .put = ac97_fp_rec_volume_put,
991 .tlv = { .p = ac97_rec_db_scale, },
992 },
993 AC97_SWITCH("Front Panel Capture Switch", 1, AC97_REC_GAIN, 15, 1),
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100994};
995
996static void oxygen_any_ctl_free(struct snd_kcontrol *ctl)
997{
998 struct oxygen *chip = ctl->private_data;
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100999 unsigned int i;
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001000
1001 /* I'm too lazy to write a function for each control :-) */
Clemens Ladisch01a3aff2008-01-14 08:56:01 +01001002 for (i = 0; i < ARRAY_SIZE(chip->controls); ++i)
1003 chip->controls[i] = NULL;
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001004}
1005
Clemens Ladisch31c77642008-01-16 08:28:17 +01001006static int add_controls(struct oxygen *chip,
1007 const struct snd_kcontrol_new controls[],
1008 unsigned int count)
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001009{
Clemens Ladisch01a3aff2008-01-14 08:56:01 +01001010 static const char *const known_ctl_names[CONTROL_COUNT] = {
1011 [CONTROL_SPDIF_PCM] =
1012 SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
1013 [CONTROL_SPDIF_INPUT_BITS] =
1014 SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Clemens Ladisch893e44b2008-01-14 08:57:05 +01001015 [CONTROL_MIC_CAPTURE_SWITCH] = "Mic Capture Switch",
1016 [CONTROL_LINE_CAPTURE_SWITCH] = "Line Capture Switch",
1017 [CONTROL_CD_CAPTURE_SWITCH] = "CD Capture Switch",
1018 [CONTROL_AUX_CAPTURE_SWITCH] = "Aux Capture Switch",
Clemens Ladisch01a3aff2008-01-14 08:56:01 +01001019 };
1020 unsigned int i, j;
Clemens Ladischccc80fb2008-01-16 08:32:08 +01001021 struct snd_kcontrol_new template;
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001022 struct snd_kcontrol *ctl;
1023 int err;
1024
Clemens Ladisch31c77642008-01-16 08:28:17 +01001025 for (i = 0; i < count; ++i) {
Clemens Ladischccc80fb2008-01-16 08:32:08 +01001026 template = controls[i];
Clemens Ladisch9bd6a732008-09-22 08:55:19 +02001027 if (chip->model.control_filter) {
1028 err = chip->model.control_filter(&template);
Clemens Ladisch9f9115d2008-06-16 14:13:52 +02001029 if (err < 0)
1030 return err;
1031 if (err == 1)
1032 continue;
1033 }
Clemens Ladisch75919d72009-09-28 11:15:49 +02001034 if (!strcmp(template.name, "Stereo Upmixing") &&
Clemens Ladisch1f4d7be2011-01-10 15:59:38 +01001035 chip->model.dac_channels_pcm == 2)
Clemens Ladisch75919d72009-09-28 11:15:49 +02001036 continue;
Clemens Ladische96f38f2010-12-02 11:39:34 +01001037 if (!strcmp(template.name, "Mic Source Capture Enum") &&
1038 !(chip->model.device_config & AC97_FMIC_SWITCH))
1039 continue;
Clemens Ladischb6ca8ab2010-10-04 13:21:52 +02001040 if (!strncmp(template.name, "CD Capture ", 11) &&
1041 !(chip->model.device_config & AC97_CD_INPUT))
1042 continue;
Clemens Ladisch4972a172008-04-16 09:15:45 +02001043 if (!strcmp(template.name, "Master Playback Volume") &&
Clemens Ladisch9bd6a732008-09-22 08:55:19 +02001044 chip->model.dac_tlv) {
1045 template.tlv.p = chip->model.dac_tlv;
Clemens Ladisch4972a172008-04-16 09:15:45 +02001046 template.access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1047 }
Clemens Ladische9d88a82008-01-21 08:52:11 +01001048 ctl = snd_ctl_new1(&template, chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001049 if (!ctl)
1050 return -ENOMEM;
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001051 err = snd_ctl_add(chip->card, ctl);
1052 if (err < 0)
1053 return err;
Clemens Ladisch01a3aff2008-01-14 08:56:01 +01001054 for (j = 0; j < CONTROL_COUNT; ++j)
1055 if (!strcmp(ctl->id.name, known_ctl_names[j])) {
1056 chip->controls[j] = ctl;
1057 ctl->private_free = oxygen_any_ctl_free;
1058 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001059 }
Clemens Ladisch31c77642008-01-16 08:28:17 +01001060 return 0;
1061}
1062
1063int oxygen_mixer_init(struct oxygen *chip)
1064{
Clemens Ladischf009ad92008-03-19 08:19:41 +01001065 unsigned int i;
Clemens Ladisch31c77642008-01-16 08:28:17 +01001066 int err;
1067
1068 err = add_controls(chip, controls, ARRAY_SIZE(controls));
1069 if (err < 0)
1070 return err;
Clemens Ladischd76596b2008-09-22 09:02:08 +02001071 if (chip->model.device_config & CAPTURE_1_FROM_SPDIF) {
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +02001072 err = add_controls(chip, spdif_input_controls,
1073 ARRAY_SIZE(spdif_input_controls));
1074 if (err < 0)
1075 return err;
1076 }
Clemens Ladischf009ad92008-03-19 08:19:41 +01001077 for (i = 0; i < ARRAY_SIZE(monitor_controls); ++i) {
Clemens Ladischd76596b2008-09-22 09:02:08 +02001078 if (!(chip->model.device_config & monitor_controls[i].pcm_dev))
Clemens Ladischf009ad92008-03-19 08:19:41 +01001079 continue;
1080 err = add_controls(chip, monitor_controls[i].controls,
1081 ARRAY_SIZE(monitor_controls[i].controls));
Clemens Ladischfa5d8102008-03-19 08:17:33 +01001082 if (err < 0)
1083 return err;
1084 }
Clemens Ladisch31c77642008-01-16 08:28:17 +01001085 if (chip->has_ac97_0) {
1086 err = add_controls(chip, ac97_controls,
1087 ARRAY_SIZE(ac97_controls));
1088 if (err < 0)
1089 return err;
1090 }
Clemens Ladischa3601562008-01-28 08:35:20 +01001091 if (chip->has_ac97_1) {
1092 err = add_controls(chip, ac97_fp_controls,
1093 ARRAY_SIZE(ac97_fp_controls));
1094 if (err < 0)
1095 return err;
1096 }
Clemens Ladisch9bd6a732008-09-22 08:55:19 +02001097 return chip->model.mixer_init ? chip->model.mixer_init(chip) : 0;
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001098}