blob: df75270939acc2572798e581872155be4669ab47 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
James Courtier-Duttoned144f32005-05-27 23:28:27 +02004 * Version: 0.0.17
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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.
James Courtier-Duttoned144f32005-05-27 23:28:27 +020040 * 0.0.17
41 * Implement Mic and Line in Capture.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 *
43 * This code was initally based on code from ALSA's emu10k1x.c which is:
44 * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
45 *
46 * This program is free software; you can redistribute it and/or modify
47 * it under the terms of the GNU General Public License as published by
48 * the Free Software Foundation; either version 2 of the License, or
49 * (at your option) any later version.
50 *
51 * This program is distributed in the hope that it will be useful,
52 * but WITHOUT ANY WARRANTY; without even the implied warranty of
53 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
54 * GNU General Public License for more details.
55 *
56 * You should have received a copy of the GNU General Public License
57 * along with this program; if not, write to the Free Software
58 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
59 *
60 */
61#include <sound/driver.h>
62#include <linux/delay.h>
63#include <linux/init.h>
64#include <linux/interrupt.h>
65#include <linux/pci.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>
Jaroslav Kysela42750b02006-06-01 18:34:01 +020073#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75#include "ca0106.h"
76
Jaroslav Kysela42750b02006-06-01 18:34:01 +020077static DECLARE_TLV_DB_SCALE(snd_ca0106_db_scale, -5150, 75, 1);
78
Takashi Iwaie4a3d142005-11-17 14:55:40 +010079static int snd_ca0106_shared_spdif_info(struct snd_kcontrol *kcontrol,
80 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
83 uinfo->count = 1;
84 uinfo->value.integer.min = 0;
85 uinfo->value.integer.max = 1;
86 return 0;
87}
88
Takashi Iwaie4a3d142005-11-17 14:55:40 +010089static int snd_ca0106_shared_spdif_get(struct snd_kcontrol *kcontrol,
90 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Takashi Iwaie4a3d142005-11-17 14:55:40 +010092 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 ucontrol->value.enumerated.item[0] = emu->spdif_enable;
95 return 0;
96}
97
Takashi Iwaie4a3d142005-11-17 14:55:40 +010098static int snd_ca0106_shared_spdif_put(struct snd_kcontrol *kcontrol,
99 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100101 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 unsigned int val;
103 int change = 0;
104 u32 mask;
105
106 val = ucontrol->value.enumerated.item[0] ;
107 change = (emu->spdif_enable != val);
108 if (change) {
109 emu->spdif_enable = val;
110 if (val == 1) {
111 /* Digital */
112 snd_ca0106_ptr_write(emu, SPDIF_SELECT1, 0, 0xf);
113 snd_ca0106_ptr_write(emu, SPDIF_SELECT2, 0, 0x0b000000);
114 snd_ca0106_ptr_write(emu, CAPTURE_CONTROL, 0,
115 snd_ca0106_ptr_read(emu, CAPTURE_CONTROL, 0) & ~0x1000);
116 mask = inl(emu->port + GPIO) & ~0x101;
117 outl(mask, emu->port + GPIO);
118
119 } else {
120 /* Analog */
121 snd_ca0106_ptr_write(emu, SPDIF_SELECT1, 0, 0xf);
James Courtier-Dutton1f829412005-05-21 16:23:37 +0200122 snd_ca0106_ptr_write(emu, SPDIF_SELECT2, 0, 0x000f0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 snd_ca0106_ptr_write(emu, CAPTURE_CONTROL, 0,
124 snd_ca0106_ptr_read(emu, CAPTURE_CONTROL, 0) | 0x1000);
125 mask = inl(emu->port + GPIO) | 0x101;
126 outl(mask, emu->port + GPIO);
127 }
128 }
129 return change;
130}
131
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100132static int snd_ca0106_capture_source_info(struct snd_kcontrol *kcontrol,
133 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Takashi Iwai95a98262005-11-17 10:40:18 +0100135 static char *texts[6] = {
James Courtier-Dutton39596dc2005-12-16 21:59:59 +0100136 "IEC958 out", "i2s mixer out", "IEC958 in", "i2s in", "AC97 in", "SRC out"
Takashi Iwai95a98262005-11-17 10:40:18 +0100137 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
140 uinfo->count = 1;
141 uinfo->value.enumerated.items = 6;
142 if (uinfo->value.enumerated.item > 5)
143 uinfo->value.enumerated.item = 5;
144 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
145 return 0;
146}
147
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100148static int snd_ca0106_capture_source_get(struct snd_kcontrol *kcontrol,
149 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100151 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 ucontrol->value.enumerated.item[0] = emu->capture_source;
154 return 0;
155}
156
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100157static int snd_ca0106_capture_source_put(struct snd_kcontrol *kcontrol,
158 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100160 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 unsigned int val;
162 int change = 0;
163 u32 mask;
164 u32 source;
165
166 val = ucontrol->value.enumerated.item[0] ;
167 change = (emu->capture_source != val);
168 if (change) {
169 emu->capture_source = val;
170 source = (val << 28) | (val << 24) | (val << 20) | (val << 16);
171 mask = snd_ca0106_ptr_read(emu, CAPTURE_SOURCE, 0) & 0xffff;
172 snd_ca0106_ptr_write(emu, CAPTURE_SOURCE, 0, source | mask);
173 }
174 return change;
175}
176
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100177static int snd_ca0106_i2c_capture_source_info(struct snd_kcontrol *kcontrol,
178 struct snd_ctl_elem_info *uinfo)
179{
180 static char *texts[6] = {
181 "Phone", "Mic", "Line in", "Aux"
182 };
183
184 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
185 uinfo->count = 1;
186 uinfo->value.enumerated.items = 4;
187 if (uinfo->value.enumerated.item > 3)
188 uinfo->value.enumerated.item = 3;
189 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
190 return 0;
191}
192
193static int snd_ca0106_i2c_capture_source_get(struct snd_kcontrol *kcontrol,
194 struct snd_ctl_elem_value *ucontrol)
195{
196 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
197
198 ucontrol->value.enumerated.item[0] = emu->i2c_capture_source;
199 return 0;
200}
201
202static int snd_ca0106_i2c_capture_source_put(struct snd_kcontrol *kcontrol,
203 struct snd_ctl_elem_value *ucontrol)
204{
205 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
206 unsigned int source_id;
207 unsigned int ngain, ogain;
208 int change = 0;
209 u32 source;
210 /* If the capture source has changed,
211 * update the capture volume from the cached value
212 * for the particular source.
213 */
214 source_id = ucontrol->value.enumerated.item[0] ;
215 change = (emu->i2c_capture_source != source_id);
216 if (change) {
217 snd_ca0106_i2c_write(emu, ADC_MUX, 0); /* Mute input */
218 ngain = emu->i2c_capture_volume[source_id][0]; /* Left */
219 ogain = emu->i2c_capture_volume[emu->i2c_capture_source][0]; /* Left */
220 if (ngain != ogain)
221 snd_ca0106_i2c_write(emu, ADC_ATTEN_ADCL, ((ngain) & 0xff));
222 ngain = emu->i2c_capture_volume[source_id][1]; /* Left */
223 ogain = emu->i2c_capture_volume[emu->i2c_capture_source][1]; /* Left */
224 if (ngain != ogain)
225 snd_ca0106_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));
226 source = 1 << source_id;
227 snd_ca0106_i2c_write(emu, ADC_MUX, source); /* Set source */
228 emu->i2c_capture_source = source_id;
229 }
230 return change;
231}
232
James Courtier-Duttonbe0b7b02006-04-09 20:48:44 +0100233static int snd_ca0106_capture_line_in_side_out_info(struct snd_kcontrol *kcontrol,
234 struct snd_ctl_elem_info *uinfo)
235{
236 static char *texts[2] = { "Side out", "Line in" };
237
238 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
239 uinfo->count = 1;
240 uinfo->value.enumerated.items = 2;
241 if (uinfo->value.enumerated.item > 1)
242 uinfo->value.enumerated.item = 1;
243 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
244 return 0;
245}
246
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100247static int snd_ca0106_capture_mic_line_in_info(struct snd_kcontrol *kcontrol,
248 struct snd_ctl_elem_info *uinfo)
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200249{
250 static char *texts[2] = { "Line in", "Mic in" };
251
252 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
253 uinfo->count = 1;
254 uinfo->value.enumerated.items = 2;
255 if (uinfo->value.enumerated.item > 1)
256 uinfo->value.enumerated.item = 1;
257 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
258 return 0;
259}
260
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100261static int snd_ca0106_capture_mic_line_in_get(struct snd_kcontrol *kcontrol,
262 struct snd_ctl_elem_value *ucontrol)
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200263{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100264 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200265
266 ucontrol->value.enumerated.item[0] = emu->capture_mic_line_in;
267 return 0;
268}
269
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100270static int snd_ca0106_capture_mic_line_in_put(struct snd_kcontrol *kcontrol,
271 struct snd_ctl_elem_value *ucontrol)
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200272{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100273 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200274 unsigned int val;
275 int change = 0;
276 u32 tmp;
277
278 val = ucontrol->value.enumerated.item[0] ;
279 change = (emu->capture_mic_line_in != val);
280 if (change) {
281 emu->capture_mic_line_in = val;
282 if (val) {
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100283 //snd_ca0106_i2c_write(emu, ADC_MUX, 0); /* Mute input */
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200284 tmp = inl(emu->port+GPIO) & ~0x400;
285 tmp = tmp | 0x400;
286 outl(tmp, emu->port+GPIO);
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100287 //snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_MIC);
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200288 } else {
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100289 //snd_ca0106_i2c_write(emu, ADC_MUX, 0); /* Mute input */
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200290 tmp = inl(emu->port+GPIO) & ~0x400;
291 outl(tmp, emu->port+GPIO);
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100292 //snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_LINEIN);
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200293 }
294 }
295 return change;
296}
297
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100298static struct snd_kcontrol_new snd_ca0106_capture_mic_line_in __devinitdata =
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200299{
300 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100301 .name = "Shared Mic/Line in Capture Switch",
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200302 .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
305};
306
James Courtier-Duttonbe0b7b02006-04-09 20:48:44 +0100307static struct snd_kcontrol_new snd_ca0106_capture_line_in_side_out __devinitdata =
308{
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
314};
315
316
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100317static int snd_ca0106_spdif_info(struct snd_kcontrol *kcontrol,
318 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
321 uinfo->count = 1;
322 return 0;
323}
324
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100325static int snd_ca0106_spdif_get(struct snd_kcontrol *kcontrol,
326 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100328 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
330
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;
336}
337
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100338static int snd_ca0106_spdif_get_mask(struct snd_kcontrol *kcontrol,
339 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
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;
346}
347
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100348static int snd_ca0106_spdif_put(struct snd_kcontrol *kcontrol,
349 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100351 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
353 int change;
354 unsigned int val;
355
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;
364 }
365 return change;
366}
367
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100368static int snd_ca0106_volume_info(struct snd_kcontrol *kcontrol,
369 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
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;
376}
377
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100378static int snd_ca0106_volume_get(struct snd_kcontrol *kcontrol,
379 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100381 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 unsigned int value;
Takashi Iwai95a98262005-11-17 10:40:18 +0100383 int channel_id, reg;
384
385 channel_id = (kcontrol->private_value >> 8) & 0xff;
386 reg = kcontrol->private_value & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
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;
392}
393
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100394static int snd_ca0106_volume_put(struct snd_kcontrol *kcontrol,
395 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100397 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
Takashi Iwai95a98262005-11-17 10:40:18 +0100398 unsigned int oval, nval;
399 int channel_id, reg;
400
401 channel_id = (kcontrol->private_value >> 8) & 0xff;
402 reg = kcontrol->private_value & 0xff;
403
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100415static int snd_ca0106_i2c_volume_info(struct snd_kcontrol *kcontrol,
416 struct snd_ctl_elem_info *uinfo)
417{
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;
423}
424
425static int snd_ca0106_i2c_volume_get(struct snd_kcontrol *kcontrol,
426 struct snd_ctl_elem_value *ucontrol)
427{
428 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
429 int source_id;
430
431 source_id = kcontrol->private_value;
432
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;
436}
437
438static int snd_ca0106_i2c_volume_put(struct snd_kcontrol *kcontrol,
439 struct snd_ctl_elem_value *ucontrol)
440{
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;
446
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 0;
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;
457 }
458 ogain = emu->i2c_capture_volume[source_id][1]; /* Right */
459 ngain = ucontrol->value.integer.value[1];
460 if (ngain > 0xff)
461 return 0;
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;
467 }
468
469 return change;
470}
471
Takashi Iwai95a98262005-11-17 10:40:18 +0100472#define CA_VOLUME(xname,chid,reg) \
473{ \
474 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200475 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
476 SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100477 .info = snd_ca0106_volume_info, \
478 .get = snd_ca0106_volume_get, \
479 .put = snd_ca0106_volume_put, \
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200480 .tlv.p = snd_ca0106_db_scale, \
Takashi Iwai95a98262005-11-17 10:40:18 +0100481 .private_value = ((chid) << 8) | (reg) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100484#define I2C_VOLUME(xname,chid) \
485{ \
486 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
487 .info = snd_ca0106_i2c_volume_info, \
488 .get = snd_ca0106_i2c_volume_get, \
489 .put = snd_ca0106_i2c_volume_put, \
490 .private_value = chid \
491}
492
Takashi Iwai95a98262005-11-17 10:40:18 +0100493
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100494static struct snd_kcontrol_new snd_ca0106_volume_ctls[] __devinitdata = {
Takashi Iwai95a98262005-11-17 10:40:18 +0100495 CA_VOLUME("Analog Front Playback Volume",
496 CONTROL_FRONT_CHANNEL, PLAYBACK_VOLUME2),
497 CA_VOLUME("Analog Rear Playback Volume",
498 CONTROL_REAR_CHANNEL, PLAYBACK_VOLUME2),
499 CA_VOLUME("Analog Center/LFE Playback Volume",
500 CONTROL_CENTER_LFE_CHANNEL, PLAYBACK_VOLUME2),
501 CA_VOLUME("Analog Side Playback Volume",
502 CONTROL_UNKNOWN_CHANNEL, PLAYBACK_VOLUME2),
503
James Courtier-Dutton39596dc2005-12-16 21:59:59 +0100504 CA_VOLUME("IEC958 Front Playback Volume",
Takashi Iwai95a98262005-11-17 10:40:18 +0100505 CONTROL_FRONT_CHANNEL, PLAYBACK_VOLUME1),
James Courtier-Dutton39596dc2005-12-16 21:59:59 +0100506 CA_VOLUME("IEC958 Rear Playback Volume",
Takashi Iwai95a98262005-11-17 10:40:18 +0100507 CONTROL_REAR_CHANNEL, PLAYBACK_VOLUME1),
James Courtier-Dutton39596dc2005-12-16 21:59:59 +0100508 CA_VOLUME("IEC958 Center/LFE Playback Volume",
Takashi Iwai95a98262005-11-17 10:40:18 +0100509 CONTROL_CENTER_LFE_CHANNEL, PLAYBACK_VOLUME1),
James Courtier-Dutton39596dc2005-12-16 21:59:59 +0100510 CA_VOLUME("IEC958 Unknown Playback Volume",
Takashi Iwai95a98262005-11-17 10:40:18 +0100511 CONTROL_UNKNOWN_CHANNEL, PLAYBACK_VOLUME1),
512
513 CA_VOLUME("CAPTURE feedback Playback Volume",
514 1, CAPTURE_CONTROL),
515
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100516 I2C_VOLUME("Phone Capture Volume", 0),
517 I2C_VOLUME("Mic Capture Volume", 1),
518 I2C_VOLUME("Line in Capture Volume", 2),
519 I2C_VOLUME("Aux Capture Volume", 3),
520
Takashi Iwai95a98262005-11-17 10:40:18 +0100521 {
522 .access = SNDRV_CTL_ELEM_ACCESS_READ,
523 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
524 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
525 .count = 4,
526 .info = snd_ca0106_spdif_info,
527 .get = snd_ca0106_spdif_get_mask
528 },
529 {
530 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
James Courtier-Dutton39596dc2005-12-16 21:59:59 +0100531 .name = "IEC958 Playback Switch",
Takashi Iwai95a98262005-11-17 10:40:18 +0100532 .info = snd_ca0106_shared_spdif_info,
533 .get = snd_ca0106_shared_spdif_get,
534 .put = snd_ca0106_shared_spdif_put
535 },
536 {
537 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100538 .name = "Digital Capture Source",
Takashi Iwai95a98262005-11-17 10:40:18 +0100539 .info = snd_ca0106_capture_source_info,
540 .get = snd_ca0106_capture_source_get,
541 .put = snd_ca0106_capture_source_put
542 },
543 {
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100544 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
545 .name = "Capture Source",
546 .info = snd_ca0106_i2c_capture_source_info,
547 .get = snd_ca0106_i2c_capture_source_get,
548 .put = snd_ca0106_i2c_capture_source_put
549 },
550 {
Takashi Iwai95a98262005-11-17 10:40:18 +0100551 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
552 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
553 .count = 4,
554 .info = snd_ca0106_spdif_info,
555 .get = snd_ca0106_spdif_get,
556 .put = snd_ca0106_spdif_put
557 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558};
559
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100560static int __devinit remove_ctl(struct snd_card *card, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100562 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 memset(&id, 0, sizeof(id));
564 strcpy(id.name, name);
565 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
566 return snd_ctl_remove_id(card, &id);
567}
568
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100569static struct snd_kcontrol __devinit *ctl_find(struct snd_card *card, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100571 struct snd_ctl_elem_id sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 memset(&sid, 0, sizeof(sid));
573 /* FIXME: strcpy is bad. */
574 strcpy(sid.name, name);
575 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
576 return snd_ctl_find_id(card, &sid);
577}
578
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100579static int __devinit rename_ctl(struct snd_card *card, const char *src, const char *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100581 struct snd_kcontrol *kctl = ctl_find(card, src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (kctl) {
583 strcpy(kctl->id.name, dst);
584 return 0;
585 }
586 return -ENOENT;
587}
588
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100589int __devinit snd_ca0106_mixer(struct snd_ca0106 *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Takashi Iwai95a98262005-11-17 10:40:18 +0100591 int i, err;
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100592 struct snd_card *card = emu->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 char **c;
594 static char *ca0106_remove_ctls[] = {
595 "Master Mono Playback Switch",
596 "Master Mono Playback Volume",
597 "3D Control - Switch",
598 "3D Control Sigmatel - Depth",
599 "PCM Playback Switch",
600 "PCM Playback Volume",
601 "CD Playback Switch",
602 "CD Playback Volume",
603 "Phone Playback Switch",
604 "Phone Playback Volume",
605 "Video Playback Switch",
606 "Video Playback Volume",
607 "PC Speaker Playback Switch",
608 "PC Speaker Playback Volume",
609 "Mono Output Select",
610 "Capture Source",
611 "Capture Switch",
612 "Capture Volume",
613 "External Amplifier",
614 "Sigmatel 4-Speaker Stereo Playback Switch",
615 "Sigmatel Surround Phase Inversion Playback ",
616 NULL
617 };
618 static char *ca0106_rename_ctls[] = {
619 "Master Playback Switch", "Capture Switch",
620 "Master Playback Volume", "Capture Volume",
621 "Line Playback Switch", "AC97 Line Capture Switch",
622 "Line Playback Volume", "AC97 Line Capture Volume",
623 "Aux Playback Switch", "AC97 Aux Capture Switch",
624 "Aux Playback Volume", "AC97 Aux Capture Volume",
625 "Mic Playback Switch", "AC97 Mic Capture Switch",
626 "Mic Playback Volume", "AC97 Mic Capture Volume",
627 "Mic Select", "AC97 Mic Select",
628 "Mic Boost (+20dB)", "AC97 Mic Boost (+20dB)",
629 NULL
630 };
631#if 1
Takashi Iwai95a98262005-11-17 10:40:18 +0100632 for (c = ca0106_remove_ctls; *c; c++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 remove_ctl(card, *c);
Takashi Iwai95a98262005-11-17 10:40:18 +0100634 for (c = ca0106_rename_ctls; *c; c += 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 rename_ctl(card, c[0], c[1]);
636#endif
637
Takashi Iwai95a98262005-11-17 10:40:18 +0100638 for (i = 0; i < ARRAY_SIZE(snd_ca0106_volume_ctls); i++) {
639 err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_volume_ctls[i], emu));
640 if (err < 0)
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200641 return err;
642 }
Takashi Iwai95a98262005-11-17 10:40:18 +0100643 if (emu->details->i2c_adc == 1) {
James Courtier-Duttonbe0b7b02006-04-09 20:48:44 +0100644 if (emu->details->gpio_type == 1)
645 err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_capture_mic_line_in, emu));
646 else /* gpio_type == 2 */
647 err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_capture_line_in_side_out, emu));
Takashi Iwai95a98262005-11-17 10:40:18 +0100648 if (err < 0)
649 return err;
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
652}
653