blob: 821df1c9d1d0c9030df947177128960aafb81c70 [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 Ladisch96007322011-01-10 16:25:44 +0100122 return snd_ctl_enum_info(info, 1, count, names);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100123}
124
125static int upmix_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
126{
127 struct oxygen *chip = ctl->private_data;
128
129 mutex_lock(&chip->mutex);
130 value->value.enumerated.item[0] = chip->dac_routing;
131 mutex_unlock(&chip->mutex);
132 return 0;
133}
134
135void oxygen_update_dac_routing(struct oxygen *chip)
136{
Clemens Ladischc9946b22008-01-21 08:44:24 +0100137 /* DAC 0: front, DAC 1: surround, DAC 2: center/LFE, DAC 3: back */
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200138 static const unsigned int reg_values[5] = {
Clemens Ladischc9946b22008-01-21 08:44:24 +0100139 /* stereo -> front */
140 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
141 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
142 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
143 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
144 /* stereo -> front+surround */
145 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
146 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
147 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
148 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
149 /* stereo -> front+surround+back */
150 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
151 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
152 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
153 (0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200154 /* stereo -> front+surround+center/LFE */
155 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
156 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
157 (0 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
158 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
159 /* stereo -> front+surround+center/LFE+back */
160 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
161 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
162 (0 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
163 (0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100164 };
Clemens Ladisch7113e952008-01-14 08:55:03 +0100165 u8 channels;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100166 unsigned int reg_value;
167
Clemens Ladisch7113e952008-01-14 08:55:03 +0100168 channels = oxygen_read8(chip, OXYGEN_PLAY_CHANNELS) &
169 OXYGEN_PLAY_CHANNELS_MASK;
170 if (channels == OXYGEN_PLAY_CHANNELS_2)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100171 reg_value = reg_values[chip->dac_routing];
Clemens Ladisch7113e952008-01-14 08:55:03 +0100172 else if (channels == OXYGEN_PLAY_CHANNELS_8)
Clemens Ladischc9946b22008-01-21 08:44:24 +0100173 /* in 7.1 mode, "rear" channels go to the "back" jack */
174 reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
175 (3 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
176 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
177 (1 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100178 else
Clemens Ladischc9946b22008-01-21 08:44:24 +0100179 reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
180 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
181 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
182 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT);
183 oxygen_write16_masked(chip, OXYGEN_PLAY_ROUTING, reg_value,
184 OXYGEN_PLAY_DAC0_SOURCE_MASK |
185 OXYGEN_PLAY_DAC1_SOURCE_MASK |
186 OXYGEN_PLAY_DAC2_SOURCE_MASK |
187 OXYGEN_PLAY_DAC3_SOURCE_MASK);
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200188 if (chip->model.update_center_lfe_mix)
189 chip->model.update_center_lfe_mix(chip, chip->dac_routing > 2);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100190}
191
192static int upmix_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
193{
194 struct oxygen *chip = ctl->private_data;
Clemens Ladisch66410bf2011-01-10 16:20:29 +0100195 unsigned int count = upmix_item_count(chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100196 int changed;
197
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200198 if (value->value.enumerated.item[0] >= count)
199 return -EINVAL;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100200 mutex_lock(&chip->mutex);
201 changed = value->value.enumerated.item[0] != chip->dac_routing;
202 if (changed) {
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200203 chip->dac_routing = value->value.enumerated.item[0];
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100204 oxygen_update_dac_routing(chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100205 }
206 mutex_unlock(&chip->mutex);
207 return changed;
208}
209
210static int spdif_switch_get(struct snd_kcontrol *ctl,
211 struct snd_ctl_elem_value *value)
212{
213 struct oxygen *chip = ctl->private_data;
214
215 mutex_lock(&chip->mutex);
216 value->value.integer.value[0] = chip->spdif_playback_enable;
217 mutex_unlock(&chip->mutex);
218 return 0;
219}
220
221static unsigned int oxygen_spdif_rate(unsigned int oxygen_rate)
222{
223 switch (oxygen_rate) {
224 case OXYGEN_RATE_32000:
225 return IEC958_AES3_CON_FS_32000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
226 case OXYGEN_RATE_44100:
227 return IEC958_AES3_CON_FS_44100 << OXYGEN_SPDIF_CS_RATE_SHIFT;
228 default: /* OXYGEN_RATE_48000 */
229 return IEC958_AES3_CON_FS_48000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
230 case OXYGEN_RATE_64000:
231 return 0xb << OXYGEN_SPDIF_CS_RATE_SHIFT;
232 case OXYGEN_RATE_88200:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200233 return IEC958_AES3_CON_FS_88200 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100234 case OXYGEN_RATE_96000:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200235 return IEC958_AES3_CON_FS_96000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100236 case OXYGEN_RATE_176400:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200237 return IEC958_AES3_CON_FS_176400 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100238 case OXYGEN_RATE_192000:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200239 return IEC958_AES3_CON_FS_192000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100240 }
241}
242
243void oxygen_update_spdif_source(struct oxygen *chip)
244{
245 u32 old_control, new_control;
246 u16 old_routing, new_routing;
247 unsigned int oxygen_rate;
248
249 old_control = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
250 old_routing = oxygen_read16(chip, OXYGEN_PLAY_ROUTING);
251 if (chip->pcm_active & (1 << PCM_SPDIF)) {
252 new_control = old_control | OXYGEN_SPDIF_OUT_ENABLE;
Clemens Ladischc9946b22008-01-21 08:44:24 +0100253 new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
254 | OXYGEN_PLAY_SPDIF_SPDIF;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100255 oxygen_rate = (old_control >> OXYGEN_SPDIF_OUT_RATE_SHIFT)
256 & OXYGEN_I2S_RATE_MASK;
257 /* S/PDIF rate was already set by the caller */
258 } else if ((chip->pcm_active & (1 << PCM_MULTICH)) &&
259 chip->spdif_playback_enable) {
Clemens Ladischc9946b22008-01-21 08:44:24 +0100260 new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
261 | OXYGEN_PLAY_SPDIF_MULTICH_01;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100262 oxygen_rate = oxygen_read16(chip, OXYGEN_I2S_MULTICH_FORMAT)
263 & OXYGEN_I2S_RATE_MASK;
264 new_control = (old_control & ~OXYGEN_SPDIF_OUT_RATE_MASK) |
265 (oxygen_rate << OXYGEN_SPDIF_OUT_RATE_SHIFT) |
266 OXYGEN_SPDIF_OUT_ENABLE;
267 } else {
268 new_control = old_control & ~OXYGEN_SPDIF_OUT_ENABLE;
269 new_routing = old_routing;
270 oxygen_rate = OXYGEN_RATE_44100;
271 }
272 if (old_routing != new_routing) {
273 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL,
274 new_control & ~OXYGEN_SPDIF_OUT_ENABLE);
275 oxygen_write16(chip, OXYGEN_PLAY_ROUTING, new_routing);
276 }
277 if (new_control & OXYGEN_SPDIF_OUT_ENABLE)
278 oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS,
279 oxygen_spdif_rate(oxygen_rate) |
280 ((chip->pcm_active & (1 << PCM_SPDIF)) ?
281 chip->spdif_pcm_bits : chip->spdif_bits));
282 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, new_control);
283}
284
285static int spdif_switch_put(struct snd_kcontrol *ctl,
286 struct snd_ctl_elem_value *value)
287{
288 struct oxygen *chip = ctl->private_data;
289 int changed;
290
291 mutex_lock(&chip->mutex);
292 changed = value->value.integer.value[0] != chip->spdif_playback_enable;
293 if (changed) {
294 chip->spdif_playback_enable = !!value->value.integer.value[0];
295 spin_lock_irq(&chip->reg_lock);
296 oxygen_update_spdif_source(chip);
297 spin_unlock_irq(&chip->reg_lock);
298 }
299 mutex_unlock(&chip->mutex);
300 return changed;
301}
302
303static int spdif_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
304{
305 info->type = SNDRV_CTL_ELEM_TYPE_IEC958;
306 info->count = 1;
307 return 0;
308}
309
310static void oxygen_to_iec958(u32 bits, struct snd_ctl_elem_value *value)
311{
312 value->value.iec958.status[0] =
313 bits & (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C |
314 OXYGEN_SPDIF_PREEMPHASIS);
315 value->value.iec958.status[1] = /* category and original */
316 bits >> OXYGEN_SPDIF_CATEGORY_SHIFT;
317}
318
319static u32 iec958_to_oxygen(struct snd_ctl_elem_value *value)
320{
321 u32 bits;
322
323 bits = value->value.iec958.status[0] &
324 (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C |
325 OXYGEN_SPDIF_PREEMPHASIS);
326 bits |= value->value.iec958.status[1] << OXYGEN_SPDIF_CATEGORY_SHIFT;
327 if (bits & OXYGEN_SPDIF_NONAUDIO)
328 bits |= OXYGEN_SPDIF_V;
329 return bits;
330}
331
332static inline void write_spdif_bits(struct oxygen *chip, u32 bits)
333{
334 oxygen_write32_masked(chip, OXYGEN_SPDIF_OUTPUT_BITS, bits,
335 OXYGEN_SPDIF_NONAUDIO |
336 OXYGEN_SPDIF_C |
337 OXYGEN_SPDIF_PREEMPHASIS |
338 OXYGEN_SPDIF_CATEGORY_MASK |
339 OXYGEN_SPDIF_ORIGINAL |
340 OXYGEN_SPDIF_V);
341}
342
343static int spdif_default_get(struct snd_kcontrol *ctl,
344 struct snd_ctl_elem_value *value)
345{
346 struct oxygen *chip = ctl->private_data;
347
348 mutex_lock(&chip->mutex);
349 oxygen_to_iec958(chip->spdif_bits, value);
350 mutex_unlock(&chip->mutex);
351 return 0;
352}
353
354static int spdif_default_put(struct snd_kcontrol *ctl,
355 struct snd_ctl_elem_value *value)
356{
357 struct oxygen *chip = ctl->private_data;
358 u32 new_bits;
359 int changed;
360
361 new_bits = iec958_to_oxygen(value);
362 mutex_lock(&chip->mutex);
363 changed = new_bits != chip->spdif_bits;
364 if (changed) {
365 chip->spdif_bits = new_bits;
366 if (!(chip->pcm_active & (1 << PCM_SPDIF)))
367 write_spdif_bits(chip, new_bits);
368 }
369 mutex_unlock(&chip->mutex);
370 return changed;
371}
372
373static int spdif_mask_get(struct snd_kcontrol *ctl,
374 struct snd_ctl_elem_value *value)
375{
376 value->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
377 IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS;
378 value->value.iec958.status[1] =
379 IEC958_AES1_CON_CATEGORY | IEC958_AES1_CON_ORIGINAL;
380 return 0;
381}
382
383static int spdif_pcm_get(struct snd_kcontrol *ctl,
384 struct snd_ctl_elem_value *value)
385{
386 struct oxygen *chip = ctl->private_data;
387
388 mutex_lock(&chip->mutex);
389 oxygen_to_iec958(chip->spdif_pcm_bits, value);
390 mutex_unlock(&chip->mutex);
391 return 0;
392}
393
394static int spdif_pcm_put(struct snd_kcontrol *ctl,
395 struct snd_ctl_elem_value *value)
396{
397 struct oxygen *chip = ctl->private_data;
398 u32 new_bits;
399 int changed;
400
401 new_bits = iec958_to_oxygen(value);
402 mutex_lock(&chip->mutex);
403 changed = new_bits != chip->spdif_pcm_bits;
404 if (changed) {
405 chip->spdif_pcm_bits = new_bits;
406 if (chip->pcm_active & (1 << PCM_SPDIF))
407 write_spdif_bits(chip, new_bits);
408 }
409 mutex_unlock(&chip->mutex);
410 return changed;
411}
412
413static int spdif_input_mask_get(struct snd_kcontrol *ctl,
414 struct snd_ctl_elem_value *value)
415{
416 value->value.iec958.status[0] = 0xff;
417 value->value.iec958.status[1] = 0xff;
418 value->value.iec958.status[2] = 0xff;
419 value->value.iec958.status[3] = 0xff;
420 return 0;
421}
422
423static int spdif_input_default_get(struct snd_kcontrol *ctl,
424 struct snd_ctl_elem_value *value)
425{
426 struct oxygen *chip = ctl->private_data;
427 u32 bits;
428
429 bits = oxygen_read32(chip, OXYGEN_SPDIF_INPUT_BITS);
430 value->value.iec958.status[0] = bits;
431 value->value.iec958.status[1] = bits >> 8;
432 value->value.iec958.status[2] = bits >> 16;
433 value->value.iec958.status[3] = bits >> 24;
434 return 0;
435}
436
Clemens Ladisch02f21c92008-01-22 08:36:03 +0100437static int spdif_loopback_get(struct snd_kcontrol *ctl,
438 struct snd_ctl_elem_value *value)
439{
440 struct oxygen *chip = ctl->private_data;
441
442 value->value.integer.value[0] =
443 !!(oxygen_read32(chip, OXYGEN_SPDIF_CONTROL)
444 & OXYGEN_SPDIF_LOOPBACK);
445 return 0;
446}
447
448static int spdif_loopback_put(struct snd_kcontrol *ctl,
449 struct snd_ctl_elem_value *value)
450{
451 struct oxygen *chip = ctl->private_data;
452 u32 oldreg, newreg;
453 int changed;
454
455 spin_lock_irq(&chip->reg_lock);
456 oldreg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
457 if (value->value.integer.value[0])
458 newreg = oldreg | OXYGEN_SPDIF_LOOPBACK;
459 else
460 newreg = oldreg & ~OXYGEN_SPDIF_LOOPBACK;
461 changed = newreg != oldreg;
462 if (changed)
463 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, newreg);
464 spin_unlock_irq(&chip->reg_lock);
465 return changed;
466}
467
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100468static int monitor_volume_info(struct snd_kcontrol *ctl,
469 struct snd_ctl_elem_info *info)
470{
471 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
472 info->count = 1;
473 info->value.integer.min = 0;
474 info->value.integer.max = 1;
475 return 0;
476}
477
478static int monitor_get(struct snd_kcontrol *ctl,
479 struct snd_ctl_elem_value *value)
480{
481 struct oxygen *chip = ctl->private_data;
482 u8 bit = ctl->private_value;
483 int invert = ctl->private_value & (1 << 8);
484
485 value->value.integer.value[0] =
486 !!invert ^ !!(oxygen_read8(chip, OXYGEN_ADC_MONITOR) & bit);
487 return 0;
488}
489
490static int monitor_put(struct snd_kcontrol *ctl,
491 struct snd_ctl_elem_value *value)
492{
493 struct oxygen *chip = ctl->private_data;
494 u8 bit = ctl->private_value;
495 int invert = ctl->private_value & (1 << 8);
496 u8 oldreg, newreg;
497 int changed;
498
499 spin_lock_irq(&chip->reg_lock);
500 oldreg = oxygen_read8(chip, OXYGEN_ADC_MONITOR);
501 if ((!!value->value.integer.value[0] ^ !!invert) != 0)
502 newreg = oldreg | bit;
503 else
504 newreg = oldreg & ~bit;
505 changed = newreg != oldreg;
506 if (changed)
507 oxygen_write8(chip, OXYGEN_ADC_MONITOR, newreg);
508 spin_unlock_irq(&chip->reg_lock);
509 return changed;
510}
511
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100512static int ac97_switch_get(struct snd_kcontrol *ctl,
513 struct snd_ctl_elem_value *value)
514{
515 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100516 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100517 unsigned int index = ctl->private_value & 0xff;
518 unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
519 int invert = ctl->private_value & (1 << 16);
520 u16 reg;
521
522 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100523 reg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100524 mutex_unlock(&chip->mutex);
525 if (!(reg & (1 << bitnr)) ^ !invert)
526 value->value.integer.value[0] = 1;
527 else
528 value->value.integer.value[0] = 0;
529 return 0;
530}
531
Clemens Ladische97f7992008-04-01 10:02:18 +0200532static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
533{
Clemens Ladisch3d839e52008-08-26 11:06:26 +0200534 unsigned int priv_idx;
Clemens Ladische97f7992008-04-01 10:02:18 +0200535 u16 value;
536
Clemens Ladisch3d839e52008-08-26 11:06:26 +0200537 if (!chip->controls[control])
538 return;
539 priv_idx = chip->controls[control]->private_value & 0xff;
Clemens Ladische97f7992008-04-01 10:02:18 +0200540 value = oxygen_read_ac97(chip, 0, priv_idx);
541 if (!(value & 0x8000)) {
542 oxygen_write_ac97(chip, 0, priv_idx, value | 0x8000);
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200543 if (chip->model.ac97_switch)
544 chip->model.ac97_switch(chip, priv_idx, 0x8000);
Clemens Ladische97f7992008-04-01 10:02:18 +0200545 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
546 &chip->controls[control]->id);
547 }
548}
549
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100550static int ac97_switch_put(struct snd_kcontrol *ctl,
551 struct snd_ctl_elem_value *value)
552{
553 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100554 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100555 unsigned int index = ctl->private_value & 0xff;
556 unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
557 int invert = ctl->private_value & (1 << 16);
558 u16 oldreg, newreg;
559 int change;
560
561 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100562 oldreg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100563 newreg = oldreg;
564 if (!value->value.integer.value[0] ^ !invert)
565 newreg |= 1 << bitnr;
566 else
567 newreg &= ~(1 << bitnr);
568 change = newreg != oldreg;
569 if (change) {
Clemens Ladischa3601562008-01-28 08:35:20 +0100570 oxygen_write_ac97(chip, codec, index, newreg);
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200571 if (codec == 0 && chip->model.ac97_switch)
572 chip->model.ac97_switch(chip, index, newreg & 0x8000);
Clemens Ladische97f7992008-04-01 10:02:18 +0200573 if (index == AC97_LINE) {
574 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
575 newreg & 0x8000 ?
576 CM9780_GPO0 : 0, CM9780_GPO0);
577 if (!(newreg & 0x8000)) {
578 mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
579 mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
580 mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
581 }
582 } else if ((index == AC97_MIC || index == AC97_CD ||
583 index == AC97_VIDEO || index == AC97_AUX) &&
584 bitnr == 15 && !(newreg & 0x8000)) {
585 mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
586 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
587 CM9780_GPO0, CM9780_GPO0);
588 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100589 }
590 mutex_unlock(&chip->mutex);
591 return change;
592}
593
594static int ac97_volume_info(struct snd_kcontrol *ctl,
595 struct snd_ctl_elem_info *info)
596{
Clemens Ladisch14744d72009-06-25 14:28:49 +0200597 int stereo = (ctl->private_value >> 16) & 1;
598
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100599 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Clemens Ladisch14744d72009-06-25 14:28:49 +0200600 info->count = stereo ? 2 : 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100601 info->value.integer.min = 0;
602 info->value.integer.max = 0x1f;
603 return 0;
604}
605
606static int ac97_volume_get(struct snd_kcontrol *ctl,
607 struct snd_ctl_elem_value *value)
608{
609 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100610 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladisch14744d72009-06-25 14:28:49 +0200611 int stereo = (ctl->private_value >> 16) & 1;
Clemens Ladischa3601562008-01-28 08:35:20 +0100612 unsigned int index = ctl->private_value & 0xff;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100613 u16 reg;
614
615 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100616 reg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100617 mutex_unlock(&chip->mutex);
618 value->value.integer.value[0] = 31 - (reg & 0x1f);
Clemens Ladisch14744d72009-06-25 14:28:49 +0200619 if (stereo)
620 value->value.integer.value[1] = 31 - ((reg >> 8) & 0x1f);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100621 return 0;
622}
623
624static int ac97_volume_put(struct snd_kcontrol *ctl,
625 struct snd_ctl_elem_value *value)
626{
627 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100628 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladisch14744d72009-06-25 14:28:49 +0200629 int stereo = (ctl->private_value >> 16) & 1;
Clemens Ladischa3601562008-01-28 08:35:20 +0100630 unsigned int index = ctl->private_value & 0xff;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100631 u16 oldreg, newreg;
632 int change;
633
634 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100635 oldreg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100636 newreg = oldreg;
637 newreg = (newreg & ~0x1f) |
638 (31 - (value->value.integer.value[0] & 0x1f));
Clemens Ladisch14744d72009-06-25 14:28:49 +0200639 if (stereo)
640 newreg = (newreg & ~0x1f00) |
641 ((31 - (value->value.integer.value[1] & 0x1f)) << 8);
642 else
643 newreg = (newreg & ~0x1f00) | ((newreg & 0x1f) << 8);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100644 change = newreg != oldreg;
645 if (change)
Clemens Ladischa3601562008-01-28 08:35:20 +0100646 oxygen_write_ac97(chip, codec, index, newreg);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100647 mutex_unlock(&chip->mutex);
648 return change;
649}
650
Clemens Ladische96f38f2010-12-02 11:39:34 +0100651static int mic_fmic_source_info(struct snd_kcontrol *ctl,
652 struct snd_ctl_elem_info *info)
653{
654 static const char *const names[] = { "Mic Jack", "Front Panel" };
655
Clemens Ladisch96007322011-01-10 16:25:44 +0100656 return snd_ctl_enum_info(info, 1, 2, names);
Clemens Ladische96f38f2010-12-02 11:39:34 +0100657}
658
659static int mic_fmic_source_get(struct snd_kcontrol *ctl,
660 struct snd_ctl_elem_value *value)
661{
662 struct oxygen *chip = ctl->private_data;
663
664 mutex_lock(&chip->mutex);
665 value->value.enumerated.item[0] =
666 !!(oxygen_read_ac97(chip, 0, CM9780_JACK) & CM9780_FMIC2MIC);
667 mutex_unlock(&chip->mutex);
668 return 0;
669}
670
671static int mic_fmic_source_put(struct snd_kcontrol *ctl,
672 struct snd_ctl_elem_value *value)
673{
674 struct oxygen *chip = ctl->private_data;
675 u16 oldreg, newreg;
676 int change;
677
678 mutex_lock(&chip->mutex);
679 oldreg = oxygen_read_ac97(chip, 0, CM9780_JACK);
680 if (value->value.enumerated.item[0])
681 newreg = oldreg | CM9780_FMIC2MIC;
682 else
683 newreg = oldreg & ~CM9780_FMIC2MIC;
684 change = newreg != oldreg;
685 if (change)
686 oxygen_write_ac97(chip, 0, CM9780_JACK, newreg);
687 mutex_unlock(&chip->mutex);
688 return change;
689}
690
Clemens Ladischa3601562008-01-28 08:35:20 +0100691static int ac97_fp_rec_volume_info(struct snd_kcontrol *ctl,
692 struct snd_ctl_elem_info *info)
693{
694 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
695 info->count = 2;
696 info->value.integer.min = 0;
697 info->value.integer.max = 7;
698 return 0;
699}
700
701static int ac97_fp_rec_volume_get(struct snd_kcontrol *ctl,
702 struct snd_ctl_elem_value *value)
703{
704 struct oxygen *chip = ctl->private_data;
705 u16 reg;
706
707 mutex_lock(&chip->mutex);
708 reg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
709 mutex_unlock(&chip->mutex);
710 value->value.integer.value[0] = reg & 7;
711 value->value.integer.value[1] = (reg >> 8) & 7;
712 return 0;
713}
714
715static int ac97_fp_rec_volume_put(struct snd_kcontrol *ctl,
716 struct snd_ctl_elem_value *value)
717{
718 struct oxygen *chip = ctl->private_data;
719 u16 oldreg, newreg;
720 int change;
721
722 mutex_lock(&chip->mutex);
723 oldreg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
724 newreg = oldreg & ~0x0707;
725 newreg = newreg | (value->value.integer.value[0] & 7);
726 newreg = newreg | ((value->value.integer.value[0] & 7) << 8);
727 change = newreg != oldreg;
728 if (change)
729 oxygen_write_ac97(chip, 1, AC97_REC_GAIN, newreg);
730 mutex_unlock(&chip->mutex);
731 return change;
732}
733
734#define AC97_SWITCH(xname, codec, index, bitnr, invert) { \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100735 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
736 .name = xname, \
737 .info = snd_ctl_boolean_mono_info, \
738 .get = ac97_switch_get, \
739 .put = ac97_switch_put, \
Clemens Ladischa3601562008-01-28 08:35:20 +0100740 .private_value = ((codec) << 24) | ((invert) << 16) | \
741 ((bitnr) << 8) | (index), \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100742 }
Clemens Ladisch14744d72009-06-25 14:28:49 +0200743#define AC97_VOLUME(xname, codec, index, stereo) { \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100744 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
745 .name = xname, \
Clemens Ladischa3601562008-01-28 08:35:20 +0100746 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
747 SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100748 .info = ac97_volume_info, \
749 .get = ac97_volume_get, \
750 .put = ac97_volume_put, \
751 .tlv = { .p = ac97_db_scale, }, \
Clemens Ladisch14744d72009-06-25 14:28:49 +0200752 .private_value = ((codec) << 24) | ((stereo) << 16) | (index), \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100753 }
754
Clemens Ladisch9a0b3792010-10-04 13:25:13 +0200755static DECLARE_TLV_DB_SCALE(monitor_db_scale, -600, 600, 0);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100756static DECLARE_TLV_DB_SCALE(ac97_db_scale, -3450, 150, 0);
Clemens Ladischa3601562008-01-28 08:35:20 +0100757static DECLARE_TLV_DB_SCALE(ac97_rec_db_scale, 0, 150, 0);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100758
759static const struct snd_kcontrol_new controls[] = {
760 {
761 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladischfb920b72008-01-15 08:39:06 +0100762 .name = "Master Playback Volume",
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100763 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100764 .info = dac_volume_info,
765 .get = dac_volume_get,
766 .put = dac_volume_put,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100767 },
768 {
769 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladischfb920b72008-01-15 08:39:06 +0100770 .name = "Master Playback Switch",
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100771 .info = snd_ctl_boolean_mono_info,
772 .get = dac_mute_get,
773 .put = dac_mute_put,
774 },
775 {
776 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
777 .name = "Stereo Upmixing",
778 .info = upmix_info,
779 .get = upmix_get,
780 .put = upmix_put,
781 },
782 {
783 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
784 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
785 .info = snd_ctl_boolean_mono_info,
786 .get = spdif_switch_get,
787 .put = spdif_switch_put,
788 },
789 {
790 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
791 .device = 1,
792 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
793 .info = spdif_info,
794 .get = spdif_default_get,
795 .put = spdif_default_put,
796 },
797 {
798 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
799 .device = 1,
800 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
801 .access = SNDRV_CTL_ELEM_ACCESS_READ,
802 .info = spdif_info,
803 .get = spdif_mask_get,
804 },
805 {
806 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
807 .device = 1,
808 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
809 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
810 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
811 .info = spdif_info,
812 .get = spdif_pcm_get,
813 .put = spdif_pcm_put,
814 },
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +0200815};
816
817static const struct snd_kcontrol_new spdif_input_controls[] = {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100818 {
819 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
820 .device = 1,
821 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK),
822 .access = SNDRV_CTL_ELEM_ACCESS_READ,
823 .info = spdif_info,
824 .get = spdif_input_mask_get,
825 },
826 {
827 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
828 .device = 1,
829 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
830 .access = SNDRV_CTL_ELEM_ACCESS_READ,
831 .info = spdif_info,
832 .get = spdif_input_default_get,
833 },
Clemens Ladisch02f21c92008-01-22 08:36:03 +0100834 {
835 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
836 .name = SNDRV_CTL_NAME_IEC958("Loopback ", NONE, SWITCH),
837 .info = snd_ctl_boolean_mono_info,
838 .get = spdif_loopback_get,
839 .put = spdif_loopback_put,
840 },
Clemens Ladisch31c77642008-01-16 08:28:17 +0100841};
842
Clemens Ladischf009ad92008-03-19 08:19:41 +0100843static const struct {
844 unsigned int pcm_dev;
845 struct snd_kcontrol_new controls[2];
846} monitor_controls[] = {
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100847 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100848 .pcm_dev = CAPTURE_0_FROM_I2S_1,
849 .controls = {
850 {
851 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200852 .name = "Analog Input Monitor Playback Switch",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100853 .info = snd_ctl_boolean_mono_info,
854 .get = monitor_get,
855 .put = monitor_put,
856 .private_value = OXYGEN_ADC_MONITOR_A,
857 },
858 {
859 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200860 .name = "Analog Input Monitor Playback Volume",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100861 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
862 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
863 .info = monitor_volume_info,
864 .get = monitor_get,
865 .put = monitor_put,
866 .private_value = OXYGEN_ADC_MONITOR_A_HALF_VOL
867 | (1 << 8),
868 .tlv = { .p = monitor_db_scale, },
869 },
870 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100871 },
872 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100873 .pcm_dev = CAPTURE_0_FROM_I2S_2,
874 .controls = {
875 {
876 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200877 .name = "Analog Input Monitor Playback Switch",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100878 .info = snd_ctl_boolean_mono_info,
879 .get = monitor_get,
880 .put = monitor_put,
881 .private_value = OXYGEN_ADC_MONITOR_B,
882 },
883 {
884 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200885 .name = "Analog Input Monitor Playback Volume",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100886 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
887 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
888 .info = monitor_volume_info,
889 .get = monitor_get,
890 .put = monitor_put,
891 .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
892 | (1 << 8),
893 .tlv = { .p = monitor_db_scale, },
894 },
895 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100896 },
897 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100898 .pcm_dev = CAPTURE_2_FROM_I2S_2,
899 .controls = {
900 {
901 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200902 .name = "Analog Input Monitor Playback Switch",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100903 .index = 1,
904 .info = snd_ctl_boolean_mono_info,
905 .get = monitor_get,
906 .put = monitor_put,
907 .private_value = OXYGEN_ADC_MONITOR_B,
908 },
909 {
910 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200911 .name = "Analog Input Monitor Playback Volume",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100912 .index = 1,
913 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
914 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
915 .info = monitor_volume_info,
916 .get = monitor_get,
917 .put = monitor_put,
918 .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
919 | (1 << 8),
920 .tlv = { .p = monitor_db_scale, },
921 },
922 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100923 },
924 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100925 .pcm_dev = CAPTURE_1_FROM_SPDIF,
926 .controls = {
927 {
928 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200929 .name = "Digital Input Monitor Playback Switch",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100930 .info = snd_ctl_boolean_mono_info,
931 .get = monitor_get,
932 .put = monitor_put,
933 .private_value = OXYGEN_ADC_MONITOR_C,
934 },
935 {
936 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladisch62428f72009-09-28 11:22:18 +0200937 .name = "Digital Input Monitor Playback Volume",
Clemens Ladischf009ad92008-03-19 08:19:41 +0100938 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
939 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
940 .info = monitor_volume_info,
941 .get = monitor_get,
942 .put = monitor_put,
943 .private_value = OXYGEN_ADC_MONITOR_C_HALF_VOL
944 | (1 << 8),
945 .tlv = { .p = monitor_db_scale, },
946 },
947 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100948 },
949};
950
Clemens Ladisch31c77642008-01-16 08:28:17 +0100951static const struct snd_kcontrol_new ac97_controls[] = {
Clemens Ladisch14744d72009-06-25 14:28:49 +0200952 AC97_VOLUME("Mic Capture Volume", 0, AC97_MIC, 0),
Clemens Ladischa3601562008-01-28 08:35:20 +0100953 AC97_SWITCH("Mic Capture Switch", 0, AC97_MIC, 15, 1),
954 AC97_SWITCH("Mic Boost (+20dB)", 0, AC97_MIC, 6, 0),
Clemens Ladische96f38f2010-12-02 11:39:34 +0100955 {
956 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
957 .name = "Mic Source Capture Enum",
958 .info = mic_fmic_source_info,
959 .get = mic_fmic_source_get,
960 .put = mic_fmic_source_put,
961 },
Clemens Ladischa3601562008-01-28 08:35:20 +0100962 AC97_SWITCH("Line Capture Switch", 0, AC97_LINE, 15, 1),
Clemens Ladisch14744d72009-06-25 14:28:49 +0200963 AC97_VOLUME("CD Capture Volume", 0, AC97_CD, 1),
Clemens Ladischa3601562008-01-28 08:35:20 +0100964 AC97_SWITCH("CD Capture Switch", 0, AC97_CD, 15, 1),
Clemens Ladisch14744d72009-06-25 14:28:49 +0200965 AC97_VOLUME("Aux Capture Volume", 0, AC97_AUX, 1),
Clemens Ladischa3601562008-01-28 08:35:20 +0100966 AC97_SWITCH("Aux Capture Switch", 0, AC97_AUX, 15, 1),
967};
968
969static const struct snd_kcontrol_new ac97_fp_controls[] = {
Clemens Ladisch14744d72009-06-25 14:28:49 +0200970 AC97_VOLUME("Front Panel Playback Volume", 1, AC97_HEADPHONE, 1),
Clemens Ladischa3601562008-01-28 08:35:20 +0100971 AC97_SWITCH("Front Panel Playback Switch", 1, AC97_HEADPHONE, 15, 1),
972 {
973 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
974 .name = "Front Panel Capture Volume",
975 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
976 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
977 .info = ac97_fp_rec_volume_info,
978 .get = ac97_fp_rec_volume_get,
979 .put = ac97_fp_rec_volume_put,
980 .tlv = { .p = ac97_rec_db_scale, },
981 },
982 AC97_SWITCH("Front Panel Capture Switch", 1, AC97_REC_GAIN, 15, 1),
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100983};
984
985static void oxygen_any_ctl_free(struct snd_kcontrol *ctl)
986{
987 struct oxygen *chip = ctl->private_data;
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100988 unsigned int i;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100989
990 /* I'm too lazy to write a function for each control :-) */
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100991 for (i = 0; i < ARRAY_SIZE(chip->controls); ++i)
992 chip->controls[i] = NULL;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100993}
994
Clemens Ladisch31c77642008-01-16 08:28:17 +0100995static int add_controls(struct oxygen *chip,
996 const struct snd_kcontrol_new controls[],
997 unsigned int count)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100998{
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100999 static const char *const known_ctl_names[CONTROL_COUNT] = {
1000 [CONTROL_SPDIF_PCM] =
1001 SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
1002 [CONTROL_SPDIF_INPUT_BITS] =
1003 SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Clemens Ladisch893e44b2008-01-14 08:57:05 +01001004 [CONTROL_MIC_CAPTURE_SWITCH] = "Mic Capture Switch",
1005 [CONTROL_LINE_CAPTURE_SWITCH] = "Line Capture Switch",
1006 [CONTROL_CD_CAPTURE_SWITCH] = "CD Capture Switch",
1007 [CONTROL_AUX_CAPTURE_SWITCH] = "Aux Capture Switch",
Clemens Ladisch01a3aff2008-01-14 08:56:01 +01001008 };
1009 unsigned int i, j;
Clemens Ladischccc80fb2008-01-16 08:32:08 +01001010 struct snd_kcontrol_new template;
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001011 struct snd_kcontrol *ctl;
1012 int err;
1013
Clemens Ladisch31c77642008-01-16 08:28:17 +01001014 for (i = 0; i < count; ++i) {
Clemens Ladischccc80fb2008-01-16 08:32:08 +01001015 template = controls[i];
Clemens Ladisch9bd6a732008-09-22 08:55:19 +02001016 if (chip->model.control_filter) {
1017 err = chip->model.control_filter(&template);
Clemens Ladisch9f9115d2008-06-16 14:13:52 +02001018 if (err < 0)
1019 return err;
1020 if (err == 1)
1021 continue;
1022 }
Clemens Ladisch75919d72009-09-28 11:15:49 +02001023 if (!strcmp(template.name, "Stereo Upmixing") &&
Clemens Ladisch1f4d7be2011-01-10 15:59:38 +01001024 chip->model.dac_channels_pcm == 2)
Clemens Ladisch75919d72009-09-28 11:15:49 +02001025 continue;
Clemens Ladische96f38f2010-12-02 11:39:34 +01001026 if (!strcmp(template.name, "Mic Source Capture Enum") &&
1027 !(chip->model.device_config & AC97_FMIC_SWITCH))
1028 continue;
Clemens Ladischb6ca8ab2010-10-04 13:21:52 +02001029 if (!strncmp(template.name, "CD Capture ", 11) &&
1030 !(chip->model.device_config & AC97_CD_INPUT))
1031 continue;
Clemens Ladisch4972a172008-04-16 09:15:45 +02001032 if (!strcmp(template.name, "Master Playback Volume") &&
Clemens Ladisch9bd6a732008-09-22 08:55:19 +02001033 chip->model.dac_tlv) {
1034 template.tlv.p = chip->model.dac_tlv;
Clemens Ladisch4972a172008-04-16 09:15:45 +02001035 template.access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1036 }
Clemens Ladische9d88a82008-01-21 08:52:11 +01001037 ctl = snd_ctl_new1(&template, chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001038 if (!ctl)
1039 return -ENOMEM;
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001040 err = snd_ctl_add(chip->card, ctl);
1041 if (err < 0)
1042 return err;
Clemens Ladisch01a3aff2008-01-14 08:56:01 +01001043 for (j = 0; j < CONTROL_COUNT; ++j)
1044 if (!strcmp(ctl->id.name, known_ctl_names[j])) {
1045 chip->controls[j] = ctl;
1046 ctl->private_free = oxygen_any_ctl_free;
1047 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001048 }
Clemens Ladisch31c77642008-01-16 08:28:17 +01001049 return 0;
1050}
1051
1052int oxygen_mixer_init(struct oxygen *chip)
1053{
Clemens Ladischf009ad92008-03-19 08:19:41 +01001054 unsigned int i;
Clemens Ladisch31c77642008-01-16 08:28:17 +01001055 int err;
1056
1057 err = add_controls(chip, controls, ARRAY_SIZE(controls));
1058 if (err < 0)
1059 return err;
Clemens Ladischd76596b2008-09-22 09:02:08 +02001060 if (chip->model.device_config & CAPTURE_1_FROM_SPDIF) {
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +02001061 err = add_controls(chip, spdif_input_controls,
1062 ARRAY_SIZE(spdif_input_controls));
1063 if (err < 0)
1064 return err;
1065 }
Clemens Ladischf009ad92008-03-19 08:19:41 +01001066 for (i = 0; i < ARRAY_SIZE(monitor_controls); ++i) {
Clemens Ladischd76596b2008-09-22 09:02:08 +02001067 if (!(chip->model.device_config & monitor_controls[i].pcm_dev))
Clemens Ladischf009ad92008-03-19 08:19:41 +01001068 continue;
1069 err = add_controls(chip, monitor_controls[i].controls,
1070 ARRAY_SIZE(monitor_controls[i].controls));
Clemens Ladischfa5d8102008-03-19 08:17:33 +01001071 if (err < 0)
1072 return err;
1073 }
Clemens Ladisch31c77642008-01-16 08:28:17 +01001074 if (chip->has_ac97_0) {
1075 err = add_controls(chip, ac97_controls,
1076 ARRAY_SIZE(ac97_controls));
1077 if (err < 0)
1078 return err;
1079 }
Clemens Ladischa3601562008-01-28 08:35:20 +01001080 if (chip->has_ac97_1) {
1081 err = add_controls(chip, ac97_fp_controls,
1082 ARRAY_SIZE(ac97_fp_controls));
1083 if (err < 0)
1084 return err;
1085 }
Clemens Ladisch9bd6a732008-09-22 08:55:19 +02001086 return chip->model.mixer_init ? chip->model.mixer_init(chip) : 0;
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001087}