Linux 2.6.26-rc5
[linux-2.6/openmoko-kernel/knife-kernel.git] / sound / pci / ca0106 / ca0106_mixer.c
blob3025ed1b6e1efd1444ccf5d3403d4ca7ab36627c
1 /*
2 * Copyright (c) 2004 James Courtier-Dutton <James@superbug.demon.co.uk>
3 * Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
4 * Version: 0.0.18
6 * FEATURES currently supported:
7 * See ca0106_main.c for features.
8 *
9 * Changelog:
10 * Support interrupts per period.
11 * Removed noise from Center/LFE channel when in Analog mode.
12 * Rename and remove mixer controls.
13 * 0.0.6
14 * Use separate card based DMA buffer for periods table list.
15 * 0.0.7
16 * Change remove and rename ctrls into lists.
17 * 0.0.8
18 * Try to fix capture sources.
19 * 0.0.9
20 * Fix AC3 output.
21 * Enable S32_LE format support.
22 * 0.0.10
23 * Enable playback 48000 and 96000 rates. (Rates other that these do not work, even with "plug:front".)
24 * 0.0.11
25 * Add Model name recognition.
26 * 0.0.12
27 * Correct interrupt timing. interrupt at end of period, instead of in the middle of a playback period.
28 * Remove redundent "voice" handling.
29 * 0.0.13
30 * Single trigger call for multi channels.
31 * 0.0.14
32 * Set limits based on what the sound card hardware can do.
33 * playback periods_min=2, periods_max=8
34 * capture hw constraints require period_size = n * 64 bytes.
35 * playback hw constraints require period_size = n * 64 bytes.
36 * 0.0.15
37 * Separated ca0106.c into separate functional .c files.
38 * 0.0.16
39 * Modified Copyright message.
40 * 0.0.17
41 * Implement Mic and Line in Capture.
42 * 0.0.18
43 * Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
45 * This code was initally based on code from ALSA's emu10k1x.c which is:
46 * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
48 * This program is free software; you can redistribute it and/or modify
49 * it under the terms of the GNU General Public License as published by
50 * the Free Software Foundation; either version 2 of the License, or
51 * (at your option) any later version.
53 * This program is distributed in the hope that it will be useful,
54 * but WITHOUT ANY WARRANTY; without even the implied warranty of
55 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
56 * GNU General Public License for more details.
58 * You should have received a copy of the GNU General Public License
59 * along with this program; if not, write to the Free Software
60 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
63 #include <linux/delay.h>
64 #include <linux/init.h>
65 #include <linux/interrupt.h>
66 #include <linux/slab.h>
67 #include <linux/moduleparam.h>
68 #include <sound/core.h>
69 #include <sound/initval.h>
70 #include <sound/pcm.h>
71 #include <sound/ac97_codec.h>
72 #include <sound/info.h>
73 #include <sound/tlv.h>
74 #include <asm/io.h>
76 #include "ca0106.h"
78 static const DECLARE_TLV_DB_SCALE(snd_ca0106_db_scale1, -5175, 25, 1);
79 static const DECLARE_TLV_DB_SCALE(snd_ca0106_db_scale2, -10350, 50, 1);
81 #define snd_ca0106_shared_spdif_info snd_ctl_boolean_mono_info
83 static int snd_ca0106_shared_spdif_get(struct snd_kcontrol *kcontrol,
84 struct snd_ctl_elem_value *ucontrol)
86 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
88 ucontrol->value.integer.value[0] = emu->spdif_enable;
89 return 0;
92 static int snd_ca0106_shared_spdif_put(struct snd_kcontrol *kcontrol,
93 struct snd_ctl_elem_value *ucontrol)
95 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
96 unsigned int val;
97 int change = 0;
98 u32 mask;
100 val = !!ucontrol->value.integer.value[0];
101 change = (emu->spdif_enable != val);
102 if (change) {
103 emu->spdif_enable = val;
104 if (val) {
105 /* Digital */
106 snd_ca0106_ptr_write(emu, SPDIF_SELECT1, 0, 0xf);
107 snd_ca0106_ptr_write(emu, SPDIF_SELECT2, 0, 0x0b000000);
108 snd_ca0106_ptr_write(emu, CAPTURE_CONTROL, 0,
109 snd_ca0106_ptr_read(emu, CAPTURE_CONTROL, 0) & ~0x1000);
110 mask = inl(emu->port + GPIO) & ~0x101;
111 outl(mask, emu->port + GPIO);
113 } else {
114 /* Analog */
115 snd_ca0106_ptr_write(emu, SPDIF_SELECT1, 0, 0xf);
116 snd_ca0106_ptr_write(emu, SPDIF_SELECT2, 0, 0x000f0000);
117 snd_ca0106_ptr_write(emu, CAPTURE_CONTROL, 0,
118 snd_ca0106_ptr_read(emu, CAPTURE_CONTROL, 0) | 0x1000);
119 mask = inl(emu->port + GPIO) | 0x101;
120 outl(mask, emu->port + GPIO);
123 return change;
126 static int snd_ca0106_capture_source_info(struct snd_kcontrol *kcontrol,
127 struct snd_ctl_elem_info *uinfo)
129 static char *texts[6] = {
130 "IEC958 out", "i2s mixer out", "IEC958 in", "i2s in", "AC97 in", "SRC out"
133 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
134 uinfo->count = 1;
135 uinfo->value.enumerated.items = 6;
136 if (uinfo->value.enumerated.item > 5)
137 uinfo->value.enumerated.item = 5;
138 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
139 return 0;
142 static int snd_ca0106_capture_source_get(struct snd_kcontrol *kcontrol,
143 struct snd_ctl_elem_value *ucontrol)
145 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
147 ucontrol->value.enumerated.item[0] = emu->capture_source;
148 return 0;
151 static int snd_ca0106_capture_source_put(struct snd_kcontrol *kcontrol,
152 struct snd_ctl_elem_value *ucontrol)
154 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
155 unsigned int val;
156 int change = 0;
157 u32 mask;
158 u32 source;
160 val = ucontrol->value.enumerated.item[0] ;
161 if (val >= 6)
162 return -EINVAL;
163 change = (emu->capture_source != val);
164 if (change) {
165 emu->capture_source = val;
166 source = (val << 28) | (val << 24) | (val << 20) | (val << 16);
167 mask = snd_ca0106_ptr_read(emu, CAPTURE_SOURCE, 0) & 0xffff;
168 snd_ca0106_ptr_write(emu, CAPTURE_SOURCE, 0, source | mask);
170 return change;
173 static int snd_ca0106_i2c_capture_source_info(struct snd_kcontrol *kcontrol,
174 struct snd_ctl_elem_info *uinfo)
176 static char *texts[6] = {
177 "Phone", "Mic", "Line in", "Aux"
180 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
181 uinfo->count = 1;
182 uinfo->value.enumerated.items = 4;
183 if (uinfo->value.enumerated.item > 3)
184 uinfo->value.enumerated.item = 3;
185 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
186 return 0;
189 static int snd_ca0106_i2c_capture_source_get(struct snd_kcontrol *kcontrol,
190 struct snd_ctl_elem_value *ucontrol)
192 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
194 ucontrol->value.enumerated.item[0] = emu->i2c_capture_source;
195 return 0;
198 static int snd_ca0106_i2c_capture_source_put(struct snd_kcontrol *kcontrol,
199 struct snd_ctl_elem_value *ucontrol)
201 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
202 unsigned int source_id;
203 unsigned int ngain, ogain;
204 int change = 0;
205 u32 source;
206 /* If the capture source has changed,
207 * update the capture volume from the cached value
208 * for the particular source.
210 source_id = ucontrol->value.enumerated.item[0] ;
211 if (source_id >= 4)
212 return -EINVAL;
213 change = (emu->i2c_capture_source != source_id);
214 if (change) {
215 snd_ca0106_i2c_write(emu, ADC_MUX, 0); /* Mute input */
216 ngain = emu->i2c_capture_volume[source_id][0]; /* Left */
217 ogain = emu->i2c_capture_volume[emu->i2c_capture_source][0]; /* Left */
218 if (ngain != ogain)
219 snd_ca0106_i2c_write(emu, ADC_ATTEN_ADCL, ((ngain) & 0xff));
220 ngain = emu->i2c_capture_volume[source_id][1]; /* Left */
221 ogain = emu->i2c_capture_volume[emu->i2c_capture_source][1]; /* Left */
222 if (ngain != ogain)
223 snd_ca0106_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));
224 source = 1 << source_id;
225 snd_ca0106_i2c_write(emu, ADC_MUX, source); /* Set source */
226 emu->i2c_capture_source = source_id;
228 return change;
231 static int snd_ca0106_capture_line_in_side_out_info(struct snd_kcontrol *kcontrol,
232 struct snd_ctl_elem_info *uinfo)
234 static char *texts[2] = { "Side out", "Line in" };
236 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
237 uinfo->count = 1;
238 uinfo->value.enumerated.items = 2;
239 if (uinfo->value.enumerated.item > 1)
240 uinfo->value.enumerated.item = 1;
241 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
242 return 0;
245 static int snd_ca0106_capture_mic_line_in_info(struct snd_kcontrol *kcontrol,
246 struct snd_ctl_elem_info *uinfo)
248 static char *texts[2] = { "Line in", "Mic in" };
250 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
251 uinfo->count = 1;
252 uinfo->value.enumerated.items = 2;
253 if (uinfo->value.enumerated.item > 1)
254 uinfo->value.enumerated.item = 1;
255 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
256 return 0;
259 static int snd_ca0106_capture_mic_line_in_get(struct snd_kcontrol *kcontrol,
260 struct snd_ctl_elem_value *ucontrol)
262 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
264 ucontrol->value.enumerated.item[0] = emu->capture_mic_line_in;
265 return 0;
268 static int snd_ca0106_capture_mic_line_in_put(struct snd_kcontrol *kcontrol,
269 struct snd_ctl_elem_value *ucontrol)
271 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
272 unsigned int val;
273 int change = 0;
274 u32 tmp;
276 val = ucontrol->value.enumerated.item[0] ;
277 if (val > 1)
278 return -EINVAL;
279 change = (emu->capture_mic_line_in != val);
280 if (change) {
281 emu->capture_mic_line_in = val;
282 if (val) {
283 //snd_ca0106_i2c_write(emu, ADC_MUX, 0); /* Mute input */
284 tmp = inl(emu->port+GPIO) & ~0x400;
285 tmp = tmp | 0x400;
286 outl(tmp, emu->port+GPIO);
287 //snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_MIC);
288 } else {
289 //snd_ca0106_i2c_write(emu, ADC_MUX, 0); /* Mute input */
290 tmp = inl(emu->port+GPIO) & ~0x400;
291 outl(tmp, emu->port+GPIO);
292 //snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_LINEIN);
295 return change;
298 static struct snd_kcontrol_new snd_ca0106_capture_mic_line_in __devinitdata =
300 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
301 .name = "Shared Mic/Line in Capture Switch",
302 .info = snd_ca0106_capture_mic_line_in_info,
303 .get = snd_ca0106_capture_mic_line_in_get,
304 .put = snd_ca0106_capture_mic_line_in_put
307 static struct snd_kcontrol_new snd_ca0106_capture_line_in_side_out __devinitdata =
309 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
310 .name = "Shared Line in/Side out Capture Switch",
311 .info = snd_ca0106_capture_line_in_side_out_info,
312 .get = snd_ca0106_capture_mic_line_in_get,
313 .put = snd_ca0106_capture_mic_line_in_put
317 static int snd_ca0106_spdif_info(struct snd_kcontrol *kcontrol,
318 struct snd_ctl_elem_info *uinfo)
320 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
321 uinfo->count = 1;
322 return 0;
325 static int snd_ca0106_spdif_get(struct snd_kcontrol *kcontrol,
326 struct snd_ctl_elem_value *ucontrol)
328 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
329 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
331 ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
332 ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
333 ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
334 ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
335 return 0;
338 static int snd_ca0106_spdif_get_mask(struct snd_kcontrol *kcontrol,
339 struct snd_ctl_elem_value *ucontrol)
341 ucontrol->value.iec958.status[0] = 0xff;
342 ucontrol->value.iec958.status[1] = 0xff;
343 ucontrol->value.iec958.status[2] = 0xff;
344 ucontrol->value.iec958.status[3] = 0xff;
345 return 0;
348 static int snd_ca0106_spdif_put(struct snd_kcontrol *kcontrol,
349 struct snd_ctl_elem_value *ucontrol)
351 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
352 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
353 int change;
354 unsigned int val;
356 val = (ucontrol->value.iec958.status[0] << 0) |
357 (ucontrol->value.iec958.status[1] << 8) |
358 (ucontrol->value.iec958.status[2] << 16) |
359 (ucontrol->value.iec958.status[3] << 24);
360 change = val != emu->spdif_bits[idx];
361 if (change) {
362 snd_ca0106_ptr_write(emu, SPCS0 + idx, 0, val);
363 emu->spdif_bits[idx] = val;
365 return change;
368 static int snd_ca0106_volume_info(struct snd_kcontrol *kcontrol,
369 struct snd_ctl_elem_info *uinfo)
371 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
372 uinfo->count = 2;
373 uinfo->value.integer.min = 0;
374 uinfo->value.integer.max = 255;
375 return 0;
378 static int snd_ca0106_volume_get(struct snd_kcontrol *kcontrol,
379 struct snd_ctl_elem_value *ucontrol)
381 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
382 unsigned int value;
383 int channel_id, reg;
385 channel_id = (kcontrol->private_value >> 8) & 0xff;
386 reg = kcontrol->private_value & 0xff;
388 value = snd_ca0106_ptr_read(emu, reg, channel_id);
389 ucontrol->value.integer.value[0] = 0xff - ((value >> 24) & 0xff); /* Left */
390 ucontrol->value.integer.value[1] = 0xff - ((value >> 16) & 0xff); /* Right */
391 return 0;
394 static int snd_ca0106_volume_put(struct snd_kcontrol *kcontrol,
395 struct snd_ctl_elem_value *ucontrol)
397 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
398 unsigned int oval, nval;
399 int channel_id, reg;
401 channel_id = (kcontrol->private_value >> 8) & 0xff;
402 reg = kcontrol->private_value & 0xff;
404 oval = snd_ca0106_ptr_read(emu, reg, channel_id);
405 nval = ((0xff - ucontrol->value.integer.value[0]) << 24) |
406 ((0xff - ucontrol->value.integer.value[1]) << 16);
407 nval |= ((0xff - ucontrol->value.integer.value[0]) << 8) |
408 ((0xff - ucontrol->value.integer.value[1]) );
409 if (oval == nval)
410 return 0;
411 snd_ca0106_ptr_write(emu, reg, channel_id, nval);
412 return 1;
415 static int snd_ca0106_i2c_volume_info(struct snd_kcontrol *kcontrol,
416 struct snd_ctl_elem_info *uinfo)
418 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
419 uinfo->count = 2;
420 uinfo->value.integer.min = 0;
421 uinfo->value.integer.max = 255;
422 return 0;
425 static int snd_ca0106_i2c_volume_get(struct snd_kcontrol *kcontrol,
426 struct snd_ctl_elem_value *ucontrol)
428 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
429 int source_id;
431 source_id = kcontrol->private_value;
433 ucontrol->value.integer.value[0] = emu->i2c_capture_volume[source_id][0];
434 ucontrol->value.integer.value[1] = emu->i2c_capture_volume[source_id][1];
435 return 0;
438 static int snd_ca0106_i2c_volume_put(struct snd_kcontrol *kcontrol,
439 struct snd_ctl_elem_value *ucontrol)
441 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
442 unsigned int ogain;
443 unsigned int ngain;
444 int source_id;
445 int change = 0;
447 source_id = kcontrol->private_value;
448 ogain = emu->i2c_capture_volume[source_id][0]; /* Left */
449 ngain = ucontrol->value.integer.value[0];
450 if (ngain > 0xff)
451 return -EINVAL;
452 if (ogain != ngain) {
453 if (emu->i2c_capture_source == source_id)
454 snd_ca0106_i2c_write(emu, ADC_ATTEN_ADCL, ((ngain) & 0xff) );
455 emu->i2c_capture_volume[source_id][0] = ucontrol->value.integer.value[0];
456 change = 1;
458 ogain = emu->i2c_capture_volume[source_id][1]; /* Right */
459 ngain = ucontrol->value.integer.value[1];
460 if (ngain > 0xff)
461 return -EINVAL;
462 if (ogain != ngain) {
463 if (emu->i2c_capture_source == source_id)
464 snd_ca0106_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));
465 emu->i2c_capture_volume[source_id][1] = ucontrol->value.integer.value[1];
466 change = 1;
469 return change;
472 #define spi_mute_info snd_ctl_boolean_mono_info
474 static int spi_mute_get(struct snd_kcontrol *kcontrol,
475 struct snd_ctl_elem_value *ucontrol)
477 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
478 unsigned int reg = kcontrol->private_value >> SPI_REG_SHIFT;
479 unsigned int bit = kcontrol->private_value & SPI_REG_MASK;
481 ucontrol->value.integer.value[0] = !(emu->spi_dac_reg[reg] & bit);
482 return 0;
485 static int spi_mute_put(struct snd_kcontrol *kcontrol,
486 struct snd_ctl_elem_value *ucontrol)
488 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
489 unsigned int reg = kcontrol->private_value >> SPI_REG_SHIFT;
490 unsigned int bit = kcontrol->private_value & SPI_REG_MASK;
491 int ret;
493 ret = emu->spi_dac_reg[reg] & bit;
494 if (ucontrol->value.integer.value[0]) {
495 if (!ret) /* bit already cleared, do nothing */
496 return 0;
497 emu->spi_dac_reg[reg] &= ~bit;
498 } else {
499 if (ret) /* bit already set, do nothing */
500 return 0;
501 emu->spi_dac_reg[reg] |= bit;
504 ret = snd_ca0106_spi_write(emu, emu->spi_dac_reg[reg]);
505 return ret ? -EINVAL : 1;
508 #define CA_VOLUME(xname,chid,reg) \
510 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
511 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
512 SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
513 .info = snd_ca0106_volume_info, \
514 .get = snd_ca0106_volume_get, \
515 .put = snd_ca0106_volume_put, \
516 .tlv = { .p = snd_ca0106_db_scale1 }, \
517 .private_value = ((chid) << 8) | (reg) \
520 static struct snd_kcontrol_new snd_ca0106_volume_ctls[] __devinitdata = {
521 CA_VOLUME("Analog Front Playback Volume",
522 CONTROL_FRONT_CHANNEL, PLAYBACK_VOLUME2),
523 CA_VOLUME("Analog Rear Playback Volume",
524 CONTROL_REAR_CHANNEL, PLAYBACK_VOLUME2),
525 CA_VOLUME("Analog Center/LFE Playback Volume",
526 CONTROL_CENTER_LFE_CHANNEL, PLAYBACK_VOLUME2),
527 CA_VOLUME("Analog Side Playback Volume",
528 CONTROL_UNKNOWN_CHANNEL, PLAYBACK_VOLUME2),
530 CA_VOLUME("IEC958 Front Playback Volume",
531 CONTROL_FRONT_CHANNEL, PLAYBACK_VOLUME1),
532 CA_VOLUME("IEC958 Rear Playback Volume",
533 CONTROL_REAR_CHANNEL, PLAYBACK_VOLUME1),
534 CA_VOLUME("IEC958 Center/LFE Playback Volume",
535 CONTROL_CENTER_LFE_CHANNEL, PLAYBACK_VOLUME1),
536 CA_VOLUME("IEC958 Unknown Playback Volume",
537 CONTROL_UNKNOWN_CHANNEL, PLAYBACK_VOLUME1),
539 CA_VOLUME("CAPTURE feedback Playback Volume",
540 1, CAPTURE_CONTROL),
543 .access = SNDRV_CTL_ELEM_ACCESS_READ,
544 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
545 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
546 .count = 4,
547 .info = snd_ca0106_spdif_info,
548 .get = snd_ca0106_spdif_get_mask
551 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
552 .name = "IEC958 Playback Switch",
553 .info = snd_ca0106_shared_spdif_info,
554 .get = snd_ca0106_shared_spdif_get,
555 .put = snd_ca0106_shared_spdif_put
558 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
559 .name = "Digital Source Capture Enum",
560 .info = snd_ca0106_capture_source_info,
561 .get = snd_ca0106_capture_source_get,
562 .put = snd_ca0106_capture_source_put
565 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
566 .name = "Analog Source Capture Enum",
567 .info = snd_ca0106_i2c_capture_source_info,
568 .get = snd_ca0106_i2c_capture_source_get,
569 .put = snd_ca0106_i2c_capture_source_put
572 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
573 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
574 .count = 4,
575 .info = snd_ca0106_spdif_info,
576 .get = snd_ca0106_spdif_get,
577 .put = snd_ca0106_spdif_put
581 #define I2C_VOLUME(xname,chid) \
583 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
584 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
585 SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
586 .info = snd_ca0106_i2c_volume_info, \
587 .get = snd_ca0106_i2c_volume_get, \
588 .put = snd_ca0106_i2c_volume_put, \
589 .tlv = { .p = snd_ca0106_db_scale2 }, \
590 .private_value = chid \
593 static struct snd_kcontrol_new snd_ca0106_volume_i2c_adc_ctls[] __devinitdata = {
594 I2C_VOLUME("Phone Capture Volume", 0),
595 I2C_VOLUME("Mic Capture Volume", 1),
596 I2C_VOLUME("Line in Capture Volume", 2),
597 I2C_VOLUME("Aux Capture Volume", 3),
600 #define SPI_SWITCH(xname,reg,bit) \
602 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
603 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
604 .info = spi_mute_info, \
605 .get = spi_mute_get, \
606 .put = spi_mute_put, \
607 .private_value = (reg<<SPI_REG_SHIFT) | (bit) \
610 static struct snd_kcontrol_new snd_ca0106_volume_spi_dac_ctls[]
611 __devinitdata = {
612 SPI_SWITCH("Analog Front Playback Switch",
613 SPI_DMUTE4_REG, SPI_DMUTE4_BIT),
614 SPI_SWITCH("Analog Rear Playback Switch",
615 SPI_DMUTE0_REG, SPI_DMUTE0_BIT),
616 SPI_SWITCH("Analog Center/LFE Playback Switch",
617 SPI_DMUTE2_REG, SPI_DMUTE2_BIT),
618 SPI_SWITCH("Analog Side Playback Switch",
619 SPI_DMUTE1_REG, SPI_DMUTE1_BIT),
622 static int __devinit remove_ctl(struct snd_card *card, const char *name)
624 struct snd_ctl_elem_id id;
625 memset(&id, 0, sizeof(id));
626 strcpy(id.name, name);
627 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
628 return snd_ctl_remove_id(card, &id);
631 static struct snd_kcontrol __devinit *ctl_find(struct snd_card *card, const char *name)
633 struct snd_ctl_elem_id sid;
634 memset(&sid, 0, sizeof(sid));
635 /* FIXME: strcpy is bad. */
636 strcpy(sid.name, name);
637 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
638 return snd_ctl_find_id(card, &sid);
641 static int __devinit rename_ctl(struct snd_card *card, const char *src, const char *dst)
643 struct snd_kcontrol *kctl = ctl_find(card, src);
644 if (kctl) {
645 strcpy(kctl->id.name, dst);
646 return 0;
648 return -ENOENT;
651 #define ADD_CTLS(emu, ctls) \
652 do { \
653 int i, _err; \
654 for (i = 0; i < ARRAY_SIZE(ctls); i++) { \
655 _err = snd_ctl_add(card, snd_ctl_new1(&ctls[i], emu)); \
656 if (_err < 0) \
657 return _err; \
659 } while (0)
661 static __devinitdata
662 DECLARE_TLV_DB_SCALE(snd_ca0106_master_db_scale, -6375, 50, 1);
664 static char *slave_vols[] __devinitdata = {
665 "Analog Front Playback Volume",
666 "Analog Rear Playback Volume",
667 "Analog Center/LFE Playback Volume",
668 "Analog Side Playback Volume",
669 "IEC958 Front Playback Volume",
670 "IEC958 Rear Playback Volume",
671 "IEC958 Center/LFE Playback Volume",
672 "IEC958 Unknown Playback Volume",
673 "CAPTURE feedback Playback Volume",
674 NULL
677 static char *slave_sws[] __devinitdata = {
678 "Analog Front Playback Switch",
679 "Analog Rear Playback Switch",
680 "Analog Center/LFE Playback Switch",
681 "Analog Side Playback Switch",
682 "IEC958 Playback Switch",
683 NULL
686 static void __devinit add_slaves(struct snd_card *card,
687 struct snd_kcontrol *master, char **list)
689 for (; *list; list++) {
690 struct snd_kcontrol *slave = ctl_find(card, *list);
691 if (slave)
692 snd_ctl_add_slave(master, slave);
696 int __devinit snd_ca0106_mixer(struct snd_ca0106 *emu)
698 int err;
699 struct snd_card *card = emu->card;
700 char **c;
701 struct snd_kcontrol *vmaster;
702 static char *ca0106_remove_ctls[] = {
703 "Master Mono Playback Switch",
704 "Master Mono Playback Volume",
705 "3D Control - Switch",
706 "3D Control Sigmatel - Depth",
707 "PCM Playback Switch",
708 "PCM Playback Volume",
709 "CD Playback Switch",
710 "CD Playback Volume",
711 "Phone Playback Switch",
712 "Phone Playback Volume",
713 "Video Playback Switch",
714 "Video Playback Volume",
715 "PC Speaker Playback Switch",
716 "PC Speaker Playback Volume",
717 "Mono Output Select",
718 "Capture Source",
719 "Capture Switch",
720 "Capture Volume",
721 "External Amplifier",
722 "Sigmatel 4-Speaker Stereo Playback Switch",
723 "Sigmatel Surround Phase Inversion Playback ",
724 NULL
726 static char *ca0106_rename_ctls[] = {
727 "Master Playback Switch", "Capture Switch",
728 "Master Playback Volume", "Capture Volume",
729 "Line Playback Switch", "AC97 Line Capture Switch",
730 "Line Playback Volume", "AC97 Line Capture Volume",
731 "Aux Playback Switch", "AC97 Aux Capture Switch",
732 "Aux Playback Volume", "AC97 Aux Capture Volume",
733 "Mic Playback Switch", "AC97 Mic Capture Switch",
734 "Mic Playback Volume", "AC97 Mic Capture Volume",
735 "Mic Select", "AC97 Mic Select",
736 "Mic Boost (+20dB)", "AC97 Mic Boost (+20dB)",
737 NULL
739 #if 1
740 for (c = ca0106_remove_ctls; *c; c++)
741 remove_ctl(card, *c);
742 for (c = ca0106_rename_ctls; *c; c += 2)
743 rename_ctl(card, c[0], c[1]);
744 #endif
746 ADD_CTLS(emu, snd_ca0106_volume_ctls);
747 if (emu->details->i2c_adc == 1) {
748 ADD_CTLS(emu, snd_ca0106_volume_i2c_adc_ctls);
749 if (emu->details->gpio_type == 1)
750 err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_capture_mic_line_in, emu));
751 else /* gpio_type == 2 */
752 err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_capture_line_in_side_out, emu));
753 if (err < 0)
754 return err;
756 if (emu->details->spi_dac == 1)
757 ADD_CTLS(emu, snd_ca0106_volume_spi_dac_ctls);
759 /* Create virtual master controls */
760 vmaster = snd_ctl_make_virtual_master("Master Playback Volume",
761 snd_ca0106_master_db_scale);
762 if (!vmaster)
763 return -ENOMEM;
764 add_slaves(card, vmaster, slave_vols);
766 if (emu->details->spi_dac == 1) {
767 vmaster = snd_ctl_make_virtual_master("Master Playback Switch",
768 NULL);
769 if (!vmaster)
770 return -ENOMEM;
771 add_slaves(card, vmaster, slave_sws);
773 return 0;