blob: 304da169bfdcbeacb669a338a25a5f4c12271de9 [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{
102 static const char *const names[3] = {
Clemens Ladisch7113e952008-01-14 08:55:03 +0100103 "Front", "Front+Surround", "Front+Surround+Back"
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100104 };
Clemens Ladisch976cd622008-01-25 08:37:49 +0100105 struct oxygen *chip = ctl->private_data;
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200106 unsigned int count = 2 + (chip->model.dac_channels == 8);
Clemens Ladisch976cd622008-01-25 08:37:49 +0100107
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100108 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
109 info->count = 1;
Clemens Ladisch976cd622008-01-25 08:37:49 +0100110 info->value.enumerated.items = count;
111 if (info->value.enumerated.item >= count)
112 info->value.enumerated.item = count - 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100113 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
114 return 0;
115}
116
117static int upmix_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
118{
119 struct oxygen *chip = ctl->private_data;
120
121 mutex_lock(&chip->mutex);
122 value->value.enumerated.item[0] = chip->dac_routing;
123 mutex_unlock(&chip->mutex);
124 return 0;
125}
126
127void oxygen_update_dac_routing(struct oxygen *chip)
128{
Clemens Ladischc9946b22008-01-21 08:44:24 +0100129 /* DAC 0: front, DAC 1: surround, DAC 2: center/LFE, DAC 3: back */
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100130 static const unsigned int reg_values[3] = {
Clemens Ladischc9946b22008-01-21 08:44:24 +0100131 /* stereo -> front */
132 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
133 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
134 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
135 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
136 /* stereo -> front+surround */
137 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
138 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
139 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
140 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
141 /* stereo -> front+surround+back */
142 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
143 (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
144 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
145 (0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100146 };
Clemens Ladisch7113e952008-01-14 08:55:03 +0100147 u8 channels;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100148 unsigned int reg_value;
149
Clemens Ladisch7113e952008-01-14 08:55:03 +0100150 channels = oxygen_read8(chip, OXYGEN_PLAY_CHANNELS) &
151 OXYGEN_PLAY_CHANNELS_MASK;
152 if (channels == OXYGEN_PLAY_CHANNELS_2)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100153 reg_value = reg_values[chip->dac_routing];
Clemens Ladisch7113e952008-01-14 08:55:03 +0100154 else if (channels == OXYGEN_PLAY_CHANNELS_8)
Clemens Ladischc9946b22008-01-21 08:44:24 +0100155 /* in 7.1 mode, "rear" channels go to the "back" jack */
156 reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
157 (3 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
158 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
159 (1 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100160 else
Clemens Ladischc9946b22008-01-21 08:44:24 +0100161 reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
162 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
163 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
164 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT);
165 oxygen_write16_masked(chip, OXYGEN_PLAY_ROUTING, reg_value,
166 OXYGEN_PLAY_DAC0_SOURCE_MASK |
167 OXYGEN_PLAY_DAC1_SOURCE_MASK |
168 OXYGEN_PLAY_DAC2_SOURCE_MASK |
169 OXYGEN_PLAY_DAC3_SOURCE_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100170}
171
172static int upmix_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
173{
174 struct oxygen *chip = ctl->private_data;
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200175 unsigned int count = 2 + (chip->model.dac_channels == 8);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100176 int changed;
177
178 mutex_lock(&chip->mutex);
179 changed = value->value.enumerated.item[0] != chip->dac_routing;
180 if (changed) {
Clemens Ladisch976cd622008-01-25 08:37:49 +0100181 chip->dac_routing = min(value->value.enumerated.item[0],
182 count - 1);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100183 spin_lock_irq(&chip->reg_lock);
184 oxygen_update_dac_routing(chip);
185 spin_unlock_irq(&chip->reg_lock);
186 }
187 mutex_unlock(&chip->mutex);
188 return changed;
189}
190
191static int spdif_switch_get(struct snd_kcontrol *ctl,
192 struct snd_ctl_elem_value *value)
193{
194 struct oxygen *chip = ctl->private_data;
195
196 mutex_lock(&chip->mutex);
197 value->value.integer.value[0] = chip->spdif_playback_enable;
198 mutex_unlock(&chip->mutex);
199 return 0;
200}
201
202static unsigned int oxygen_spdif_rate(unsigned int oxygen_rate)
203{
204 switch (oxygen_rate) {
205 case OXYGEN_RATE_32000:
206 return IEC958_AES3_CON_FS_32000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
207 case OXYGEN_RATE_44100:
208 return IEC958_AES3_CON_FS_44100 << OXYGEN_SPDIF_CS_RATE_SHIFT;
209 default: /* OXYGEN_RATE_48000 */
210 return IEC958_AES3_CON_FS_48000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
211 case OXYGEN_RATE_64000:
212 return 0xb << OXYGEN_SPDIF_CS_RATE_SHIFT;
213 case OXYGEN_RATE_88200:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200214 return IEC958_AES3_CON_FS_88200 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100215 case OXYGEN_RATE_96000:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200216 return IEC958_AES3_CON_FS_96000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100217 case OXYGEN_RATE_176400:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200218 return IEC958_AES3_CON_FS_176400 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100219 case OXYGEN_RATE_192000:
Clemens Ladisch6627bea2008-09-22 08:53:31 +0200220 return IEC958_AES3_CON_FS_192000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100221 }
222}
223
224void oxygen_update_spdif_source(struct oxygen *chip)
225{
226 u32 old_control, new_control;
227 u16 old_routing, new_routing;
228 unsigned int oxygen_rate;
229
230 old_control = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
231 old_routing = oxygen_read16(chip, OXYGEN_PLAY_ROUTING);
232 if (chip->pcm_active & (1 << PCM_SPDIF)) {
233 new_control = old_control | OXYGEN_SPDIF_OUT_ENABLE;
Clemens Ladischc9946b22008-01-21 08:44:24 +0100234 new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
235 | OXYGEN_PLAY_SPDIF_SPDIF;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100236 oxygen_rate = (old_control >> OXYGEN_SPDIF_OUT_RATE_SHIFT)
237 & OXYGEN_I2S_RATE_MASK;
238 /* S/PDIF rate was already set by the caller */
239 } else if ((chip->pcm_active & (1 << PCM_MULTICH)) &&
240 chip->spdif_playback_enable) {
Clemens Ladischc9946b22008-01-21 08:44:24 +0100241 new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
242 | OXYGEN_PLAY_SPDIF_MULTICH_01;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100243 oxygen_rate = oxygen_read16(chip, OXYGEN_I2S_MULTICH_FORMAT)
244 & OXYGEN_I2S_RATE_MASK;
245 new_control = (old_control & ~OXYGEN_SPDIF_OUT_RATE_MASK) |
246 (oxygen_rate << OXYGEN_SPDIF_OUT_RATE_SHIFT) |
247 OXYGEN_SPDIF_OUT_ENABLE;
248 } else {
249 new_control = old_control & ~OXYGEN_SPDIF_OUT_ENABLE;
250 new_routing = old_routing;
251 oxygen_rate = OXYGEN_RATE_44100;
252 }
253 if (old_routing != new_routing) {
254 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL,
255 new_control & ~OXYGEN_SPDIF_OUT_ENABLE);
256 oxygen_write16(chip, OXYGEN_PLAY_ROUTING, new_routing);
257 }
258 if (new_control & OXYGEN_SPDIF_OUT_ENABLE)
259 oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS,
260 oxygen_spdif_rate(oxygen_rate) |
261 ((chip->pcm_active & (1 << PCM_SPDIF)) ?
262 chip->spdif_pcm_bits : chip->spdif_bits));
263 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, new_control);
264}
265
266static int spdif_switch_put(struct snd_kcontrol *ctl,
267 struct snd_ctl_elem_value *value)
268{
269 struct oxygen *chip = ctl->private_data;
270 int changed;
271
272 mutex_lock(&chip->mutex);
273 changed = value->value.integer.value[0] != chip->spdif_playback_enable;
274 if (changed) {
275 chip->spdif_playback_enable = !!value->value.integer.value[0];
276 spin_lock_irq(&chip->reg_lock);
277 oxygen_update_spdif_source(chip);
278 spin_unlock_irq(&chip->reg_lock);
279 }
280 mutex_unlock(&chip->mutex);
281 return changed;
282}
283
284static int spdif_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
285{
286 info->type = SNDRV_CTL_ELEM_TYPE_IEC958;
287 info->count = 1;
288 return 0;
289}
290
291static void oxygen_to_iec958(u32 bits, struct snd_ctl_elem_value *value)
292{
293 value->value.iec958.status[0] =
294 bits & (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C |
295 OXYGEN_SPDIF_PREEMPHASIS);
296 value->value.iec958.status[1] = /* category and original */
297 bits >> OXYGEN_SPDIF_CATEGORY_SHIFT;
298}
299
300static u32 iec958_to_oxygen(struct snd_ctl_elem_value *value)
301{
302 u32 bits;
303
304 bits = value->value.iec958.status[0] &
305 (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C |
306 OXYGEN_SPDIF_PREEMPHASIS);
307 bits |= value->value.iec958.status[1] << OXYGEN_SPDIF_CATEGORY_SHIFT;
308 if (bits & OXYGEN_SPDIF_NONAUDIO)
309 bits |= OXYGEN_SPDIF_V;
310 return bits;
311}
312
313static inline void write_spdif_bits(struct oxygen *chip, u32 bits)
314{
315 oxygen_write32_masked(chip, OXYGEN_SPDIF_OUTPUT_BITS, bits,
316 OXYGEN_SPDIF_NONAUDIO |
317 OXYGEN_SPDIF_C |
318 OXYGEN_SPDIF_PREEMPHASIS |
319 OXYGEN_SPDIF_CATEGORY_MASK |
320 OXYGEN_SPDIF_ORIGINAL |
321 OXYGEN_SPDIF_V);
322}
323
324static int spdif_default_get(struct snd_kcontrol *ctl,
325 struct snd_ctl_elem_value *value)
326{
327 struct oxygen *chip = ctl->private_data;
328
329 mutex_lock(&chip->mutex);
330 oxygen_to_iec958(chip->spdif_bits, value);
331 mutex_unlock(&chip->mutex);
332 return 0;
333}
334
335static int spdif_default_put(struct snd_kcontrol *ctl,
336 struct snd_ctl_elem_value *value)
337{
338 struct oxygen *chip = ctl->private_data;
339 u32 new_bits;
340 int changed;
341
342 new_bits = iec958_to_oxygen(value);
343 mutex_lock(&chip->mutex);
344 changed = new_bits != chip->spdif_bits;
345 if (changed) {
346 chip->spdif_bits = new_bits;
347 if (!(chip->pcm_active & (1 << PCM_SPDIF)))
348 write_spdif_bits(chip, new_bits);
349 }
350 mutex_unlock(&chip->mutex);
351 return changed;
352}
353
354static int spdif_mask_get(struct snd_kcontrol *ctl,
355 struct snd_ctl_elem_value *value)
356{
357 value->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
358 IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS;
359 value->value.iec958.status[1] =
360 IEC958_AES1_CON_CATEGORY | IEC958_AES1_CON_ORIGINAL;
361 return 0;
362}
363
364static int spdif_pcm_get(struct snd_kcontrol *ctl,
365 struct snd_ctl_elem_value *value)
366{
367 struct oxygen *chip = ctl->private_data;
368
369 mutex_lock(&chip->mutex);
370 oxygen_to_iec958(chip->spdif_pcm_bits, value);
371 mutex_unlock(&chip->mutex);
372 return 0;
373}
374
375static int spdif_pcm_put(struct snd_kcontrol *ctl,
376 struct snd_ctl_elem_value *value)
377{
378 struct oxygen *chip = ctl->private_data;
379 u32 new_bits;
380 int changed;
381
382 new_bits = iec958_to_oxygen(value);
383 mutex_lock(&chip->mutex);
384 changed = new_bits != chip->spdif_pcm_bits;
385 if (changed) {
386 chip->spdif_pcm_bits = new_bits;
387 if (chip->pcm_active & (1 << PCM_SPDIF))
388 write_spdif_bits(chip, new_bits);
389 }
390 mutex_unlock(&chip->mutex);
391 return changed;
392}
393
394static int spdif_input_mask_get(struct snd_kcontrol *ctl,
395 struct snd_ctl_elem_value *value)
396{
397 value->value.iec958.status[0] = 0xff;
398 value->value.iec958.status[1] = 0xff;
399 value->value.iec958.status[2] = 0xff;
400 value->value.iec958.status[3] = 0xff;
401 return 0;
402}
403
404static int spdif_input_default_get(struct snd_kcontrol *ctl,
405 struct snd_ctl_elem_value *value)
406{
407 struct oxygen *chip = ctl->private_data;
408 u32 bits;
409
410 bits = oxygen_read32(chip, OXYGEN_SPDIF_INPUT_BITS);
411 value->value.iec958.status[0] = bits;
412 value->value.iec958.status[1] = bits >> 8;
413 value->value.iec958.status[2] = bits >> 16;
414 value->value.iec958.status[3] = bits >> 24;
415 return 0;
416}
417
Clemens Ladisch02f21c92008-01-22 08:36:03 +0100418static int spdif_loopback_get(struct snd_kcontrol *ctl,
419 struct snd_ctl_elem_value *value)
420{
421 struct oxygen *chip = ctl->private_data;
422
423 value->value.integer.value[0] =
424 !!(oxygen_read32(chip, OXYGEN_SPDIF_CONTROL)
425 & OXYGEN_SPDIF_LOOPBACK);
426 return 0;
427}
428
429static int spdif_loopback_put(struct snd_kcontrol *ctl,
430 struct snd_ctl_elem_value *value)
431{
432 struct oxygen *chip = ctl->private_data;
433 u32 oldreg, newreg;
434 int changed;
435
436 spin_lock_irq(&chip->reg_lock);
437 oldreg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
438 if (value->value.integer.value[0])
439 newreg = oldreg | OXYGEN_SPDIF_LOOPBACK;
440 else
441 newreg = oldreg & ~OXYGEN_SPDIF_LOOPBACK;
442 changed = newreg != oldreg;
443 if (changed)
444 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, newreg);
445 spin_unlock_irq(&chip->reg_lock);
446 return changed;
447}
448
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100449static int monitor_volume_info(struct snd_kcontrol *ctl,
450 struct snd_ctl_elem_info *info)
451{
452 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
453 info->count = 1;
454 info->value.integer.min = 0;
455 info->value.integer.max = 1;
456 return 0;
457}
458
459static int monitor_get(struct snd_kcontrol *ctl,
460 struct snd_ctl_elem_value *value)
461{
462 struct oxygen *chip = ctl->private_data;
463 u8 bit = ctl->private_value;
464 int invert = ctl->private_value & (1 << 8);
465
466 value->value.integer.value[0] =
467 !!invert ^ !!(oxygen_read8(chip, OXYGEN_ADC_MONITOR) & bit);
468 return 0;
469}
470
471static int monitor_put(struct snd_kcontrol *ctl,
472 struct snd_ctl_elem_value *value)
473{
474 struct oxygen *chip = ctl->private_data;
475 u8 bit = ctl->private_value;
476 int invert = ctl->private_value & (1 << 8);
477 u8 oldreg, newreg;
478 int changed;
479
480 spin_lock_irq(&chip->reg_lock);
481 oldreg = oxygen_read8(chip, OXYGEN_ADC_MONITOR);
482 if ((!!value->value.integer.value[0] ^ !!invert) != 0)
483 newreg = oldreg | bit;
484 else
485 newreg = oldreg & ~bit;
486 changed = newreg != oldreg;
487 if (changed)
488 oxygen_write8(chip, OXYGEN_ADC_MONITOR, newreg);
489 spin_unlock_irq(&chip->reg_lock);
490 return changed;
491}
492
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100493static int ac97_switch_get(struct snd_kcontrol *ctl,
494 struct snd_ctl_elem_value *value)
495{
496 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100497 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100498 unsigned int index = ctl->private_value & 0xff;
499 unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
500 int invert = ctl->private_value & (1 << 16);
501 u16 reg;
502
503 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100504 reg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100505 mutex_unlock(&chip->mutex);
506 if (!(reg & (1 << bitnr)) ^ !invert)
507 value->value.integer.value[0] = 1;
508 else
509 value->value.integer.value[0] = 0;
510 return 0;
511}
512
Clemens Ladische97f7992008-04-01 10:02:18 +0200513static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
514{
Clemens Ladisch3d839e52008-08-26 11:06:26 +0200515 unsigned int priv_idx;
Clemens Ladische97f7992008-04-01 10:02:18 +0200516 u16 value;
517
Clemens Ladisch3d839e52008-08-26 11:06:26 +0200518 if (!chip->controls[control])
519 return;
520 priv_idx = chip->controls[control]->private_value & 0xff;
Clemens Ladische97f7992008-04-01 10:02:18 +0200521 value = oxygen_read_ac97(chip, 0, priv_idx);
522 if (!(value & 0x8000)) {
523 oxygen_write_ac97(chip, 0, priv_idx, value | 0x8000);
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200524 if (chip->model.ac97_switch)
525 chip->model.ac97_switch(chip, priv_idx, 0x8000);
Clemens Ladische97f7992008-04-01 10:02:18 +0200526 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
527 &chip->controls[control]->id);
528 }
529}
530
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100531static int ac97_switch_put(struct snd_kcontrol *ctl,
532 struct snd_ctl_elem_value *value)
533{
534 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100535 unsigned int codec = (ctl->private_value >> 24) & 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100536 unsigned int index = ctl->private_value & 0xff;
537 unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
538 int invert = ctl->private_value & (1 << 16);
539 u16 oldreg, newreg;
540 int change;
541
542 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100543 oldreg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100544 newreg = oldreg;
545 if (!value->value.integer.value[0] ^ !invert)
546 newreg |= 1 << bitnr;
547 else
548 newreg &= ~(1 << bitnr);
549 change = newreg != oldreg;
550 if (change) {
Clemens Ladischa3601562008-01-28 08:35:20 +0100551 oxygen_write_ac97(chip, codec, index, newreg);
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200552 if (codec == 0 && chip->model.ac97_switch)
553 chip->model.ac97_switch(chip, index, newreg & 0x8000);
Clemens Ladische97f7992008-04-01 10:02:18 +0200554 if (index == AC97_LINE) {
555 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
556 newreg & 0x8000 ?
557 CM9780_GPO0 : 0, CM9780_GPO0);
558 if (!(newreg & 0x8000)) {
559 mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
560 mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
561 mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
562 }
563 } else if ((index == AC97_MIC || index == AC97_CD ||
564 index == AC97_VIDEO || index == AC97_AUX) &&
565 bitnr == 15 && !(newreg & 0x8000)) {
566 mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
567 oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
568 CM9780_GPO0, CM9780_GPO0);
569 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100570 }
571 mutex_unlock(&chip->mutex);
572 return change;
573}
574
575static int ac97_volume_info(struct snd_kcontrol *ctl,
576 struct snd_ctl_elem_info *info)
577{
578 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
579 info->count = 2;
580 info->value.integer.min = 0;
581 info->value.integer.max = 0x1f;
582 return 0;
583}
584
585static int ac97_volume_get(struct snd_kcontrol *ctl,
586 struct snd_ctl_elem_value *value)
587{
588 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100589 unsigned int codec = (ctl->private_value >> 24) & 1;
590 unsigned int index = ctl->private_value & 0xff;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100591 u16 reg;
592
593 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100594 reg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100595 mutex_unlock(&chip->mutex);
596 value->value.integer.value[0] = 31 - (reg & 0x1f);
597 value->value.integer.value[1] = 31 - ((reg >> 8) & 0x1f);
598 return 0;
599}
600
601static int ac97_volume_put(struct snd_kcontrol *ctl,
602 struct snd_ctl_elem_value *value)
603{
604 struct oxygen *chip = ctl->private_data;
Clemens Ladischa3601562008-01-28 08:35:20 +0100605 unsigned int codec = (ctl->private_value >> 24) & 1;
606 unsigned int index = ctl->private_value & 0xff;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100607 u16 oldreg, newreg;
608 int change;
609
610 mutex_lock(&chip->mutex);
Clemens Ladischa3601562008-01-28 08:35:20 +0100611 oldreg = oxygen_read_ac97(chip, codec, index);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100612 newreg = oldreg;
613 newreg = (newreg & ~0x1f) |
614 (31 - (value->value.integer.value[0] & 0x1f));
615 newreg = (newreg & ~0x1f00) |
616 ((31 - (value->value.integer.value[0] & 0x1f)) << 8);
617 change = newreg != oldreg;
618 if (change)
Clemens Ladischa3601562008-01-28 08:35:20 +0100619 oxygen_write_ac97(chip, codec, index, newreg);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100620 mutex_unlock(&chip->mutex);
621 return change;
622}
623
Clemens Ladischa3601562008-01-28 08:35:20 +0100624static int ac97_fp_rec_volume_info(struct snd_kcontrol *ctl,
625 struct snd_ctl_elem_info *info)
626{
627 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
628 info->count = 2;
629 info->value.integer.min = 0;
630 info->value.integer.max = 7;
631 return 0;
632}
633
634static int ac97_fp_rec_volume_get(struct snd_kcontrol *ctl,
635 struct snd_ctl_elem_value *value)
636{
637 struct oxygen *chip = ctl->private_data;
638 u16 reg;
639
640 mutex_lock(&chip->mutex);
641 reg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
642 mutex_unlock(&chip->mutex);
643 value->value.integer.value[0] = reg & 7;
644 value->value.integer.value[1] = (reg >> 8) & 7;
645 return 0;
646}
647
648static int ac97_fp_rec_volume_put(struct snd_kcontrol *ctl,
649 struct snd_ctl_elem_value *value)
650{
651 struct oxygen *chip = ctl->private_data;
652 u16 oldreg, newreg;
653 int change;
654
655 mutex_lock(&chip->mutex);
656 oldreg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
657 newreg = oldreg & ~0x0707;
658 newreg = newreg | (value->value.integer.value[0] & 7);
659 newreg = newreg | ((value->value.integer.value[0] & 7) << 8);
660 change = newreg != oldreg;
661 if (change)
662 oxygen_write_ac97(chip, 1, AC97_REC_GAIN, newreg);
663 mutex_unlock(&chip->mutex);
664 return change;
665}
666
667#define AC97_SWITCH(xname, codec, index, bitnr, invert) { \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100668 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
669 .name = xname, \
670 .info = snd_ctl_boolean_mono_info, \
671 .get = ac97_switch_get, \
672 .put = ac97_switch_put, \
Clemens Ladischa3601562008-01-28 08:35:20 +0100673 .private_value = ((codec) << 24) | ((invert) << 16) | \
674 ((bitnr) << 8) | (index), \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100675 }
Clemens Ladischa3601562008-01-28 08:35:20 +0100676#define AC97_VOLUME(xname, codec, index) { \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100677 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
678 .name = xname, \
Clemens Ladischa3601562008-01-28 08:35:20 +0100679 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
680 SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100681 .info = ac97_volume_info, \
682 .get = ac97_volume_get, \
683 .put = ac97_volume_put, \
684 .tlv = { .p = ac97_db_scale, }, \
Clemens Ladischa3601562008-01-28 08:35:20 +0100685 .private_value = ((codec) << 24) | (index), \
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100686 }
687
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100688static DECLARE_TLV_DB_SCALE(monitor_db_scale, -1000, 1000, 0);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100689static DECLARE_TLV_DB_SCALE(ac97_db_scale, -3450, 150, 0);
Clemens Ladischa3601562008-01-28 08:35:20 +0100690static DECLARE_TLV_DB_SCALE(ac97_rec_db_scale, 0, 150, 0);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100691
692static const struct snd_kcontrol_new controls[] = {
693 {
694 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladischfb920b72008-01-15 08:39:06 +0100695 .name = "Master Playback Volume",
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100696 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100697 .info = dac_volume_info,
698 .get = dac_volume_get,
699 .put = dac_volume_put,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100700 },
701 {
702 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Clemens Ladischfb920b72008-01-15 08:39:06 +0100703 .name = "Master Playback Switch",
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100704 .info = snd_ctl_boolean_mono_info,
705 .get = dac_mute_get,
706 .put = dac_mute_put,
707 },
708 {
709 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
710 .name = "Stereo Upmixing",
711 .info = upmix_info,
712 .get = upmix_get,
713 .put = upmix_put,
714 },
715 {
716 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
717 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
718 .info = snd_ctl_boolean_mono_info,
719 .get = spdif_switch_get,
720 .put = spdif_switch_put,
721 },
722 {
723 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
724 .device = 1,
725 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
726 .info = spdif_info,
727 .get = spdif_default_get,
728 .put = spdif_default_put,
729 },
730 {
731 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
732 .device = 1,
733 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
734 .access = SNDRV_CTL_ELEM_ACCESS_READ,
735 .info = spdif_info,
736 .get = spdif_mask_get,
737 },
738 {
739 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
740 .device = 1,
741 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
742 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
743 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
744 .info = spdif_info,
745 .get = spdif_pcm_get,
746 .put = spdif_pcm_put,
747 },
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +0200748};
749
750static const struct snd_kcontrol_new spdif_input_controls[] = {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100751 {
752 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
753 .device = 1,
754 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK),
755 .access = SNDRV_CTL_ELEM_ACCESS_READ,
756 .info = spdif_info,
757 .get = spdif_input_mask_get,
758 },
759 {
760 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
761 .device = 1,
762 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
763 .access = SNDRV_CTL_ELEM_ACCESS_READ,
764 .info = spdif_info,
765 .get = spdif_input_default_get,
766 },
Clemens Ladisch02f21c92008-01-22 08:36:03 +0100767 {
768 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
769 .name = SNDRV_CTL_NAME_IEC958("Loopback ", NONE, SWITCH),
770 .info = snd_ctl_boolean_mono_info,
771 .get = spdif_loopback_get,
772 .put = spdif_loopback_put,
773 },
Clemens Ladisch31c77642008-01-16 08:28:17 +0100774};
775
Clemens Ladischf009ad92008-03-19 08:19:41 +0100776static const struct {
777 unsigned int pcm_dev;
778 struct snd_kcontrol_new controls[2];
779} monitor_controls[] = {
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100780 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100781 .pcm_dev = CAPTURE_0_FROM_I2S_1,
782 .controls = {
783 {
784 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
785 .name = "Analog Input Monitor Switch",
786 .info = snd_ctl_boolean_mono_info,
787 .get = monitor_get,
788 .put = monitor_put,
789 .private_value = OXYGEN_ADC_MONITOR_A,
790 },
791 {
792 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
793 .name = "Analog Input Monitor Volume",
794 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
795 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
796 .info = monitor_volume_info,
797 .get = monitor_get,
798 .put = monitor_put,
799 .private_value = OXYGEN_ADC_MONITOR_A_HALF_VOL
800 | (1 << 8),
801 .tlv = { .p = monitor_db_scale, },
802 },
803 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100804 },
805 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100806 .pcm_dev = CAPTURE_0_FROM_I2S_2,
807 .controls = {
808 {
809 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
810 .name = "Analog Input Monitor Switch",
811 .info = snd_ctl_boolean_mono_info,
812 .get = monitor_get,
813 .put = monitor_put,
814 .private_value = OXYGEN_ADC_MONITOR_B,
815 },
816 {
817 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
818 .name = "Analog Input Monitor Volume",
819 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
820 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
821 .info = monitor_volume_info,
822 .get = monitor_get,
823 .put = monitor_put,
824 .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
825 | (1 << 8),
826 .tlv = { .p = monitor_db_scale, },
827 },
828 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100829 },
830 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100831 .pcm_dev = CAPTURE_2_FROM_I2S_2,
832 .controls = {
833 {
834 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
835 .name = "Analog Input Monitor Switch",
836 .index = 1,
837 .info = snd_ctl_boolean_mono_info,
838 .get = monitor_get,
839 .put = monitor_put,
840 .private_value = OXYGEN_ADC_MONITOR_B,
841 },
842 {
843 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
844 .name = "Analog Input Monitor Volume",
845 .index = 1,
846 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
847 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
848 .info = monitor_volume_info,
849 .get = monitor_get,
850 .put = monitor_put,
851 .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
852 | (1 << 8),
853 .tlv = { .p = monitor_db_scale, },
854 },
855 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100856 },
857 {
Clemens Ladischf009ad92008-03-19 08:19:41 +0100858 .pcm_dev = CAPTURE_1_FROM_SPDIF,
859 .controls = {
860 {
861 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
862 .name = "Digital Input Monitor Switch",
863 .info = snd_ctl_boolean_mono_info,
864 .get = monitor_get,
865 .put = monitor_put,
866 .private_value = OXYGEN_ADC_MONITOR_C,
867 },
868 {
869 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
870 .name = "Digital Input Monitor Volume",
871 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
872 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
873 .info = monitor_volume_info,
874 .get = monitor_get,
875 .put = monitor_put,
876 .private_value = OXYGEN_ADC_MONITOR_C_HALF_VOL
877 | (1 << 8),
878 .tlv = { .p = monitor_db_scale, },
879 },
880 },
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100881 },
882};
883
Clemens Ladisch31c77642008-01-16 08:28:17 +0100884static const struct snd_kcontrol_new ac97_controls[] = {
Clemens Ladischa3601562008-01-28 08:35:20 +0100885 AC97_VOLUME("Mic Capture Volume", 0, AC97_MIC),
886 AC97_SWITCH("Mic Capture Switch", 0, AC97_MIC, 15, 1),
887 AC97_SWITCH("Mic Boost (+20dB)", 0, AC97_MIC, 6, 0),
Clemens Ladischa3601562008-01-28 08:35:20 +0100888 AC97_SWITCH("Line Capture Switch", 0, AC97_LINE, 15, 1),
889 AC97_VOLUME("CD Capture Volume", 0, AC97_CD),
890 AC97_SWITCH("CD Capture Switch", 0, AC97_CD, 15, 1),
891 AC97_VOLUME("Aux Capture Volume", 0, AC97_AUX),
892 AC97_SWITCH("Aux Capture Switch", 0, AC97_AUX, 15, 1),
893};
894
895static const struct snd_kcontrol_new ac97_fp_controls[] = {
896 AC97_VOLUME("Front Panel Playback Volume", 1, AC97_HEADPHONE),
897 AC97_SWITCH("Front Panel Playback Switch", 1, AC97_HEADPHONE, 15, 1),
898 {
899 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
900 .name = "Front Panel Capture Volume",
901 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
902 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
903 .info = ac97_fp_rec_volume_info,
904 .get = ac97_fp_rec_volume_get,
905 .put = ac97_fp_rec_volume_put,
906 .tlv = { .p = ac97_rec_db_scale, },
907 },
908 AC97_SWITCH("Front Panel Capture Switch", 1, AC97_REC_GAIN, 15, 1),
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100909};
910
911static void oxygen_any_ctl_free(struct snd_kcontrol *ctl)
912{
913 struct oxygen *chip = ctl->private_data;
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100914 unsigned int i;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100915
916 /* I'm too lazy to write a function for each control :-) */
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100917 for (i = 0; i < ARRAY_SIZE(chip->controls); ++i)
918 chip->controls[i] = NULL;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100919}
920
Clemens Ladisch31c77642008-01-16 08:28:17 +0100921static int add_controls(struct oxygen *chip,
922 const struct snd_kcontrol_new controls[],
923 unsigned int count)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100924{
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100925 static const char *const known_ctl_names[CONTROL_COUNT] = {
926 [CONTROL_SPDIF_PCM] =
927 SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
928 [CONTROL_SPDIF_INPUT_BITS] =
929 SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Clemens Ladisch893e44b2008-01-14 08:57:05 +0100930 [CONTROL_MIC_CAPTURE_SWITCH] = "Mic Capture Switch",
931 [CONTROL_LINE_CAPTURE_SWITCH] = "Line Capture Switch",
932 [CONTROL_CD_CAPTURE_SWITCH] = "CD Capture Switch",
933 [CONTROL_AUX_CAPTURE_SWITCH] = "Aux Capture Switch",
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100934 };
935 unsigned int i, j;
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100936 struct snd_kcontrol_new template;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100937 struct snd_kcontrol *ctl;
938 int err;
939
Clemens Ladisch31c77642008-01-16 08:28:17 +0100940 for (i = 0; i < count; ++i) {
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100941 template = controls[i];
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200942 if (chip->model.control_filter) {
943 err = chip->model.control_filter(&template);
Clemens Ladisch9f9115d2008-06-16 14:13:52 +0200944 if (err < 0)
945 return err;
946 if (err == 1)
947 continue;
948 }
Clemens Ladisch4972a172008-04-16 09:15:45 +0200949 if (!strcmp(template.name, "Master Playback Volume") &&
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200950 chip->model.dac_tlv) {
951 template.tlv.p = chip->model.dac_tlv;
Clemens Ladisch4972a172008-04-16 09:15:45 +0200952 template.access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
953 }
Clemens Ladische9d88a82008-01-21 08:52:11 +0100954 ctl = snd_ctl_new1(&template, chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100955 if (!ctl)
956 return -ENOMEM;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100957 err = snd_ctl_add(chip->card, ctl);
958 if (err < 0)
959 return err;
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100960 for (j = 0; j < CONTROL_COUNT; ++j)
961 if (!strcmp(ctl->id.name, known_ctl_names[j])) {
962 chip->controls[j] = ctl;
963 ctl->private_free = oxygen_any_ctl_free;
964 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100965 }
Clemens Ladisch31c77642008-01-16 08:28:17 +0100966 return 0;
967}
968
969int oxygen_mixer_init(struct oxygen *chip)
970{
Clemens Ladischf009ad92008-03-19 08:19:41 +0100971 unsigned int i;
Clemens Ladisch31c77642008-01-16 08:28:17 +0100972 int err;
973
974 err = add_controls(chip, controls, ARRAY_SIZE(controls));
975 if (err < 0)
976 return err;
Clemens Ladischd76596b2008-09-22 09:02:08 +0200977 if (chip->model.device_config & CAPTURE_1_FROM_SPDIF) {
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +0200978 err = add_controls(chip, spdif_input_controls,
979 ARRAY_SIZE(spdif_input_controls));
980 if (err < 0)
981 return err;
982 }
Clemens Ladischf009ad92008-03-19 08:19:41 +0100983 for (i = 0; i < ARRAY_SIZE(monitor_controls); ++i) {
Clemens Ladischd76596b2008-09-22 09:02:08 +0200984 if (!(chip->model.device_config & monitor_controls[i].pcm_dev))
Clemens Ladischf009ad92008-03-19 08:19:41 +0100985 continue;
986 err = add_controls(chip, monitor_controls[i].controls,
987 ARRAY_SIZE(monitor_controls[i].controls));
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100988 if (err < 0)
989 return err;
990 }
Clemens Ladisch31c77642008-01-16 08:28:17 +0100991 if (chip->has_ac97_0) {
992 err = add_controls(chip, ac97_controls,
993 ARRAY_SIZE(ac97_controls));
994 if (err < 0)
995 return err;
996 }
Clemens Ladischa3601562008-01-28 08:35:20 +0100997 if (chip->has_ac97_1) {
998 err = add_controls(chip, ac97_fp_controls,
999 ARRAY_SIZE(ac97_fp_controls));
1000 if (err < 0)
1001 return err;
1002 }
Clemens Ladisch9bd6a732008-09-22 08:55:19 +02001003 return chip->model.mixer_init ? chip->model.mixer_init(chip) : 0;
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001004}