xref: /openbmc/linux/sound/pci/oxygen/oxygen_mixer.c (revision a09d2831)
1 /*
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 
20 #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"
26 #include "cm9780.h"
27 
28 static int dac_volume_info(struct snd_kcontrol *ctl,
29 			   struct snd_ctl_elem_info *info)
30 {
31 	struct oxygen *chip = ctl->private_data;
32 
33 	info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
34 	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;
37 	return 0;
38 }
39 
40 static 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);
47 	for (i = 0; i < chip->model.dac_channels; ++i)
48 		value->value.integer.value[i] = chip->dac_volume[i];
49 	mutex_unlock(&chip->mutex);
50 	return 0;
51 }
52 
53 static 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);
62 	for (i = 0; i < chip->model.dac_channels; ++i)
63 		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)
68 		chip->model.update_dac_volume(chip);
69 	mutex_unlock(&chip->mutex);
70 	return changed;
71 }
72 
73 static 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 
84 static 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];
94 		chip->model.update_dac_mute(chip);
95 	}
96 	mutex_unlock(&chip->mutex);
97 	return changed;
98 }
99 
100 static int upmix_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
101 {
102 	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",
108 	};
109 	struct oxygen *chip = ctl->private_data;
110 	unsigned int count = chip->model.update_center_lfe_mix ? 5 : 3;
111 
112 	info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
113 	info->count = 1;
114 	info->value.enumerated.items = count;
115 	if (info->value.enumerated.item >= count)
116 		info->value.enumerated.item = count - 1;
117 	strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
118 	return 0;
119 }
120 
121 static 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 
131 void oxygen_update_dac_routing(struct oxygen *chip)
132 {
133 	/* DAC 0: front, DAC 1: surround, DAC 2: center/LFE, DAC 3: back */
134 	static const unsigned int reg_values[5] = {
135 		/* 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),
150 		/* 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),
160 	};
161 	u8 channels;
162 	unsigned int reg_value;
163 
164 	channels = oxygen_read8(chip, OXYGEN_PLAY_CHANNELS) &
165 		OXYGEN_PLAY_CHANNELS_MASK;
166 	if (channels == OXYGEN_PLAY_CHANNELS_2)
167 		reg_value = reg_values[chip->dac_routing];
168 	else if (channels == OXYGEN_PLAY_CHANNELS_8)
169 		/* 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);
174 	else
175 		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);
184 	if (chip->model.update_center_lfe_mix)
185 		chip->model.update_center_lfe_mix(chip, chip->dac_routing > 2);
186 }
187 
188 static int upmix_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
189 {
190 	struct oxygen *chip = ctl->private_data;
191 	unsigned int count = chip->model.update_center_lfe_mix ? 5 : 3;
192 	int changed;
193 
194 	if (value->value.enumerated.item[0] >= count)
195 		return -EINVAL;
196 	mutex_lock(&chip->mutex);
197 	changed = value->value.enumerated.item[0] != chip->dac_routing;
198 	if (changed) {
199 		chip->dac_routing = value->value.enumerated.item[0];
200 		oxygen_update_dac_routing(chip);
201 	}
202 	mutex_unlock(&chip->mutex);
203 	return changed;
204 }
205 
206 static 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 
217 static 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:
229 		return IEC958_AES3_CON_FS_88200 << OXYGEN_SPDIF_CS_RATE_SHIFT;
230 	case OXYGEN_RATE_96000:
231 		return IEC958_AES3_CON_FS_96000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
232 	case OXYGEN_RATE_176400:
233 		return IEC958_AES3_CON_FS_176400 << OXYGEN_SPDIF_CS_RATE_SHIFT;
234 	case OXYGEN_RATE_192000:
235 		return IEC958_AES3_CON_FS_192000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
236 	}
237 }
238 
239 void 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;
249 		new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
250 			| OXYGEN_PLAY_SPDIF_SPDIF;
251 		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) {
256 		new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
257 			| OXYGEN_PLAY_SPDIF_MULTICH_01;
258 		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 
281 static 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 
299 static 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 
306 static 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 
315 static 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 
328 static 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 
339 static 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 
350 static 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 
369 static 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 
379 static 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 
390 static 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 
409 static 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 
419 static 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 
433 static 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 
444 static 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 
464 static 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 
474 static 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 
486 static 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 
508 static int ac97_switch_get(struct snd_kcontrol *ctl,
509 			   struct snd_ctl_elem_value *value)
510 {
511 	struct oxygen *chip = ctl->private_data;
512 	unsigned int codec = (ctl->private_value >> 24) & 1;
513 	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);
519 	reg = oxygen_read_ac97(chip, codec, index);
520 	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 
528 static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
529 {
530 	unsigned int priv_idx;
531 	u16 value;
532 
533 	if (!chip->controls[control])
534 		return;
535 	priv_idx = chip->controls[control]->private_value & 0xff;
536 	value = oxygen_read_ac97(chip, 0, priv_idx);
537 	if (!(value & 0x8000)) {
538 		oxygen_write_ac97(chip, 0, priv_idx, value | 0x8000);
539 		if (chip->model.ac97_switch)
540 			chip->model.ac97_switch(chip, priv_idx, 0x8000);
541 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
542 			       &chip->controls[control]->id);
543 	}
544 }
545 
546 static int ac97_switch_put(struct snd_kcontrol *ctl,
547 			   struct snd_ctl_elem_value *value)
548 {
549 	struct oxygen *chip = ctl->private_data;
550 	unsigned int codec = (ctl->private_value >> 24) & 1;
551 	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);
558 	oldreg = oxygen_read_ac97(chip, codec, index);
559 	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) {
566 		oxygen_write_ac97(chip, codec, index, newreg);
567 		if (codec == 0 && chip->model.ac97_switch)
568 			chip->model.ac97_switch(chip, index, newreg & 0x8000);
569 		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 		}
585 	}
586 	mutex_unlock(&chip->mutex);
587 	return change;
588 }
589 
590 static int ac97_volume_info(struct snd_kcontrol *ctl,
591 			    struct snd_ctl_elem_info *info)
592 {
593 	int stereo = (ctl->private_value >> 16) & 1;
594 
595 	info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
596 	info->count = stereo ? 2 : 1;
597 	info->value.integer.min = 0;
598 	info->value.integer.max = 0x1f;
599 	return 0;
600 }
601 
602 static int ac97_volume_get(struct snd_kcontrol *ctl,
603 			   struct snd_ctl_elem_value *value)
604 {
605 	struct oxygen *chip = ctl->private_data;
606 	unsigned int codec = (ctl->private_value >> 24) & 1;
607 	int stereo = (ctl->private_value >> 16) & 1;
608 	unsigned int index = ctl->private_value & 0xff;
609 	u16 reg;
610 
611 	mutex_lock(&chip->mutex);
612 	reg = oxygen_read_ac97(chip, codec, index);
613 	mutex_unlock(&chip->mutex);
614 	value->value.integer.value[0] = 31 - (reg & 0x1f);
615 	if (stereo)
616 		value->value.integer.value[1] = 31 - ((reg >> 8) & 0x1f);
617 	return 0;
618 }
619 
620 static int ac97_volume_put(struct snd_kcontrol *ctl,
621 			   struct snd_ctl_elem_value *value)
622 {
623 	struct oxygen *chip = ctl->private_data;
624 	unsigned int codec = (ctl->private_value >> 24) & 1;
625 	int stereo = (ctl->private_value >> 16) & 1;
626 	unsigned int index = ctl->private_value & 0xff;
627 	u16 oldreg, newreg;
628 	int change;
629 
630 	mutex_lock(&chip->mutex);
631 	oldreg = oxygen_read_ac97(chip, codec, index);
632 	newreg = oldreg;
633 	newreg = (newreg & ~0x1f) |
634 		(31 - (value->value.integer.value[0] & 0x1f));
635 	if (stereo)
636 		newreg = (newreg & ~0x1f00) |
637 			((31 - (value->value.integer.value[1] & 0x1f)) << 8);
638 	else
639 		newreg = (newreg & ~0x1f00) | ((newreg & 0x1f) << 8);
640 	change = newreg != oldreg;
641 	if (change)
642 		oxygen_write_ac97(chip, codec, index, newreg);
643 	mutex_unlock(&chip->mutex);
644 	return change;
645 }
646 
647 static 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 
657 static 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 
671 static 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) { \
691 		.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, \
696 		.private_value = ((codec) << 24) | ((invert) << 16) | \
697 				 ((bitnr) << 8) | (index), \
698 	}
699 #define AC97_VOLUME(xname, codec, index, stereo) { \
700 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
701 		.name = xname, \
702 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
703 			  SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
704 		.info = ac97_volume_info, \
705 		.get = ac97_volume_get, \
706 		.put = ac97_volume_put, \
707 		.tlv = { .p = ac97_db_scale, }, \
708 		.private_value = ((codec) << 24) | ((stereo) << 16) | (index), \
709 	}
710 
711 static DECLARE_TLV_DB_SCALE(monitor_db_scale, -1000, 1000, 0);
712 static DECLARE_TLV_DB_SCALE(ac97_db_scale, -3450, 150, 0);
713 static DECLARE_TLV_DB_SCALE(ac97_rec_db_scale, 0, 150, 0);
714 
715 static const struct snd_kcontrol_new controls[] = {
716 	{
717 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
718 		.name = "Master Playback Volume",
719 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
720 		.info = dac_volume_info,
721 		.get = dac_volume_get,
722 		.put = dac_volume_put,
723 	},
724 	{
725 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
726 		.name = "Master Playback Switch",
727 		.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 	},
771 };
772 
773 static const struct snd_kcontrol_new spdif_input_controls[] = {
774 	{
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 	},
790 	{
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 	},
797 };
798 
799 static const struct {
800 	unsigned int pcm_dev;
801 	struct snd_kcontrol_new controls[2];
802 } monitor_controls[] = {
803 	{
804 		.pcm_dev = CAPTURE_0_FROM_I2S_1,
805 		.controls = {
806 			{
807 				.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
808 				.name = "Analog Input Monitor Playback 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 Playback 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 		},
827 	},
828 	{
829 		.pcm_dev = CAPTURE_0_FROM_I2S_2,
830 		.controls = {
831 			{
832 				.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
833 				.name = "Analog Input Monitor Playback 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 Playback 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 		},
852 	},
853 	{
854 		.pcm_dev = CAPTURE_2_FROM_I2S_2,
855 		.controls = {
856 			{
857 				.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
858 				.name = "Analog Input Monitor Playback 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 Playback 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 		},
879 	},
880 	{
881 		.pcm_dev = CAPTURE_1_FROM_SPDIF,
882 		.controls = {
883 			{
884 				.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
885 				.name = "Digital Input Monitor Playback 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 Playback 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 		},
904 	},
905 };
906 
907 static const struct snd_kcontrol_new ac97_controls[] = {
908 	AC97_VOLUME("Mic Capture Volume", 0, AC97_MIC, 0),
909 	AC97_SWITCH("Mic Capture Switch", 0, AC97_MIC, 15, 1),
910 	AC97_SWITCH("Mic Boost (+20dB)", 0, AC97_MIC, 6, 0),
911 	AC97_SWITCH("Line Capture Switch", 0, AC97_LINE, 15, 1),
912 	AC97_VOLUME("CD Capture Volume", 0, AC97_CD, 1),
913 	AC97_SWITCH("CD Capture Switch", 0, AC97_CD, 15, 1),
914 	AC97_VOLUME("Aux Capture Volume", 0, AC97_AUX, 1),
915 	AC97_SWITCH("Aux Capture Switch", 0, AC97_AUX, 15, 1),
916 };
917 
918 static const struct snd_kcontrol_new ac97_fp_controls[] = {
919 	AC97_VOLUME("Front Panel Playback Volume", 1, AC97_HEADPHONE, 1),
920 	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),
932 };
933 
934 static void oxygen_any_ctl_free(struct snd_kcontrol *ctl)
935 {
936 	struct oxygen *chip = ctl->private_data;
937 	unsigned int i;
938 
939 	/* I'm too lazy to write a function for each control :-) */
940 	for (i = 0; i < ARRAY_SIZE(chip->controls); ++i)
941 		chip->controls[i] = NULL;
942 }
943 
944 static int add_controls(struct oxygen *chip,
945 			const struct snd_kcontrol_new controls[],
946 			unsigned int count)
947 {
948 	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),
953 		[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",
957 	};
958 	unsigned int i, j;
959 	struct snd_kcontrol_new template;
960 	struct snd_kcontrol *ctl;
961 	int err;
962 
963 	for (i = 0; i < count; ++i) {
964 		template = controls[i];
965 		if (chip->model.control_filter) {
966 			err = chip->model.control_filter(&template);
967 			if (err < 0)
968 				return err;
969 			if (err == 1)
970 				continue;
971 		}
972 		if (!strcmp(template.name, "Stereo Upmixing") &&
973 		    chip->model.dac_channels == 2)
974 			continue;
975 		if (!strcmp(template.name, "Master Playback Volume") &&
976 		    chip->model.dac_tlv) {
977 			template.tlv.p = chip->model.dac_tlv;
978 			template.access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
979 		}
980 		ctl = snd_ctl_new1(&template, chip);
981 		if (!ctl)
982 			return -ENOMEM;
983 		err = snd_ctl_add(chip->card, ctl);
984 		if (err < 0)
985 			return err;
986 		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 			}
991 	}
992 	return 0;
993 }
994 
995 int oxygen_mixer_init(struct oxygen *chip)
996 {
997 	unsigned int i;
998 	int err;
999 
1000 	err = add_controls(chip, controls, ARRAY_SIZE(controls));
1001 	if (err < 0)
1002 		return err;
1003 	if (chip->model.device_config & CAPTURE_1_FROM_SPDIF) {
1004 		err = add_controls(chip, spdif_input_controls,
1005 				   ARRAY_SIZE(spdif_input_controls));
1006 		if (err < 0)
1007 			return err;
1008 	}
1009 	for (i = 0; i < ARRAY_SIZE(monitor_controls); ++i) {
1010 		if (!(chip->model.device_config & monitor_controls[i].pcm_dev))
1011 			continue;
1012 		err = add_controls(chip, monitor_controls[i].controls,
1013 				   ARRAY_SIZE(monitor_controls[i].controls));
1014 		if (err < 0)
1015 			return err;
1016 	}
1017 	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 	}
1023 	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 	}
1029 	return chip->model.mixer_init ? chip->model.mixer_init(chip) : 0;
1030 }
1031