Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
oxygen_mixer.c
Go to the documentation of this file.
1 /*
2  * C-Media CMI8788 driver - mixer code
3  *
4  * Copyright (c) Clemens Ladisch <[email protected]>
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 
34  info->count = chip->model.dac_channels_mixer;
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_mixer; ++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_mixer; ++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 unsigned int upmix_item_count(struct oxygen *chip)
101 {
102  if (chip->model.dac_channels_pcm < 8)
103  return 2;
104  else if (chip->model.update_center_lfe_mix)
105  return 5;
106  else
107  return 3;
108 }
109 
110 static int upmix_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
111 {
112  static const char *const names[5] = {
113  "Front",
114  "Front+Surround",
115  "Front+Surround+Back",
116  "Front+Surround+Center/LFE",
117  "Front+Surround+Center/LFE+Back",
118  };
119  struct oxygen *chip = ctl->private_data;
120  unsigned int count = upmix_item_count(chip);
121 
122  return snd_ctl_enum_info(info, 1, count, names);
123 }
124 
125 static int upmix_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
126 {
127  struct oxygen *chip = ctl->private_data;
128 
129  mutex_lock(&chip->mutex);
130  value->value.enumerated.item[0] = chip->dac_routing;
131  mutex_unlock(&chip->mutex);
132  return 0;
133 }
134 
136 {
137  /* DAC 0: front, DAC 1: surround, DAC 2: center/LFE, DAC 3: back */
138  static const unsigned int reg_values[5] = {
139  /* stereo -> front */
144  /* stereo -> front+surround */
149  /* stereo -> front+surround+back */
154  /* stereo -> front+surround+center/LFE */
159  /* stereo -> front+surround+center/LFE+back */
164  };
165  u8 channels;
166  unsigned int reg_value;
167 
168  channels = oxygen_read8(chip, OXYGEN_PLAY_CHANNELS) &
170  if (channels == OXYGEN_PLAY_CHANNELS_2)
171  reg_value = reg_values[chip->dac_routing];
172  else if (channels == OXYGEN_PLAY_CHANNELS_8)
173  /* in 7.1 mode, "rear" channels go to the "back" jack */
174  reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
178  else
179  reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
183  if (chip->model.adjust_dac_routing)
184  reg_value = chip->model.adjust_dac_routing(chip, reg_value);
190  if (chip->model.update_center_lfe_mix)
191  chip->model.update_center_lfe_mix(chip, chip->dac_routing > 2);
192 }
193 
194 static int upmix_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
195 {
196  struct oxygen *chip = ctl->private_data;
197  unsigned int count = upmix_item_count(chip);
198  int changed;
199 
200  if (value->value.enumerated.item[0] >= count)
201  return -EINVAL;
202  mutex_lock(&chip->mutex);
203  changed = value->value.enumerated.item[0] != chip->dac_routing;
204  if (changed) {
205  chip->dac_routing = value->value.enumerated.item[0];
207  }
208  mutex_unlock(&chip->mutex);
209  return changed;
210 }
211 
212 static int spdif_switch_get(struct snd_kcontrol *ctl,
213  struct snd_ctl_elem_value *value)
214 {
215  struct oxygen *chip = ctl->private_data;
216 
217  mutex_lock(&chip->mutex);
218  value->value.integer.value[0] = chip->spdif_playback_enable;
219  mutex_unlock(&chip->mutex);
220  return 0;
221 }
222 
223 static unsigned int oxygen_spdif_rate(unsigned int oxygen_rate)
224 {
225  switch (oxygen_rate) {
226  case OXYGEN_RATE_32000:
228  case OXYGEN_RATE_44100:
230  default: /* OXYGEN_RATE_48000 */
232  case OXYGEN_RATE_64000:
233  return 0xb << OXYGEN_SPDIF_CS_RATE_SHIFT;
234  case OXYGEN_RATE_88200:
236  case OXYGEN_RATE_96000:
238  case OXYGEN_RATE_176400:
240  case OXYGEN_RATE_192000:
242  }
243 }
244 
246 {
247  u32 old_control, new_control;
248  u16 old_routing, new_routing;
249  unsigned int oxygen_rate;
250 
251  old_control = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
252  old_routing = oxygen_read16(chip, OXYGEN_PLAY_ROUTING);
253  if (chip->pcm_active & (1 << PCM_SPDIF)) {
254  new_control = old_control | OXYGEN_SPDIF_OUT_ENABLE;
255  new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
257  oxygen_rate = (old_control >> OXYGEN_SPDIF_OUT_RATE_SHIFT)
259  /* S/PDIF rate was already set by the caller */
260  } else if ((chip->pcm_active & (1 << PCM_MULTICH)) &&
261  chip->spdif_playback_enable) {
262  new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
264  oxygen_rate = oxygen_read16(chip, OXYGEN_I2S_MULTICH_FORMAT)
266  new_control = (old_control & ~OXYGEN_SPDIF_OUT_RATE_MASK) |
267  (oxygen_rate << OXYGEN_SPDIF_OUT_RATE_SHIFT) |
269  } else {
270  new_control = old_control & ~OXYGEN_SPDIF_OUT_ENABLE;
271  new_routing = old_routing;
272  oxygen_rate = OXYGEN_RATE_44100;
273  }
274  if (old_routing != new_routing) {
276  new_control & ~OXYGEN_SPDIF_OUT_ENABLE);
277  oxygen_write16(chip, OXYGEN_PLAY_ROUTING, new_routing);
278  }
279  if (new_control & OXYGEN_SPDIF_OUT_ENABLE)
281  oxygen_spdif_rate(oxygen_rate) |
282  ((chip->pcm_active & (1 << PCM_SPDIF)) ?
283  chip->spdif_pcm_bits : chip->spdif_bits));
284  oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, new_control);
285 }
286 
287 static int spdif_switch_put(struct snd_kcontrol *ctl,
288  struct snd_ctl_elem_value *value)
289 {
290  struct oxygen *chip = ctl->private_data;
291  int changed;
292 
293  mutex_lock(&chip->mutex);
294  changed = value->value.integer.value[0] != chip->spdif_playback_enable;
295  if (changed) {
296  chip->spdif_playback_enable = !!value->value.integer.value[0];
297  spin_lock_irq(&chip->reg_lock);
299  spin_unlock_irq(&chip->reg_lock);
300  }
301  mutex_unlock(&chip->mutex);
302  return changed;
303 }
304 
305 static int spdif_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
306 {
308  info->count = 1;
309  return 0;
310 }
311 
312 static void oxygen_to_iec958(u32 bits, struct snd_ctl_elem_value *value)
313 {
314  value->value.iec958.status[0] =
317  value->value.iec958.status[1] = /* category and original */
319 }
320 
321 static u32 iec958_to_oxygen(struct snd_ctl_elem_value *value)
322 {
323  u32 bits;
324 
325  bits = value->value.iec958.status[0] &
328  bits |= value->value.iec958.status[1] << OXYGEN_SPDIF_CATEGORY_SHIFT;
329  if (bits & OXYGEN_SPDIF_NONAUDIO)
330  bits |= OXYGEN_SPDIF_V;
331  return bits;
332 }
333 
334 static inline void write_spdif_bits(struct oxygen *chip, u32 bits)
335 {
343 }
344 
345 static int spdif_default_get(struct snd_kcontrol *ctl,
346  struct snd_ctl_elem_value *value)
347 {
348  struct oxygen *chip = ctl->private_data;
349 
350  mutex_lock(&chip->mutex);
351  oxygen_to_iec958(chip->spdif_bits, value);
352  mutex_unlock(&chip->mutex);
353  return 0;
354 }
355 
356 static int spdif_default_put(struct snd_kcontrol *ctl,
357  struct snd_ctl_elem_value *value)
358 {
359  struct oxygen *chip = ctl->private_data;
360  u32 new_bits;
361  int changed;
362 
363  new_bits = iec958_to_oxygen(value);
364  mutex_lock(&chip->mutex);
365  changed = new_bits != chip->spdif_bits;
366  if (changed) {
367  chip->spdif_bits = new_bits;
368  if (!(chip->pcm_active & (1 << PCM_SPDIF)))
369  write_spdif_bits(chip, new_bits);
370  }
371  mutex_unlock(&chip->mutex);
372  return changed;
373 }
374 
375 static int spdif_mask_get(struct snd_kcontrol *ctl,
376  struct snd_ctl_elem_value *value)
377 {
378  value->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
380  value->value.iec958.status[1] =
382  return 0;
383 }
384 
385 static int spdif_pcm_get(struct snd_kcontrol *ctl,
386  struct snd_ctl_elem_value *value)
387 {
388  struct oxygen *chip = ctl->private_data;
389 
390  mutex_lock(&chip->mutex);
391  oxygen_to_iec958(chip->spdif_pcm_bits, value);
392  mutex_unlock(&chip->mutex);
393  return 0;
394 }
395 
396 static int spdif_pcm_put(struct snd_kcontrol *ctl,
397  struct snd_ctl_elem_value *value)
398 {
399  struct oxygen *chip = ctl->private_data;
400  u32 new_bits;
401  int changed;
402 
403  new_bits = iec958_to_oxygen(value);
404  mutex_lock(&chip->mutex);
405  changed = new_bits != chip->spdif_pcm_bits;
406  if (changed) {
407  chip->spdif_pcm_bits = new_bits;
408  if (chip->pcm_active & (1 << PCM_SPDIF))
409  write_spdif_bits(chip, new_bits);
410  }
411  mutex_unlock(&chip->mutex);
412  return changed;
413 }
414 
415 static int spdif_input_mask_get(struct snd_kcontrol *ctl,
416  struct snd_ctl_elem_value *value)
417 {
418  value->value.iec958.status[0] = 0xff;
419  value->value.iec958.status[1] = 0xff;
420  value->value.iec958.status[2] = 0xff;
421  value->value.iec958.status[3] = 0xff;
422  return 0;
423 }
424 
425 static int spdif_input_default_get(struct snd_kcontrol *ctl,
426  struct snd_ctl_elem_value *value)
427 {
428  struct oxygen *chip = ctl->private_data;
429  u32 bits;
430 
432  value->value.iec958.status[0] = bits;
433  value->value.iec958.status[1] = bits >> 8;
434  value->value.iec958.status[2] = bits >> 16;
435  value->value.iec958.status[3] = bits >> 24;
436  return 0;
437 }
438 
439 static int spdif_bit_switch_get(struct snd_kcontrol *ctl,
440  struct snd_ctl_elem_value *value)
441 {
442  struct oxygen *chip = ctl->private_data;
443  u32 bit = ctl->private_value;
444 
445  value->value.integer.value[0] =
447  return 0;
448 }
449 
450 static int spdif_bit_switch_put(struct snd_kcontrol *ctl,
451  struct snd_ctl_elem_value *value)
452 {
453  struct oxygen *chip = ctl->private_data;
454  u32 bit = ctl->private_value;
455  u32 oldreg, newreg;
456  int changed;
457 
458  spin_lock_irq(&chip->reg_lock);
459  oldreg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
460  if (value->value.integer.value[0])
461  newreg = oldreg | bit;
462  else
463  newreg = oldreg & ~bit;
464  changed = newreg != oldreg;
465  if (changed)
466  oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, newreg);
467  spin_unlock_irq(&chip->reg_lock);
468  return changed;
469 }
470 
471 static int monitor_volume_info(struct snd_kcontrol *ctl,
472  struct snd_ctl_elem_info *info)
473 {
475  info->count = 1;
476  info->value.integer.min = 0;
477  info->value.integer.max = 1;
478  return 0;
479 }
480 
481 static int monitor_get(struct snd_kcontrol *ctl,
482  struct snd_ctl_elem_value *value)
483 {
484  struct oxygen *chip = ctl->private_data;
485  u8 bit = ctl->private_value;
486  int invert = ctl->private_value & (1 << 8);
487 
488  value->value.integer.value[0] =
489  !!invert ^ !!(oxygen_read8(chip, OXYGEN_ADC_MONITOR) & bit);
490  return 0;
491 }
492 
493 static int monitor_put(struct snd_kcontrol *ctl,
494  struct snd_ctl_elem_value *value)
495 {
496  struct oxygen *chip = ctl->private_data;
497  u8 bit = ctl->private_value;
498  int invert = ctl->private_value & (1 << 8);
499  u8 oldreg, newreg;
500  int changed;
501 
502  spin_lock_irq(&chip->reg_lock);
503  oldreg = oxygen_read8(chip, OXYGEN_ADC_MONITOR);
504  if ((!!value->value.integer.value[0] ^ !!invert) != 0)
505  newreg = oldreg | bit;
506  else
507  newreg = oldreg & ~bit;
508  changed = newreg != oldreg;
509  if (changed)
510  oxygen_write8(chip, OXYGEN_ADC_MONITOR, newreg);
511  spin_unlock_irq(&chip->reg_lock);
512  return changed;
513 }
514 
515 static int ac97_switch_get(struct snd_kcontrol *ctl,
516  struct snd_ctl_elem_value *value)
517 {
518  struct oxygen *chip = ctl->private_data;
519  unsigned int codec = (ctl->private_value >> 24) & 1;
520  unsigned int index = ctl->private_value & 0xff;
521  unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
522  int invert = ctl->private_value & (1 << 16);
523  u16 reg;
524 
525  mutex_lock(&chip->mutex);
526  reg = oxygen_read_ac97(chip, codec, index);
527  mutex_unlock(&chip->mutex);
528  if (!(reg & (1 << bitnr)) ^ !invert)
529  value->value.integer.value[0] = 1;
530  else
531  value->value.integer.value[0] = 0;
532  return 0;
533 }
534 
535 static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
536 {
537  unsigned int priv_idx;
538  u16 value;
539 
540  if (!chip->controls[control])
541  return;
542  priv_idx = chip->controls[control]->private_value & 0xff;
543  value = oxygen_read_ac97(chip, 0, priv_idx);
544  if (!(value & 0x8000)) {
545  oxygen_write_ac97(chip, 0, priv_idx, value | 0x8000);
546  if (chip->model.ac97_switch)
547  chip->model.ac97_switch(chip, priv_idx, 0x8000);
549  &chip->controls[control]->id);
550  }
551 }
552 
553 static int ac97_switch_put(struct snd_kcontrol *ctl,
554  struct snd_ctl_elem_value *value)
555 {
556  struct oxygen *chip = ctl->private_data;
557  unsigned int codec = (ctl->private_value >> 24) & 1;
558  unsigned int index = ctl->private_value & 0xff;
559  unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
560  int invert = ctl->private_value & (1 << 16);
561  u16 oldreg, newreg;
562  int change;
563 
564  mutex_lock(&chip->mutex);
565  oldreg = oxygen_read_ac97(chip, codec, index);
566  newreg = oldreg;
567  if (!value->value.integer.value[0] ^ !invert)
568  newreg |= 1 << bitnr;
569  else
570  newreg &= ~(1 << bitnr);
571  change = newreg != oldreg;
572  if (change) {
573  oxygen_write_ac97(chip, codec, index, newreg);
574  if (codec == 0 && chip->model.ac97_switch)
575  chip->model.ac97_switch(chip, index, newreg & 0x8000);
576  if (index == AC97_LINE) {
578  newreg & 0x8000 ?
579  CM9780_GPO0 : 0, CM9780_GPO0);
580  if (!(newreg & 0x8000)) {
581  mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
582  mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
583  mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
584  }
585  } else if ((index == AC97_MIC || index == AC97_CD ||
586  index == AC97_VIDEO || index == AC97_AUX) &&
587  bitnr == 15 && !(newreg & 0x8000)) {
588  mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
591  }
592  }
593  mutex_unlock(&chip->mutex);
594  return change;
595 }
596 
597 static int ac97_volume_info(struct snd_kcontrol *ctl,
598  struct snd_ctl_elem_info *info)
599 {
600  int stereo = (ctl->private_value >> 16) & 1;
601 
603  info->count = stereo ? 2 : 1;
604  info->value.integer.min = 0;
605  info->value.integer.max = 0x1f;
606  return 0;
607 }
608 
609 static int ac97_volume_get(struct snd_kcontrol *ctl,
610  struct snd_ctl_elem_value *value)
611 {
612  struct oxygen *chip = ctl->private_data;
613  unsigned int codec = (ctl->private_value >> 24) & 1;
614  int stereo = (ctl->private_value >> 16) & 1;
615  unsigned int index = ctl->private_value & 0xff;
616  u16 reg;
617 
618  mutex_lock(&chip->mutex);
619  reg = oxygen_read_ac97(chip, codec, index);
620  mutex_unlock(&chip->mutex);
621  if (!stereo) {
622  value->value.integer.value[0] = 31 - (reg & 0x1f);
623  } else {
624  value->value.integer.value[0] = 31 - ((reg >> 8) & 0x1f);
625  value->value.integer.value[1] = 31 - (reg & 0x1f);
626  }
627  return 0;
628 }
629 
630 static int ac97_volume_put(struct snd_kcontrol *ctl,
631  struct snd_ctl_elem_value *value)
632 {
633  struct oxygen *chip = ctl->private_data;
634  unsigned int codec = (ctl->private_value >> 24) & 1;
635  int stereo = (ctl->private_value >> 16) & 1;
636  unsigned int index = ctl->private_value & 0xff;
637  u16 oldreg, newreg;
638  int change;
639 
640  mutex_lock(&chip->mutex);
641  oldreg = oxygen_read_ac97(chip, codec, index);
642  if (!stereo) {
643  newreg = oldreg & ~0x1f;
644  newreg |= 31 - (value->value.integer.value[0] & 0x1f);
645  } else {
646  newreg = oldreg & ~0x1f1f;
647  newreg |= (31 - (value->value.integer.value[0] & 0x1f)) << 8;
648  newreg |= 31 - (value->value.integer.value[1] & 0x1f);
649  }
650  change = newreg != oldreg;
651  if (change)
652  oxygen_write_ac97(chip, codec, index, newreg);
653  mutex_unlock(&chip->mutex);
654  return change;
655 }
656 
657 static int mic_fmic_source_info(struct snd_kcontrol *ctl,
658  struct snd_ctl_elem_info *info)
659 {
660  static const char *const names[] = { "Mic Jack", "Front Panel" };
661 
662  return snd_ctl_enum_info(info, 1, 2, names);
663 }
664 
665 static int mic_fmic_source_get(struct snd_kcontrol *ctl,
666  struct snd_ctl_elem_value *value)
667 {
668  struct oxygen *chip = ctl->private_data;
669 
670  mutex_lock(&chip->mutex);
671  value->value.enumerated.item[0] =
673  mutex_unlock(&chip->mutex);
674  return 0;
675 }
676 
677 static int mic_fmic_source_put(struct snd_kcontrol *ctl,
678  struct snd_ctl_elem_value *value)
679 {
680  struct oxygen *chip = ctl->private_data;
681  u16 oldreg, newreg;
682  int change;
683 
684  mutex_lock(&chip->mutex);
685  oldreg = oxygen_read_ac97(chip, 0, CM9780_JACK);
686  if (value->value.enumerated.item[0])
687  newreg = oldreg | CM9780_FMIC2MIC;
688  else
689  newreg = oldreg & ~CM9780_FMIC2MIC;
690  change = newreg != oldreg;
691  if (change)
692  oxygen_write_ac97(chip, 0, CM9780_JACK, newreg);
693  mutex_unlock(&chip->mutex);
694  return change;
695 }
696 
697 static int ac97_fp_rec_volume_info(struct snd_kcontrol *ctl,
698  struct snd_ctl_elem_info *info)
699 {
701  info->count = 2;
702  info->value.integer.min = 0;
703  info->value.integer.max = 7;
704  return 0;
705 }
706 
707 static int ac97_fp_rec_volume_get(struct snd_kcontrol *ctl,
708  struct snd_ctl_elem_value *value)
709 {
710  struct oxygen *chip = ctl->private_data;
711  u16 reg;
712 
713  mutex_lock(&chip->mutex);
714  reg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
715  mutex_unlock(&chip->mutex);
716  value->value.integer.value[0] = reg & 7;
717  value->value.integer.value[1] = (reg >> 8) & 7;
718  return 0;
719 }
720 
721 static int ac97_fp_rec_volume_put(struct snd_kcontrol *ctl,
722  struct snd_ctl_elem_value *value)
723 {
724  struct oxygen *chip = ctl->private_data;
725  u16 oldreg, newreg;
726  int change;
727 
728  mutex_lock(&chip->mutex);
729  oldreg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
730  newreg = oldreg & ~0x0707;
731  newreg = newreg | (value->value.integer.value[0] & 7);
732  newreg = newreg | ((value->value.integer.value[0] & 7) << 8);
733  change = newreg != oldreg;
734  if (change)
735  oxygen_write_ac97(chip, 1, AC97_REC_GAIN, newreg);
736  mutex_unlock(&chip->mutex);
737  return change;
738 }
739 
740 #define AC97_SWITCH(xname, codec, index, bitnr, invert) { \
741  .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
742  .name = xname, \
743  .info = snd_ctl_boolean_mono_info, \
744  .get = ac97_switch_get, \
745  .put = ac97_switch_put, \
746  .private_value = ((codec) << 24) | ((invert) << 16) | \
747  ((bitnr) << 8) | (index), \
748  }
749 #define AC97_VOLUME(xname, codec, index, stereo) { \
750  .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
751  .name = xname, \
752  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
753  SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
754  .info = ac97_volume_info, \
755  .get = ac97_volume_get, \
756  .put = ac97_volume_put, \
757  .tlv = { .p = ac97_db_scale, }, \
758  .private_value = ((codec) << 24) | ((stereo) << 16) | (index), \
759  }
760 
761 static DECLARE_TLV_DB_SCALE(monitor_db_scale, -600, 600, 0);
762 static DECLARE_TLV_DB_SCALE(ac97_db_scale, -3450, 150, 0);
763 static DECLARE_TLV_DB_SCALE(ac97_rec_db_scale, 0, 150, 0);
764 
765 static const struct snd_kcontrol_new controls[] = {
766  {
768  .name = "Master Playback Volume",
770  .info = dac_volume_info,
771  .get = dac_volume_get,
772  .put = dac_volume_put,
773  },
774  {
776  .name = "Master Playback Switch",
778  .get = dac_mute_get,
779  .put = dac_mute_put,
780  },
781  {
783  .name = "Stereo Upmixing",
784  .info = upmix_info,
785  .get = upmix_get,
786  .put = upmix_put,
787  },
788  {
790  .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
792  .get = spdif_switch_get,
793  .put = spdif_switch_put,
794  },
795  {
796  .iface = SNDRV_CTL_ELEM_IFACE_PCM,
797  .device = 1,
798  .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
799  .info = spdif_info,
800  .get = spdif_default_get,
801  .put = spdif_default_put,
802  },
803  {
804  .iface = SNDRV_CTL_ELEM_IFACE_PCM,
805  .device = 1,
807  .access = SNDRV_CTL_ELEM_ACCESS_READ,
808  .info = spdif_info,
809  .get = spdif_mask_get,
810  },
811  {
812  .iface = SNDRV_CTL_ELEM_IFACE_PCM,
813  .device = 1,
814  .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
817  .info = spdif_info,
818  .get = spdif_pcm_get,
819  .put = spdif_pcm_put,
820  },
821 };
822 
823 static const struct snd_kcontrol_new spdif_input_controls[] = {
824  {
825  .iface = SNDRV_CTL_ELEM_IFACE_PCM,
826  .device = 1,
827  .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK),
828  .access = SNDRV_CTL_ELEM_ACCESS_READ,
829  .info = spdif_info,
830  .get = spdif_input_mask_get,
831  },
832  {
833  .iface = SNDRV_CTL_ELEM_IFACE_PCM,
834  .device = 1,
835  .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
836  .access = SNDRV_CTL_ELEM_ACCESS_READ,
837  .info = spdif_info,
838  .get = spdif_input_default_get,
839  },
840  {
842  .name = SNDRV_CTL_NAME_IEC958("Loopback ", NONE, SWITCH),
844  .get = spdif_bit_switch_get,
845  .put = spdif_bit_switch_put,
846  .private_value = OXYGEN_SPDIF_LOOPBACK,
847  },
848  {
850  .name = SNDRV_CTL_NAME_IEC958("Validity Check ",CAPTURE,SWITCH),
852  .get = spdif_bit_switch_get,
853  .put = spdif_bit_switch_put,
854  .private_value = OXYGEN_SPDIF_SPDVALID,
855  },
856 };
857 
858 static const struct {
859  unsigned int pcm_dev;
860  struct snd_kcontrol_new controls[2];
861 } monitor_controls[] = {
862  {
863  .pcm_dev = CAPTURE_0_FROM_I2S_1,
864  .controls = {
865  {
867  .name = "Analog Input Monitor Playback Switch",
869  .get = monitor_get,
870  .put = monitor_put,
871  .private_value = OXYGEN_ADC_MONITOR_A,
872  },
873  {
875  .name = "Analog Input Monitor Playback Volume",
878  .info = monitor_volume_info,
879  .get = monitor_get,
880  .put = monitor_put,
881  .private_value = OXYGEN_ADC_MONITOR_A_HALF_VOL
882  | (1 << 8),
883  .tlv = { .p = monitor_db_scale, },
884  },
885  },
886  },
887  {
888  .pcm_dev = CAPTURE_0_FROM_I2S_2,
889  .controls = {
890  {
892  .name = "Analog Input Monitor Playback Switch",
894  .get = monitor_get,
895  .put = monitor_put,
896  .private_value = OXYGEN_ADC_MONITOR_B,
897  },
898  {
900  .name = "Analog Input Monitor Playback Volume",
903  .info = monitor_volume_info,
904  .get = monitor_get,
905  .put = monitor_put,
906  .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
907  | (1 << 8),
908  .tlv = { .p = monitor_db_scale, },
909  },
910  },
911  },
912  {
913  .pcm_dev = CAPTURE_2_FROM_I2S_2,
914  .controls = {
915  {
917  .name = "Analog Input Monitor Playback Switch",
918  .index = 1,
920  .get = monitor_get,
921  .put = monitor_put,
922  .private_value = OXYGEN_ADC_MONITOR_B,
923  },
924  {
926  .name = "Analog Input Monitor Playback Volume",
927  .index = 1,
930  .info = monitor_volume_info,
931  .get = monitor_get,
932  .put = monitor_put,
933  .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
934  | (1 << 8),
935  .tlv = { .p = monitor_db_scale, },
936  },
937  },
938  },
939  {
940  .pcm_dev = CAPTURE_1_FROM_SPDIF,
941  .controls = {
942  {
944  .name = "Digital Input Monitor Playback Switch",
946  .get = monitor_get,
947  .put = monitor_put,
948  .private_value = OXYGEN_ADC_MONITOR_C,
949  },
950  {
952  .name = "Digital Input Monitor Playback Volume",
955  .info = monitor_volume_info,
956  .get = monitor_get,
957  .put = monitor_put,
958  .private_value = OXYGEN_ADC_MONITOR_C_HALF_VOL
959  | (1 << 8),
960  .tlv = { .p = monitor_db_scale, },
961  },
962  },
963  },
964 };
965 
966 static const struct snd_kcontrol_new ac97_controls[] = {
967  AC97_VOLUME("Mic Capture Volume", 0, AC97_MIC, 0),
968  AC97_SWITCH("Mic Capture Switch", 0, AC97_MIC, 15, 1),
969  AC97_SWITCH("Mic Boost (+20dB)", 0, AC97_MIC, 6, 0),
970  {
972  .name = "Mic Source Capture Enum",
973  .info = mic_fmic_source_info,
974  .get = mic_fmic_source_get,
975  .put = mic_fmic_source_put,
976  },
977  AC97_SWITCH("Line Capture Switch", 0, AC97_LINE, 15, 1),
978  AC97_VOLUME("CD Capture Volume", 0, AC97_CD, 1),
979  AC97_SWITCH("CD Capture Switch", 0, AC97_CD, 15, 1),
980  AC97_VOLUME("Aux Capture Volume", 0, AC97_AUX, 1),
981  AC97_SWITCH("Aux Capture Switch", 0, AC97_AUX, 15, 1),
982 };
983 
984 static const struct snd_kcontrol_new ac97_fp_controls[] = {
985  AC97_VOLUME("Front Panel Playback Volume", 1, AC97_HEADPHONE, 1),
986  AC97_SWITCH("Front Panel Playback Switch", 1, AC97_HEADPHONE, 15, 1),
987  {
989  .name = "Front Panel Capture Volume",
992  .info = ac97_fp_rec_volume_info,
993  .get = ac97_fp_rec_volume_get,
994  .put = ac97_fp_rec_volume_put,
995  .tlv = { .p = ac97_rec_db_scale, },
996  },
997  AC97_SWITCH("Front Panel Capture Switch", 1, AC97_REC_GAIN, 15, 1),
998 };
999 
1000 static void oxygen_any_ctl_free(struct snd_kcontrol *ctl)
1001 {
1002  struct oxygen *chip = ctl->private_data;
1003  unsigned int i;
1004 
1005  /* I'm too lazy to write a function for each control :-) */
1006  for (i = 0; i < ARRAY_SIZE(chip->controls); ++i)
1007  chip->controls[i] = NULL;
1008 }
1009 
1010 static int add_controls(struct oxygen *chip,
1011  const struct snd_kcontrol_new controls[],
1012  unsigned int count)
1013 {
1014  static const char *const known_ctl_names[CONTROL_COUNT] = {
1015  [CONTROL_SPDIF_PCM] =
1016  SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
1019  [CONTROL_MIC_CAPTURE_SWITCH] = "Mic Capture Switch",
1020  [CONTROL_LINE_CAPTURE_SWITCH] = "Line Capture Switch",
1021  [CONTROL_CD_CAPTURE_SWITCH] = "CD Capture Switch",
1022  [CONTROL_AUX_CAPTURE_SWITCH] = "Aux Capture Switch",
1023  };
1024  unsigned int i, j;
1025  struct snd_kcontrol_new template;
1026  struct snd_kcontrol *ctl;
1027  int err;
1028 
1029  for (i = 0; i < count; ++i) {
1030  template = controls[i];
1031  if (chip->model.control_filter) {
1032  err = chip->model.control_filter(&template);
1033  if (err < 0)
1034  return err;
1035  if (err == 1)
1036  continue;
1037  }
1038  if (!strcmp(template.name, "Stereo Upmixing") &&
1039  chip->model.dac_channels_pcm == 2)
1040  continue;
1041  if (!strcmp(template.name, "Mic Source Capture Enum") &&
1042  !(chip->model.device_config & AC97_FMIC_SWITCH))
1043  continue;
1044  if (!strncmp(template.name, "CD Capture ", 11) &&
1045  !(chip->model.device_config & AC97_CD_INPUT))
1046  continue;
1047  if (!strcmp(template.name, "Master Playback Volume") &&
1048  chip->model.dac_tlv) {
1049  template.tlv.p = chip->model.dac_tlv;
1050  template.access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1051  }
1052  ctl = snd_ctl_new1(&template, chip);
1053  if (!ctl)
1054  return -ENOMEM;
1055  err = snd_ctl_add(chip->card, ctl);
1056  if (err < 0)
1057  return err;
1058  for (j = 0; j < CONTROL_COUNT; ++j)
1059  if (!strcmp(ctl->id.name, known_ctl_names[j])) {
1060  chip->controls[j] = ctl;
1061  ctl->private_free = oxygen_any_ctl_free;
1062  }
1063  }
1064  return 0;
1065 }
1066 
1067 int oxygen_mixer_init(struct oxygen *chip)
1068 {
1069  unsigned int i;
1070  int err;
1071 
1072  err = add_controls(chip, controls, ARRAY_SIZE(controls));
1073  if (err < 0)
1074  return err;
1075  if (chip->model.device_config & CAPTURE_1_FROM_SPDIF) {
1076  err = add_controls(chip, spdif_input_controls,
1077  ARRAY_SIZE(spdif_input_controls));
1078  if (err < 0)
1079  return err;
1080  }
1081  for (i = 0; i < ARRAY_SIZE(monitor_controls); ++i) {
1082  if (!(chip->model.device_config & monitor_controls[i].pcm_dev))
1083  continue;
1084  err = add_controls(chip, monitor_controls[i].controls,
1085  ARRAY_SIZE(monitor_controls[i].controls));
1086  if (err < 0)
1087  return err;
1088  }
1089  if (chip->has_ac97_0) {
1090  err = add_controls(chip, ac97_controls,
1091  ARRAY_SIZE(ac97_controls));
1092  if (err < 0)
1093  return err;
1094  }
1095  if (chip->has_ac97_1) {
1096  err = add_controls(chip, ac97_fp_controls,
1097  ARRAY_SIZE(ac97_fp_controls));
1098  if (err < 0)
1099  return err;
1100  }
1101  return chip->model.mixer_init ? chip->model.mixer_init(chip) : 0;
1102 }