blob: 5dfb5fb73381a9aa7eac3ce779d5802c199a2261 [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 Ladisch9bd6a732008-09-22 08:55:19 +020034 info->count = chip->model.dac_channels;
35 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 Ladisch9bd6a732008-09-22 08:55:19 +020047 for (i = 0; i < chip->model.dac_channels; ++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 Ladisch9bd6a732008-09-22 08:55:19 +020062 for (i = 0; i < chip->model.dac_channels; ++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
100static int upmix_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
101{
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200102 static const char *const names[5] = {
103 "Front",
104 "Front+Surround",
105 "Front+Surround+Back",
106 "Front+Surround+Center/LFE",
107 "Front+Surround+Center/LFE+Back",
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100108 };
Clemens Ladisch976cd622008-01-25 08:37:49 +0100109 struct oxygen *chip = ctl->private_data;
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200110 unsigned int count = chip->model.update_center_lfe_mix ? 5 : 3;
Clemens Ladisch976cd622008-01-25 08:37:49 +0100111
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100112 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
113 info->count = 1;
Clemens Ladisch976cd622008-01-25 08:37:49 +0100114 info->value.enumerated.items = count;
115 if (info->value.enumerated.item >= count)
116 info->value.enumerated.item = count - 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100117 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
118 return 0;
119}
120
121static int upmix_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
122{
123 struct oxygen *chip = ctl->private_data;
124
125 mutex_lock(&chip->mutex);
126 value->value.enumerated.item[0] = chip->dac_routing;
127 mutex_unlock(&chip->mutex);
128 return 0;
129}
130
131void oxygen_update_dac_routing(struct oxygen *chip)
132{
Clemens Ladischc9946b22008-01-21 08:44:24 +0100133 /* DAC 0: front, DAC 1: surround, DAC 2: center/LFE, DAC 3: back */
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200134 static const unsigned int reg_values[5] = {
Clemens Ladischc9946b22008-01-21 08:44:24 +0100135 /* stereo -> front */
136 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
137 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
138 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
139 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
140 /* stereo -> front+surround */
141 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
142 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
143 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
144 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
145 /* stereo -> front+surround+back */
146 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
147 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
148 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
149 (0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200150 /* stereo -> front+surround+center/LFE */
151 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
152 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
153 (0 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
154 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
155 /* stereo -> front+surround+center/LFE+back */
156 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
157 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
158 (0 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
159 (0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100160 };
Clemens Ladisch7113e952008-01-14 08:55:03 +0100161 u8 channels;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100162 unsigned int reg_value;
163
Clemens Ladisch7113e952008-01-14 08:55:03 +0100164 channels = oxygen_read8(chip, OXYGEN_PLAY_CHANNELS) &
165 OXYGEN_PLAY_CHANNELS_MASK;
166 if (channels == OXYGEN_PLAY_CHANNELS_2)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100167 reg_value = reg_values[chip->dac_routing];
Clemens Ladisch7113e952008-01-14 08:55:03 +0100168 else if (channels == OXYGEN_PLAY_CHANNELS_8)
Clemens Ladischc9946b22008-01-21 08:44:24 +0100169 /* in 7.1 mode, "rear" channels go to the "back" jack */
170 reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
171 (3 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
172 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
173 (1 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100174 else
Clemens Ladischc9946b22008-01-21 08:44:24 +0100175 reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
176 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
177 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
178 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT);
179 oxygen_write16_masked(chip, OXYGEN_PLAY_ROUTING, reg_value,
180 OXYGEN_PLAY_DAC0_SOURCE_MASK |
181 OXYGEN_PLAY_DAC1_SOURCE_MASK |
182 OXYGEN_PLAY_DAC2_SOURCE_MASK |
183 OXYGEN_PLAY_DAC3_SOURCE_MASK);
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200184 if (chip->model.update_center_lfe_mix)
185 chip->model.update_center_lfe_mix(chip, chip->dac_routing > 2);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100186}
187
188static int upmix_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
189{
190 struct oxygen *chip = ctl->private_data;
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200191 unsigned int count = chip->model.update_center_lfe_mix ? 5 : 3;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100192 int changed;
193
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200194 if (value->value.enumerated.item[0] >= count)
195 return -EINVAL;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100196 mutex_lock(&chip->mutex);
197 changed = value->value.enumerated.item[0] != chip->dac_routing;
198 if (changed) {
Clemens Ladisch3d8bb452009-09-28 11:16:41 +0200199 chip->dac_routing = value->value.enumerated.item[0];
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100200 oxygen_update_dac_routing(chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100201 }
202 mutex_unlock(&chip->mutex);
203 return changed;
204}
205
206static int spdif_switch_get(struct snd_kcontrol *ctl,
207 struct snd_ctl_elem_value *value)
208{
209 struct oxygen *chip = ctl->private_data;
210
211 mutex_lock(&chip->mutex);
212 value->value.integer.value[0] = chip->spdif_playback_enable;
213 mutex_unlock(&chip->mutex);
214 return 0;
215}
216
217static unsigned int oxygen_spdif_rate(unsigned int oxygen_rate)
218{
219 switch (oxygen_rate) {
220 case OXYGEN_RATE_32000:
221 return IEC958_AES3_CON_FS_32000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
222 case OXYGEN_RATE_44100:
223 return IEC958_AES3_CON_FS_44100 << OXYGEN_SPDIF_CS_RATE_SHIFT;
224 default: /* OXYGEN_RATE_48000 */
225 return IEC958_AES3_CON_FS_48000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
226 case OXYGEN_RATE_64000:
227 return 0xb << OXYGEN_SPDIF_CS_RATE_SHIFT;
228 case OXYGEN_RATE_88200:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200229 return IEC958_AES3_CON_FS_88200 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100230 case OXYGEN_RATE_96000:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200231 return IEC958_AES3_CON_FS_96000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100232 case OXYGEN_RATE_176400:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200233 return IEC958_AES3_CON_FS_176400 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100234 case OXYGEN_RATE_192000:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200235 return IEC958_AES3_CON_FS_192000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100236 }
237}
238
239void oxygen_update_spdif_source(struct oxygen *chip)
240{
241 u32 old_control, new_control;
242 u16 old_routing, new_routing;
243 unsigned int oxygen_rate;
244
245 old_control = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
246 old_routing = oxygen_read16(chip, OXYGEN_PLAY_ROUTING);
247 if (chip->pcm_active & (1 << PCM_SPDIF)) {
248 new_control = old_control | OXYGEN_SPDIF_OUT_ENABLE;
Clemens Ladischc9946b22008-01-21 08:44:24 +0100249 new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
250 | OXYGEN_PLAY_SPDIF_SPDIF;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100251 oxygen_rate = (old_control >> OXYGEN_SPDIF_OUT_RATE_SHIFT)
252 & OXYGEN_I2S_RATE_MASK;
253 /* S/PDIF rate was already set by the caller */
254 } else if ((chip->pcm_active & (1 << PCM_MULTICH)) &&
255 chip->spdif_playback_enable) {
Clemens Ladischc9946b22008-01-21 08:44:24 +0100256 new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
257 | OXYGEN_PLAY_SPDIF_MULTICH_01;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100258 oxygen_rate = oxygen_read16(chip, OXYGEN_I2S_MULTICH_FORMAT)
259 & OXYGEN_I2S_RATE_MASK;
260 new_control = (old_control & ~OXYGEN_SPDIF_OUT_RATE_MASK) |
261 (oxygen_rate << OXYGEN_SPDIF_OUT_RATE_SHIFT) |
262 OXYGEN_SPDIF_OUT_ENABLE;
263 } else {
264 new_control = old_control & ~OXYGEN_SPDIF_OUT_ENABLE;
265 new_routing = old_routing;
266 oxygen_rate = OXYGEN_RATE_44100;
267 }
268 if (old_routing != new_routing) {
269 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL,
270 new_control & ~OXYGEN_SPDIF_OUT_ENABLE);
271 oxygen_write16(chip, OXYGEN_PLAY_ROUTING, new_routing);
272 }
273 if (new_control & OXYGEN_SPDIF_OUT_ENABLE)
274 oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS,
275 oxygen_spdif_rate(oxygen_rate) |
276 ((chip->pcm_active & (1 << PCM_SPDIF)) ?
277 chip->spdif_pcm_bits : chip->spdif_bits));
278 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, new_control);
279}
280
281static int spdif_switch_put(struct snd_kcontrol *ctl,
282 struct snd_ctl_elem_value *value)
283{
284 struct oxygen *chip = ctl->private_data;
285 int changed;
286
287 mutex_lock(&chip->mutex);
288 changed = value->value.integer.value[0] != chip->spdif_playback_enable;
289 if (changed) {
290 chip->spdif_playback_enable = !!value->value.integer.value[0];
291 spin_lock_irq(&chip->reg_lock);
292 oxygen_update_spdif_source(chip);
293 spin_unlock_irq(&chip->reg_lock);
294 }
295 mutex_unlock(&chip->mutex);
296 return changed;
297}
298
299static int spdif_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
300{
301 info->type = SNDRV_CTL_ELEM_TYPE_IEC958;
302 info->count = 1;
303 return 0;
304}
305
306static void oxygen_to_iec958(u32 bits, struct snd_ctl_elem_value *value)
307{
308 value->value.iec958.status[0] =
309 bits & (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C |
310 OXYGEN_SPDIF_PREEMPHASIS);
311 value->value.iec958.status[1] = /* category and original */
312 bits >> OXYGEN_SPDIF_CATEGORY_SHIFT;
313}
314
315static u32 iec958_to_oxygen(struct snd_ctl_elem_value *value)
316{
317 u32 bits;
318
319 bits = value->value.iec958.status[0] &
320 (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C |
321 OXYGEN_SPDIF_PREEMPHASIS);
322 bits |= value->value.iec958.status[1] << OXYGEN_SPDIF_CATEGORY_SHIFT;
323 if (bits & OXYGEN_SPDIF_NONAUDIO)
324 bits |= OXYGEN_SPDIF_V;
325 return bits;
326}
327
328static inline void write_spdif_bits(struct oxygen *chip, u32 bits)
329{
330 oxygen_write32_masked(chip, OXYGEN_SPDIF_OUTPUT_BITS, bits,
331 OXYGEN_SPDIF_NONAUDIO |
332 OXYGEN_SPDIF_C |
333 OXYGEN_SPDIF_PREEMPHASIS |
334 OXYGEN_SPDIF_CATEGORY_MASK |
335 OXYGEN_SPDIF_ORIGINAL |
336 OXYGEN_SPDIF_V);
337}
338
339static int spdif_default_get(struct snd_kcontrol *ctl,
340 struct snd_ctl_elem_value *value)
341{
342 struct oxygen *chip = ctl->private_data;
343
344 mutex_lock(&chip->mutex);
345 oxygen_to_iec958(chip->spdif_bits, value);
346 mutex_unlock(&chip->mutex);
347 return 0;
348}
349
350static int spdif_default_put(struct snd_kcontrol *ctl,
351 struct snd_ctl_elem_value *value)
352{
353 struct oxygen *chip = ctl->private_data;
354 u32 new_bits;
355 int changed;
356
357 new_bits = iec958_to_oxygen(value);
358 mutex_lock(&chip->mutex);
359 changed = new_bits != chip->spdif_bits;
360 if (changed) {
361 chip->spdif_bits = new_bits;
362 if (!(chip->pcm_active & (1 << PCM_SPDIF)))
363 write_spdif_bits(chip, new_bits);
364 }
365 mutex_unlock(&chip->mutex);
366 return changed;
367}
368
369static int spdif_mask_get(struct snd_kcontrol *ctl,
370 struct snd_ctl_elem_value *value)
371{
372 value->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
373 IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS;
374 value->value.iec958.status[1] =
375 IEC958_AES1_CON_CATEGORY | IEC958_AES1_CON_ORIGINAL;
376 return 0;
377}
378
379static int spdif_pcm_get(struct snd_kcontrol *ctl,
380 struct snd_ctl_elem_value *value)
381{
382 struct oxygen *chip = ctl->private_data;
383
384 mutex_lock(&chip->mutex);
385 oxygen_to_iec958(chip->spdif_pcm_bits, value);
386 mutex_unlock(&chip->mutex);
387 return 0;
388}
389
390static int spdif_pcm_put(struct snd_kcontrol *ctl,
391 struct snd_ctl_elem_value *value)
392{
393 struct oxygen *chip = ctl->private_data;
394 u32 new_bits;
395 int changed;
396
397 new_bits = iec958_to_oxygen(value);
398 mutex_lock(&chip->mutex);
399 changed = new_bits != chip->spdif_pcm_bits;
400 if (changed) {
401 chip->spdif_pcm_bits = new_bits;
402 if (chip->pcm_active & (1 << PCM_SPDIF))
403 write_spdif_bits(chip, new_bits);
404 }
405 mutex_unlock(&chip->mutex);
406 return changed;
407}
408
409static int spdif_input_mask_get(struct snd_kcontrol *ctl,
410 struct snd_ctl_elem_value *value)
411{
412 value->value.iec958.status[0] = 0xff;
413 value->value.iec958.status[1] = 0xff;
414 value->value.iec958.status[2] = 0xff;
415 value->value.iec958.status[3] = 0xff;
416 return 0;
417}
418
419static int spdif_input_default_get(struct snd_kcontrol *ctl,
420 struct snd_ctl_elem_value *value)
421{
422 struct oxygen *chip = ctl->private_data;
423 u32 bits;
424
425 bits = oxygen_read32(chip, OXYGEN_SPDIF_INPUT_BITS);
426 value->value.iec958.status[0] = bits;
427 value->value.iec958.status[1] = bits >> 8;
428 value->value.iec958.status[2] = bits >> 16;
429 value->value.iec958.status[3] = bits >> 24;
430 return 0;
431}
432
Clemens Ladisch02f21c92008-01-22 08:36:03 +0100433static int spdif_loopback_get(struct snd_kcontrol *ctl,
434 struct snd_ctl_elem_value *value)
435{
436 struct oxygen *chip = ctl->private_data;
437
438 value->value.integer.value[0] =
439 !!(oxygen_read32(chip, OXYGEN_SPDIF_CONTROL)
440 & OXYGEN_SPDIF_LOOPBACK);
441 return 0;
442}
443
444static int spdif_loopback_put(struct snd_kcontrol *ctl,
445 struct snd_ctl_elem_value *value)
446{
447 struct oxygen *chip = ctl->private_data;
448 u32 oldreg, newreg;
449 int changed;
450
451 spin_lock_irq(&chip->reg_lock);
452 oldreg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
453 if (value->value.integer.value[0])
454 newreg = oldreg | OXYGEN_SPDIF_LOOPBACK;
455 else
456 newreg = oldreg & ~OXYGEN_SPDIF_LOOPBACK;
457 changed = newreg != oldreg;
458 if (changed)
459 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, newreg);
460 spin_unlock_irq(&chip->reg_lock);
461 return changed;
462}
463
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100464static int monitor_volume_info(struct snd_kcontrol *ctl,
465 struct snd_ctl_elem_info *info)
466{
467 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
468 info->count = 1;
469 info->value.integer.min = 0;
470 info->value.integer.max = 1;
471 return 0;
472}
473
474static int monitor_get(struct snd_kcontrol *ctl,
475 struct snd_ctl_elem_value *value)
476{
477 struct oxygen *chip = ctl->private_data;
478 u8 bit = ctl->private_value;
479 int invert = ctl->private_value & (1 << 8);
480
481 value->value.integer.value[0] =
482 !!invert ^ !!(oxygen_read8(chip, OXYGEN_ADC_MONITOR) & bit);
483 return 0;
484}
485
486static int monitor_put(struct snd_kcontrol *ctl,
487 struct snd_ctl_elem_value *value)
488{
489 struct oxygen *chip = ctl->private_data;
490 u8 bit = ctl->private_value;
491 int invert = ctl->private_value & (1 << 8);
492 u8 oldreg, newreg;
493 int changed;
494
495 spin_lock_irq(&chip->reg_lock);
496 oldreg = oxygen_read8(chip, OXYGEN_ADC_MONITOR);
497 if ((!!value->value.integer.value[0] ^ !!invert) != 0)
498 newreg = oldreg | bit;
499 else
500 newreg = oldreg & ~bit;
501 changed = newreg != oldreg;
502 if (changed)
503 oxygen_write8(chip, OXYGEN_ADC_MONITOR, newreg);
504 spin_unlock_irq(&chip->reg_lock);
505 return changed;
506}
507
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100508static int ac97_switch_get(struct snd_kcontrol *ctl,
509 struct snd_ctl_elem_value *value)
510{
511 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100512 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100513 unsigned int index = ctl->private_value & 0xff;
514 unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
515 int invert = ctl->private_value & (1 << 16);
516 u16 reg;
517
518 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100519 reg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100520 mutex_unlock(&chip->mutex);
521 if (!(reg & (1 << bitnr)) ^ !invert)
522 value->value.integer.value[0] = 1;
523 else
524 value->value.integer.value[0] = 0;
525 return 0;
526}
527
Clemens Ladische97f7992008-04-01 10:02:18 +0200528static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
529{
Clemens Ladisch3d839e52008-08-26 11:06:26 +0200530 unsigned int priv_idx;
Clemens Ladische97f7992008-04-01 10:02:18 +0200531 u16 value;
532
Clemens Ladisch3d839e52008-08-26 11:06:26 +0200533 if (!chip->controls[control])
534 return;
535 priv_idx = chip->controls[control]->private_value & 0xff;
Clemens Ladische97f7992008-04-01 10:02:18 +0200536 value = oxygen_read_ac97(chip, 0, priv_idx);
537 if (!(value & 0x8000)) {
538 oxygen_write_ac97(chip, 0, priv_idx, value | 0x8000);
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200539 if (chip->model.ac97_switch)
540 chip->model.ac97_switch(chip, priv_idx, 0x8000);
Clemens Ladische97f7992008-04-01 10:02:18 +0200541 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
542 &chip->controls[control]->id);
543 }
544}
545
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100546static int ac97_switch_put(struct snd_kcontrol *ctl,
547 struct snd_ctl_elem_value *value)
548{
549 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100550 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100551 unsigned int index = ctl->private_value & 0xff;
552 unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
553 int invert = ctl->private_value & (1 << 16);
554 u16 oldreg, newreg;
555 int change;
556
557 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100558 oldreg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100559 newreg = oldreg;
560 if (!value->value.integer.value[0] ^ !invert)
561 newreg |= 1 << bitnr;
562 else
563 newreg &= ~(1 << bitnr);
564 change = newreg != oldreg;
565 if (change) {
Clemens Ladischa3601562008-01-28 08:35:20 +0100566 oxygen_write_ac97(chip, codec, index, newreg);
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200567 if (codec == 0 && chip->model.ac97_switch)
568 chip->model.ac97_switch(chip, index, newreg & 0x8000);
Clemens Ladische97f7992008-04-01 10:02:18 +0200569 if (index == AC97_LINE) {
570 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
571 newreg & 0x8000 ?
572 CM9780_GPO0 : 0, CM9780_GPO0);
573 if (!(newreg & 0x8000)) {
574 mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
575 mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
576 mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
577 }
578 } else if ((index == AC97_MIC || index == AC97_CD ||
579 index == AC97_VIDEO || index == AC97_AUX) &&
580 bitnr == 15 && !(newreg & 0x8000)) {
581 mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
582 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
583 CM9780_GPO0, CM9780_GPO0);
584 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100585 }
586 mutex_unlock(&chip->mutex);
587 return change;
588}
589
590static int ac97_volume_info(struct snd_kcontrol *ctl,
591 struct snd_ctl_elem_info *info)
592{
Clemens Ladisch14744d72009-06-25 14:28:49 +0200593 int stereo = (ctl->private_value >> 16) & 1;
594
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100595 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Clemens Ladisch14744d72009-06-25 14:28:49 +0200596 info->count = stereo ? 2 : 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100597 info->value.integer.min = 0;
598 info->value.integer.max = 0x1f;
599 return 0;
600}
601
602static int ac97_volume_get(struct snd_kcontrol *ctl,
603 struct snd_ctl_elem_value *value)
604{
605 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100606 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladisch14744d72009-06-25 14:28:49 +0200607 int stereo = (ctl->private_value >> 16) & 1;
Clemens Ladischa3601562008-01-28 08:35:20 +0100608 unsigned int index = ctl->private_value & 0xff;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100609 u16 reg;
610
611 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100612 reg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100613 mutex_unlock(&chip->mutex);
614 value->value.integer.value[0] = 31 - (reg & 0x1f);
Clemens Ladisch14744d72009-06-25 14:28:49 +0200615 if (stereo)
616 value->value.integer.value[1] = 31 - ((reg >> 8) & 0x1f);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100617 return 0;
618}
619
620static int ac97_volume_put(struct snd_kcontrol *ctl,
621 struct snd_ctl_elem_value *value)
622{
623 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100624 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladisch14744d72009-06-25 14:28:49 +0200625 int stereo = (ctl->private_value >> 16) & 1;
Clemens Ladischa3601562008-01-28 08:35:20 +0100626 unsigned int index = ctl->private_value & 0xff;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100627 u16 oldreg, newreg;
628 int change;
629
630 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100631 oldreg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100632 newreg = oldreg;
633 newreg = (newreg & ~0x1f) |
634 (31 - (value->value.integer.value[0] & 0x1f));
Clemens Ladisch14744d72009-06-25 14:28:49 +0200635 if (stereo)
636 newreg = (newreg & ~0x1f00) |
637 ((31 - (value->value.integer.value[1] & 0x1f)) << 8);
638 else
639 newreg = (newreg & ~0x1f00) | ((newreg & 0x1f) << 8);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100640 change = newreg != oldreg;
641 if (change)
Clemens Ladischa3601562008-01-28 08:35:20 +0100642 oxygen_write_ac97(chip, codec, index, newreg);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100643 mutex_unlock(&chip->mutex);
644 return change;
645}
646
Clemens Ladischa3601562008-01-28 08:35:20 +0100647static int ac97_fp_rec_volume_info(struct snd_kcontrol *ctl,
648 struct snd_ctl_elem_info *info)
649{
650 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
651 info->count = 2;
652 info->value.integer.min = 0;
653 info->value.integer.max = 7;
654 return 0;
655}
656
657static int ac97_fp_rec_volume_get(struct snd_kcontrol *ctl,
658 struct snd_ctl_elem_value *value)
659{
660 struct oxygen *chip = ctl->private_data;
661 u16 reg;
662
663 mutex_lock(&chip->mutex);
664 reg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
665 mutex_unlock(&chip->mutex);
666 value->value.integer.value[0] = reg & 7;
667 value->value.integer.value[1] = (reg >> 8) & 7;
668 return 0;
669}
670
671static int ac97_fp_rec_volume_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, 1, AC97_REC_GAIN);
680 newreg = oldreg & ~0x0707;
681 newreg = newreg | (value->value.integer.value[0] & 7);
682 newreg = newreg | ((value->value.integer.value[0] & 7) << 8);
683 change = newreg != oldreg;
684 if (change)
685 oxygen_write_ac97(chip, 1, AC97_REC_GAIN, newreg);
686 mutex_unlock(&chip->mutex);
687 return change;
688}
689
690#define AC97_SWITCH(xname, codec, index, bitnr, invert) { \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100691 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
692 .name = xname, \
693 .info = snd_ctl_boolean_mono_info, \
694 .get = ac97_switch_get, \
695 .put = ac97_switch_put, \
Clemens Ladischa3601562008-01-28 08:35:20 +0100696 .private_value = ((codec) << 24) | ((invert) << 16) | \
697 ((bitnr) << 8) | (index), \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100698 }
Clemens Ladisch14744d72009-06-25 14:28:49 +0200699#define AC97_VOLUME(xname, codec, index, stereo) { \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100700 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
701 .name = xname, \
Clemens Ladischa3601562008-01-28 08:35:20 +0100702 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
703 SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100704 .info = ac97_volume_info, \
705 .get = ac97_volume_get, \
706 .put = ac97_volume_put, \
707 .tlv = { .p = ac97_db_scale, }, \
Clemens Ladisch14744d72009-06-25 14:28:49 +0200708 .private_value = ((codec) << 24) | ((stereo) << 16) | (index), \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100709 }
710
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100711static DECLARE_TLV_DB_SCALE(monitor_db_scale, -1000, 1000, 0);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100712static DECLARE_TLV_DB_SCALE(ac97_db_scale, -3450, 150, 0);
Clemens Ladischa3601562008-01-28 08:35:20 +0100713static DECLARE_TLV_DB_SCALE(ac97_rec_db_scale, 0, 150, 0);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100714
715static const struct snd_kcontrol_new controls[] = {
716 {
717 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladischfb920b72008-01-15 08:39:06 +0100718 .name = "Master Playback Volume",
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100719 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100720 .info = dac_volume_info,
721 .get = dac_volume_get,
722 .put = dac_volume_put,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100723 },
724 {
725 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladischfb920b72008-01-15 08:39:06 +0100726 .name = "Master Playback Switch",
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100727 .info = snd_ctl_boolean_mono_info,
728 .get = dac_mute_get,
729 .put = dac_mute_put,
730 },
731 {
732 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
733 .name = "Stereo Upmixing",
734 .info = upmix_info,
735 .get = upmix_get,
736 .put = upmix_put,
737 },
738 {
739 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
740 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
741 .info = snd_ctl_boolean_mono_info,
742 .get = spdif_switch_get,
743 .put = spdif_switch_put,
744 },
745 {
746 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
747 .device = 1,
748 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
749 .info = spdif_info,
750 .get = spdif_default_get,
751 .put = spdif_default_put,
752 },
753 {
754 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
755 .device = 1,
756 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
757 .access = SNDRV_CTL_ELEM_ACCESS_READ,
758 .info = spdif_info,
759 .get = spdif_mask_get,
760 },
761 {
762 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
763 .device = 1,
764 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
765 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
766 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
767 .info = spdif_info,
768 .get = spdif_pcm_get,
769 .put = spdif_pcm_put,
770 },
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +0200771};
772
773static const struct snd_kcontrol_new spdif_input_controls[] = {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100774 {
775 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
776 .device = 1,
777 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK),
778 .access = SNDRV_CTL_ELEM_ACCESS_READ,
779 .info = spdif_info,
780 .get = spdif_input_mask_get,
781 },
782 {
783 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
784 .device = 1,
785 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
786 .access = SNDRV_CTL_ELEM_ACCESS_READ,
787 .info = spdif_info,
788 .get = spdif_input_default_get,
789 },
Clemens Ladisch02f21c92008-01-22 08:36:03 +0100790 {
791 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
792 .name = SNDRV_CTL_NAME_IEC958("Loopback ", NONE, SWITCH),
793 .info = snd_ctl_boolean_mono_info,
794 .get = spdif_loopback_get,
795 .put = spdif_loopback_put,
796 },
Clemens Ladisch31c77642008-01-16 08:28:17 +0100797};
798
Clemens Ladischf009ad92008-03-19 08:19:41 +0100799static const struct {
800 unsigned int pcm_dev;
801 struct snd_kcontrol_new controls[2];
802} monitor_controls[] = {
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100803 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100804 .pcm_dev = CAPTURE_0_FROM_I2S_1,
805 .controls = {
806 {
807 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
808 .name = "Analog Input Monitor Switch",
809 .info = snd_ctl_boolean_mono_info,
810 .get = monitor_get,
811 .put = monitor_put,
812 .private_value = OXYGEN_ADC_MONITOR_A,
813 },
814 {
815 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
816 .name = "Analog Input Monitor Volume",
817 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
818 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
819 .info = monitor_volume_info,
820 .get = monitor_get,
821 .put = monitor_put,
822 .private_value = OXYGEN_ADC_MONITOR_A_HALF_VOL
823 | (1 << 8),
824 .tlv = { .p = monitor_db_scale, },
825 },
826 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100827 },
828 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100829 .pcm_dev = CAPTURE_0_FROM_I2S_2,
830 .controls = {
831 {
832 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
833 .name = "Analog Input Monitor Switch",
834 .info = snd_ctl_boolean_mono_info,
835 .get = monitor_get,
836 .put = monitor_put,
837 .private_value = OXYGEN_ADC_MONITOR_B,
838 },
839 {
840 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
841 .name = "Analog Input Monitor Volume",
842 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
843 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
844 .info = monitor_volume_info,
845 .get = monitor_get,
846 .put = monitor_put,
847 .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
848 | (1 << 8),
849 .tlv = { .p = monitor_db_scale, },
850 },
851 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100852 },
853 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100854 .pcm_dev = CAPTURE_2_FROM_I2S_2,
855 .controls = {
856 {
857 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
858 .name = "Analog Input Monitor Switch",
859 .index = 1,
860 .info = snd_ctl_boolean_mono_info,
861 .get = monitor_get,
862 .put = monitor_put,
863 .private_value = OXYGEN_ADC_MONITOR_B,
864 },
865 {
866 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
867 .name = "Analog Input Monitor Volume",
868 .index = 1,
869 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
870 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
871 .info = monitor_volume_info,
872 .get = monitor_get,
873 .put = monitor_put,
874 .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
875 | (1 << 8),
876 .tlv = { .p = monitor_db_scale, },
877 },
878 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100879 },
880 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100881 .pcm_dev = CAPTURE_1_FROM_SPDIF,
882 .controls = {
883 {
884 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
885 .name = "Digital Input Monitor Switch",
886 .info = snd_ctl_boolean_mono_info,
887 .get = monitor_get,
888 .put = monitor_put,
889 .private_value = OXYGEN_ADC_MONITOR_C,
890 },
891 {
892 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
893 .name = "Digital Input Monitor Volume",
894 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
895 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
896 .info = monitor_volume_info,
897 .get = monitor_get,
898 .put = monitor_put,
899 .private_value = OXYGEN_ADC_MONITOR_C_HALF_VOL
900 | (1 << 8),
901 .tlv = { .p = monitor_db_scale, },
902 },
903 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100904 },
905};
906
Clemens Ladisch31c77642008-01-16 08:28:17 +0100907static const struct snd_kcontrol_new ac97_controls[] = {
Clemens Ladisch14744d72009-06-25 14:28:49 +0200908 AC97_VOLUME("Mic Capture Volume", 0, AC97_MIC, 0),
Clemens Ladischa3601562008-01-28 08:35:20 +0100909 AC97_SWITCH("Mic Capture Switch", 0, AC97_MIC, 15, 1),
910 AC97_SWITCH("Mic Boost (+20dB)", 0, AC97_MIC, 6, 0),
Clemens Ladischa3601562008-01-28 08:35:20 +0100911 AC97_SWITCH("Line Capture Switch", 0, AC97_LINE, 15, 1),
Clemens Ladisch14744d72009-06-25 14:28:49 +0200912 AC97_VOLUME("CD Capture Volume", 0, AC97_CD, 1),
Clemens Ladischa3601562008-01-28 08:35:20 +0100913 AC97_SWITCH("CD Capture Switch", 0, AC97_CD, 15, 1),
Clemens Ladisch14744d72009-06-25 14:28:49 +0200914 AC97_VOLUME("Aux Capture Volume", 0, AC97_AUX, 1),
Clemens Ladischa3601562008-01-28 08:35:20 +0100915 AC97_SWITCH("Aux Capture Switch", 0, AC97_AUX, 15, 1),
916};
917
918static const struct snd_kcontrol_new ac97_fp_controls[] = {
Clemens Ladisch14744d72009-06-25 14:28:49 +0200919 AC97_VOLUME("Front Panel Playback Volume", 1, AC97_HEADPHONE, 1),
Clemens Ladischa3601562008-01-28 08:35:20 +0100920 AC97_SWITCH("Front Panel Playback Switch", 1, AC97_HEADPHONE, 15, 1),
921 {
922 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
923 .name = "Front Panel Capture Volume",
924 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
925 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
926 .info = ac97_fp_rec_volume_info,
927 .get = ac97_fp_rec_volume_get,
928 .put = ac97_fp_rec_volume_put,
929 .tlv = { .p = ac97_rec_db_scale, },
930 },
931 AC97_SWITCH("Front Panel Capture Switch", 1, AC97_REC_GAIN, 15, 1),
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100932};
933
934static void oxygen_any_ctl_free(struct snd_kcontrol *ctl)
935{
936 struct oxygen *chip = ctl->private_data;
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100937 unsigned int i;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100938
939 /* I'm too lazy to write a function for each control :-) */
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100940 for (i = 0; i < ARRAY_SIZE(chip->controls); ++i)
941 chip->controls[i] = NULL;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100942}
943
Clemens Ladisch31c77642008-01-16 08:28:17 +0100944static int add_controls(struct oxygen *chip,
945 const struct snd_kcontrol_new controls[],
946 unsigned int count)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100947{
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100948 static const char *const known_ctl_names[CONTROL_COUNT] = {
949 [CONTROL_SPDIF_PCM] =
950 SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
951 [CONTROL_SPDIF_INPUT_BITS] =
952 SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Clemens Ladisch893e44b2008-01-14 08:57:05 +0100953 [CONTROL_MIC_CAPTURE_SWITCH] = "Mic Capture Switch",
954 [CONTROL_LINE_CAPTURE_SWITCH] = "Line Capture Switch",
955 [CONTROL_CD_CAPTURE_SWITCH] = "CD Capture Switch",
956 [CONTROL_AUX_CAPTURE_SWITCH] = "Aux Capture Switch",
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100957 };
958 unsigned int i, j;
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100959 struct snd_kcontrol_new template;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100960 struct snd_kcontrol *ctl;
961 int err;
962
Clemens Ladisch31c77642008-01-16 08:28:17 +0100963 for (i = 0; i < count; ++i) {
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100964 template = controls[i];
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200965 if (chip->model.control_filter) {
966 err = chip->model.control_filter(&template);
Clemens Ladisch9f9115d2008-06-16 14:13:52 +0200967 if (err < 0)
968 return err;
969 if (err == 1)
970 continue;
971 }
Clemens Ladisch75919d72009-09-28 11:15:49 +0200972 if (!strcmp(template.name, "Stereo Upmixing") &&
973 chip->model.dac_channels == 2)
974 continue;
Clemens Ladisch4972a172008-04-16 09:15:45 +0200975 if (!strcmp(template.name, "Master Playback Volume") &&
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200976 chip->model.dac_tlv) {
977 template.tlv.p = chip->model.dac_tlv;
Clemens Ladisch4972a172008-04-16 09:15:45 +0200978 template.access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
979 }
Clemens Ladische9d88a82008-01-21 08:52:11 +0100980 ctl = snd_ctl_new1(&template, chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100981 if (!ctl)
982 return -ENOMEM;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100983 err = snd_ctl_add(chip->card, ctl);
984 if (err < 0)
985 return err;
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100986 for (j = 0; j < CONTROL_COUNT; ++j)
987 if (!strcmp(ctl->id.name, known_ctl_names[j])) {
988 chip->controls[j] = ctl;
989 ctl->private_free = oxygen_any_ctl_free;
990 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100991 }
Clemens Ladisch31c77642008-01-16 08:28:17 +0100992 return 0;
993}
994
995int oxygen_mixer_init(struct oxygen *chip)
996{
Clemens Ladischf009ad92008-03-19 08:19:41 +0100997 unsigned int i;
Clemens Ladisch31c77642008-01-16 08:28:17 +0100998 int err;
999
1000 err = add_controls(chip, controls, ARRAY_SIZE(controls));
1001 if (err < 0)
1002 return err;
Clemens Ladischd76596b2008-09-22 09:02:08 +02001003 if (chip->model.device_config & CAPTURE_1_FROM_SPDIF) {
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +02001004 err = add_controls(chip, spdif_input_controls,
1005 ARRAY_SIZE(spdif_input_controls));
1006 if (err < 0)
1007 return err;
1008 }
Clemens Ladischf009ad92008-03-19 08:19:41 +01001009 for (i = 0; i < ARRAY_SIZE(monitor_controls); ++i) {
Clemens Ladischd76596b2008-09-22 09:02:08 +02001010 if (!(chip->model.device_config & monitor_controls[i].pcm_dev))
Clemens Ladischf009ad92008-03-19 08:19:41 +01001011 continue;
1012 err = add_controls(chip, monitor_controls[i].controls,
1013 ARRAY_SIZE(monitor_controls[i].controls));
Clemens Ladischfa5d8102008-03-19 08:17:33 +01001014 if (err < 0)
1015 return err;
1016 }
Clemens Ladisch31c77642008-01-16 08:28:17 +01001017 if (chip->has_ac97_0) {
1018 err = add_controls(chip, ac97_controls,
1019 ARRAY_SIZE(ac97_controls));
1020 if (err < 0)
1021 return err;
1022 }
Clemens Ladischa3601562008-01-28 08:35:20 +01001023 if (chip->has_ac97_1) {
1024 err = add_controls(chip, ac97_fp_controls,
1025 ARRAY_SIZE(ac97_fp_controls));
1026 if (err < 0)
1027 return err;
1028 }
Clemens Ladisch9bd6a732008-09-22 08:55:19 +02001029 return chip->model.mixer_init ? chip->model.mixer_init(chip) : 0;
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001030}