Commit | Line | Data |
---|---|---|
d0ce9946 CL |
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 | ||
d0ce9946 CL |
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" | |
878ac3ee | 26 | #include "cm9780.h" |
d0ce9946 CL |
27 | |
28 | static int dac_volume_info(struct snd_kcontrol *ctl, | |
29 | struct snd_ctl_elem_info *info) | |
30 | { | |
976cd627 CL |
31 | struct oxygen *chip = ctl->private_data; |
32 | ||
d0ce9946 | 33 | info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
976cd627 | 34 | info->count = chip->model->dac_channels; |
ccc80fb4 | 35 | info->value.integer.min = 0; |
d0ce9946 CL |
36 | info->value.integer.max = 0xff; |
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); | |
976cd627 | 47 | for (i = 0; i < chip->model->dac_channels; ++i) |
d0ce9946 CL |
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); | |
976cd627 | 62 | for (i = 0; i < chip->model->dac_channels; ++i) |
d0ce9946 CL |
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[3] = { | |
7113e958 | 103 | "Front", "Front+Surround", "Front+Surround+Back" |
d0ce9946 | 104 | }; |
976cd627 CL |
105 | struct oxygen *chip = ctl->private_data; |
106 | unsigned int count = 2 + (chip->model->dac_channels == 8); | |
107 | ||
d0ce9946 CL |
108 | info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
109 | info->count = 1; | |
976cd627 CL |
110 | info->value.enumerated.items = count; |
111 | if (info->value.enumerated.item >= count) | |
112 | info->value.enumerated.item = count - 1; | |
d0ce9946 CL |
113 | strcpy(info->value.enumerated.name, names[info->value.enumerated.item]); |
114 | return 0; | |
115 | } | |
116 | ||
117 | static 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 | ||
127 | void oxygen_update_dac_routing(struct oxygen *chip) | |
128 | { | |
c9946b2c | 129 | /* DAC 0: front, DAC 1: surround, DAC 2: center/LFE, DAC 3: back */ |
d0ce9946 | 130 | static const unsigned int reg_values[3] = { |
c9946b2c CL |
131 | /* 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), | |
d0ce9946 | 146 | }; |
7113e958 | 147 | u8 channels; |
d0ce9946 CL |
148 | unsigned int reg_value; |
149 | ||
7113e958 CL |
150 | channels = oxygen_read8(chip, OXYGEN_PLAY_CHANNELS) & |
151 | OXYGEN_PLAY_CHANNELS_MASK; | |
152 | if (channels == OXYGEN_PLAY_CHANNELS_2) | |
d0ce9946 | 153 | reg_value = reg_values[chip->dac_routing]; |
7113e958 | 154 | else if (channels == OXYGEN_PLAY_CHANNELS_8) |
c9946b2c CL |
155 | /* 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); | |
d0ce9946 | 160 | else |
c9946b2c CL |
161 | 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); | |
d0ce9946 CL |
170 | } |
171 | ||
172 | static int upmix_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value) | |
173 | { | |
174 | struct oxygen *chip = ctl->private_data; | |
976cd627 | 175 | unsigned int count = 2 + (chip->model->dac_channels == 8); |
d0ce9946 CL |
176 | int changed; |
177 | ||
178 | mutex_lock(&chip->mutex); | |
179 | changed = value->value.enumerated.item[0] != chip->dac_routing; | |
180 | if (changed) { | |
976cd627 CL |
181 | chip->dac_routing = min(value->value.enumerated.item[0], |
182 | count - 1); | |
d0ce9946 CL |
183 | 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 | ||
191 | static 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 | ||
202 | static 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: | |
214 | return 0x8 << OXYGEN_SPDIF_CS_RATE_SHIFT; | |
215 | case OXYGEN_RATE_96000: | |
216 | return 0xa << OXYGEN_SPDIF_CS_RATE_SHIFT; | |
217 | case OXYGEN_RATE_176400: | |
218 | return 0xc << OXYGEN_SPDIF_CS_RATE_SHIFT; | |
219 | case OXYGEN_RATE_192000: | |
220 | return 0xe << OXYGEN_SPDIF_CS_RATE_SHIFT; | |
221 | } | |
222 | } | |
223 | ||
224 | void 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; | |
c9946b2c CL |
234 | new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK) |
235 | | OXYGEN_PLAY_SPDIF_SPDIF; | |
d0ce9946 CL |
236 | 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) { | |
c9946b2c CL |
241 | new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK) |
242 | | OXYGEN_PLAY_SPDIF_MULTICH_01; | |
d0ce9946 CL |
243 | 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 | ||
266 | static 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 | ||
284 | static 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 | ||
291 | static 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 | ||
300 | static 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 | ||
313 | static 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 | ||
324 | static 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 | ||
335 | static 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 | ||
354 | static 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 | ||
364 | static 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 | ||
375 | static 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 | ||
394 | static 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 | ||
404 | static 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 | ||
02f21c9d CL |
418 | static 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 | ||
429 | static 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 | ||
d0ce9946 CL |
449 | static int ac97_switch_get(struct snd_kcontrol *ctl, |
450 | struct snd_ctl_elem_value *value) | |
451 | { | |
452 | struct oxygen *chip = ctl->private_data; | |
453 | unsigned int index = ctl->private_value & 0xff; | |
454 | unsigned int bitnr = (ctl->private_value >> 8) & 0xff; | |
455 | int invert = ctl->private_value & (1 << 16); | |
456 | u16 reg; | |
457 | ||
458 | mutex_lock(&chip->mutex); | |
459 | reg = oxygen_read_ac97(chip, 0, index); | |
460 | mutex_unlock(&chip->mutex); | |
461 | if (!(reg & (1 << bitnr)) ^ !invert) | |
462 | value->value.integer.value[0] = 1; | |
463 | else | |
464 | value->value.integer.value[0] = 0; | |
465 | return 0; | |
466 | } | |
467 | ||
468 | static int ac97_switch_put(struct snd_kcontrol *ctl, | |
469 | struct snd_ctl_elem_value *value) | |
470 | { | |
471 | struct oxygen *chip = ctl->private_data; | |
472 | unsigned int index = ctl->private_value & 0xff; | |
473 | unsigned int bitnr = (ctl->private_value >> 8) & 0xff; | |
474 | int invert = ctl->private_value & (1 << 16); | |
475 | u16 oldreg, newreg; | |
476 | int change; | |
477 | ||
478 | mutex_lock(&chip->mutex); | |
479 | oldreg = oxygen_read_ac97(chip, 0, index); | |
480 | newreg = oldreg; | |
481 | if (!value->value.integer.value[0] ^ !invert) | |
482 | newreg |= 1 << bitnr; | |
483 | else | |
484 | newreg &= ~(1 << bitnr); | |
485 | change = newreg != oldreg; | |
486 | if (change) { | |
487 | oxygen_write_ac97(chip, 0, index, newreg); | |
911b499a CL |
488 | if (bitnr == 15 && chip->model->ac97_switch_hook) |
489 | chip->model->ac97_switch_hook(chip, index, | |
490 | newreg & 0x8000); | |
d0ce9946 CL |
491 | } |
492 | mutex_unlock(&chip->mutex); | |
493 | return change; | |
494 | } | |
495 | ||
496 | static int ac97_volume_info(struct snd_kcontrol *ctl, | |
497 | struct snd_ctl_elem_info *info) | |
498 | { | |
499 | info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
500 | info->count = 2; | |
501 | info->value.integer.min = 0; | |
502 | info->value.integer.max = 0x1f; | |
503 | return 0; | |
504 | } | |
505 | ||
506 | static int ac97_volume_get(struct snd_kcontrol *ctl, | |
507 | struct snd_ctl_elem_value *value) | |
508 | { | |
509 | struct oxygen *chip = ctl->private_data; | |
510 | unsigned int index = ctl->private_value; | |
511 | u16 reg; | |
512 | ||
513 | mutex_lock(&chip->mutex); | |
514 | reg = oxygen_read_ac97(chip, 0, index); | |
515 | mutex_unlock(&chip->mutex); | |
516 | value->value.integer.value[0] = 31 - (reg & 0x1f); | |
517 | value->value.integer.value[1] = 31 - ((reg >> 8) & 0x1f); | |
518 | return 0; | |
519 | } | |
520 | ||
521 | static int ac97_volume_put(struct snd_kcontrol *ctl, | |
522 | struct snd_ctl_elem_value *value) | |
523 | { | |
524 | struct oxygen *chip = ctl->private_data; | |
525 | unsigned int index = ctl->private_value; | |
526 | u16 oldreg, newreg; | |
527 | int change; | |
528 | ||
529 | mutex_lock(&chip->mutex); | |
530 | oldreg = oxygen_read_ac97(chip, 0, index); | |
531 | newreg = oldreg; | |
532 | newreg = (newreg & ~0x1f) | | |
533 | (31 - (value->value.integer.value[0] & 0x1f)); | |
534 | newreg = (newreg & ~0x1f00) | | |
535 | ((31 - (value->value.integer.value[0] & 0x1f)) << 8); | |
536 | change = newreg != oldreg; | |
537 | if (change) | |
538 | oxygen_write_ac97(chip, 0, index, newreg); | |
539 | mutex_unlock(&chip->mutex); | |
540 | return change; | |
541 | } | |
542 | ||
543 | #define AC97_SWITCH(xname, index, bitnr, invert) { \ | |
544 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ | |
545 | .name = xname, \ | |
546 | .info = snd_ctl_boolean_mono_info, \ | |
547 | .get = ac97_switch_get, \ | |
548 | .put = ac97_switch_put, \ | |
549 | .private_value = ((invert) << 16) | ((bitnr) << 8) | (index), \ | |
550 | } | |
551 | #define AC97_VOLUME(xname, index) { \ | |
552 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ | |
553 | .name = xname, \ | |
554 | .info = ac97_volume_info, \ | |
555 | .get = ac97_volume_get, \ | |
556 | .put = ac97_volume_put, \ | |
557 | .tlv = { .p = ac97_db_scale, }, \ | |
558 | .private_value = (index), \ | |
559 | } | |
560 | ||
561 | static DECLARE_TLV_DB_SCALE(ac97_db_scale, -3450, 150, 0); | |
562 | ||
563 | static const struct snd_kcontrol_new controls[] = { | |
564 | { | |
565 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
fb920b7d | 566 | .name = "Master Playback Volume", |
ccc80fb4 | 567 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
d0ce9946 CL |
568 | .info = dac_volume_info, |
569 | .get = dac_volume_get, | |
570 | .put = dac_volume_put, | |
d0ce9946 CL |
571 | }, |
572 | { | |
573 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
fb920b7d | 574 | .name = "Master Playback Switch", |
d0ce9946 CL |
575 | .info = snd_ctl_boolean_mono_info, |
576 | .get = dac_mute_get, | |
577 | .put = dac_mute_put, | |
578 | }, | |
579 | { | |
580 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
581 | .name = "Stereo Upmixing", | |
582 | .info = upmix_info, | |
583 | .get = upmix_get, | |
584 | .put = upmix_put, | |
585 | }, | |
586 | { | |
587 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
588 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), | |
589 | .info = snd_ctl_boolean_mono_info, | |
590 | .get = spdif_switch_get, | |
591 | .put = spdif_switch_put, | |
592 | }, | |
593 | { | |
594 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, | |
595 | .device = 1, | |
596 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), | |
597 | .info = spdif_info, | |
598 | .get = spdif_default_get, | |
599 | .put = spdif_default_put, | |
600 | }, | |
601 | { | |
602 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, | |
603 | .device = 1, | |
604 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), | |
605 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
606 | .info = spdif_info, | |
607 | .get = spdif_mask_get, | |
608 | }, | |
609 | { | |
610 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, | |
611 | .device = 1, | |
612 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM), | |
613 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | | |
614 | SNDRV_CTL_ELEM_ACCESS_INACTIVE, | |
615 | .info = spdif_info, | |
616 | .get = spdif_pcm_get, | |
617 | .put = spdif_pcm_put, | |
618 | }, | |
619 | { | |
620 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, | |
621 | .device = 1, | |
622 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK), | |
623 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
624 | .info = spdif_info, | |
625 | .get = spdif_input_mask_get, | |
626 | }, | |
627 | { | |
628 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, | |
629 | .device = 1, | |
630 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), | |
631 | .access = SNDRV_CTL_ELEM_ACCESS_READ, | |
632 | .info = spdif_info, | |
633 | .get = spdif_input_default_get, | |
634 | }, | |
02f21c9d CL |
635 | { |
636 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
637 | .name = SNDRV_CTL_NAME_IEC958("Loopback ", NONE, SWITCH), | |
638 | .info = snd_ctl_boolean_mono_info, | |
639 | .get = spdif_loopback_get, | |
640 | .put = spdif_loopback_put, | |
641 | }, | |
31c77643 CL |
642 | }; |
643 | ||
644 | static const struct snd_kcontrol_new ac97_controls[] = { | |
d0ce9946 CL |
645 | AC97_VOLUME("Mic Capture Volume", AC97_MIC), |
646 | AC97_SWITCH("Mic Capture Switch", AC97_MIC, 15, 1), | |
647 | AC97_SWITCH("Mic Boost (+20dB)", AC97_MIC, 6, 0), | |
911b499a | 648 | AC97_VOLUME("Line Capture Volume", AC97_LINE), |
d0ce9946 CL |
649 | AC97_SWITCH("Line Capture Switch", AC97_LINE, 15, 1), |
650 | AC97_VOLUME("CD Capture Volume", AC97_CD), | |
651 | AC97_SWITCH("CD Capture Switch", AC97_CD, 15, 1), | |
652 | AC97_VOLUME("Aux Capture Volume", AC97_AUX), | |
653 | AC97_SWITCH("Aux Capture Switch", AC97_AUX, 15, 1), | |
654 | }; | |
655 | ||
656 | static void oxygen_any_ctl_free(struct snd_kcontrol *ctl) | |
657 | { | |
658 | struct oxygen *chip = ctl->private_data; | |
01a3affb | 659 | unsigned int i; |
d0ce9946 CL |
660 | |
661 | /* I'm too lazy to write a function for each control :-) */ | |
01a3affb CL |
662 | for (i = 0; i < ARRAY_SIZE(chip->controls); ++i) |
663 | chip->controls[i] = NULL; | |
d0ce9946 CL |
664 | } |
665 | ||
31c77643 CL |
666 | static int add_controls(struct oxygen *chip, |
667 | const struct snd_kcontrol_new controls[], | |
668 | unsigned int count) | |
d0ce9946 | 669 | { |
01a3affb CL |
670 | static const char *const known_ctl_names[CONTROL_COUNT] = { |
671 | [CONTROL_SPDIF_PCM] = | |
672 | SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM), | |
673 | [CONTROL_SPDIF_INPUT_BITS] = | |
674 | SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), | |
893e44ba CL |
675 | [CONTROL_MIC_CAPTURE_SWITCH] = "Mic Capture Switch", |
676 | [CONTROL_LINE_CAPTURE_SWITCH] = "Line Capture Switch", | |
677 | [CONTROL_CD_CAPTURE_SWITCH] = "CD Capture Switch", | |
678 | [CONTROL_AUX_CAPTURE_SWITCH] = "Aux Capture Switch", | |
01a3affb CL |
679 | }; |
680 | unsigned int i, j; | |
ccc80fb4 | 681 | struct snd_kcontrol_new template; |
d0ce9946 CL |
682 | struct snd_kcontrol *ctl; |
683 | int err; | |
684 | ||
31c77643 | 685 | for (i = 0; i < count; ++i) { |
ccc80fb4 CL |
686 | template = controls[i]; |
687 | err = chip->model->control_filter(&template); | |
688 | if (err < 0) | |
689 | return err; | |
c626026d CL |
690 | if (err == 1) |
691 | continue; | |
e9d88a8b | 692 | ctl = snd_ctl_new1(&template, chip); |
d0ce9946 CL |
693 | if (!ctl) |
694 | return -ENOMEM; | |
d0ce9946 CL |
695 | err = snd_ctl_add(chip->card, ctl); |
696 | if (err < 0) | |
697 | return err; | |
01a3affb CL |
698 | for (j = 0; j < CONTROL_COUNT; ++j) |
699 | if (!strcmp(ctl->id.name, known_ctl_names[j])) { | |
700 | chip->controls[j] = ctl; | |
701 | ctl->private_free = oxygen_any_ctl_free; | |
702 | } | |
d0ce9946 | 703 | } |
31c77643 CL |
704 | return 0; |
705 | } | |
706 | ||
707 | int oxygen_mixer_init(struct oxygen *chip) | |
708 | { | |
709 | int err; | |
710 | ||
711 | err = add_controls(chip, controls, ARRAY_SIZE(controls)); | |
712 | if (err < 0) | |
713 | return err; | |
714 | if (chip->has_ac97_0) { | |
715 | err = add_controls(chip, ac97_controls, | |
716 | ARRAY_SIZE(ac97_controls)); | |
717 | if (err < 0) | |
718 | return err; | |
719 | } | |
d0ce9946 CL |
720 | return chip->model->mixer_init ? chip->model->mixer_init(chip) : 0; |
721 | } |