blob: 3025ed1b6e1efd1444ccf5d3403d4ca7ab36627c [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
Trent Piephob18cd532007-07-24 12:06:16 +02004 * Version: 0.0.18
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.
Trent Piephob18cd532007-07-24 12:06:16 +020042 * 0.0.18
43 * Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
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>
47 *
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.
52 *
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.
57 *
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
61 *
62 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/delay.h>
64#include <linux/init.h>
65#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#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>
Jean Delvare6473d162007-03-06 02:45:12 -080074#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#include "ca0106.h"
77
Takashi Iwai0cb29ea2007-01-29 15:33:49 +010078static const DECLARE_TLV_DB_SCALE(snd_ca0106_db_scale1, -5175, 25, 1);
79static const DECLARE_TLV_DB_SCALE(snd_ca0106_db_scale2, -10350, 50, 1);
Jaroslav Kysela42750b02006-06-01 18:34:01 +020080
Takashi Iwaia5ce8892007-07-23 15:42:26 +020081#define snd_ca0106_shared_spdif_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Takashi Iwaie4a3d142005-11-17 14:55:40 +010083static int snd_ca0106_shared_spdif_get(struct snd_kcontrol *kcontrol,
84 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Takashi Iwaie4a3d142005-11-17 14:55:40 +010086 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Takashi Iwai5fe619f2007-11-15 14:42:34 +010088 ucontrol->value.integer.value[0] = emu->spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return 0;
90}
91
Takashi Iwaie4a3d142005-11-17 14:55:40 +010092static int snd_ca0106_shared_spdif_put(struct snd_kcontrol *kcontrol,
93 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Takashi Iwaie4a3d142005-11-17 14:55:40 +010095 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 unsigned int val;
97 int change = 0;
98 u32 mask;
99
Takashi Iwai5fe619f2007-11-15 14:42:34 +0100100 val = !!ucontrol->value.integer.value[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 change = (emu->spdif_enable != val);
102 if (change) {
103 emu->spdif_enable = val;
Takashi Iwai5fe619f2007-11-15 14:42:34 +0100104 if (val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /* 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);
112
113 } else {
114 /* Analog */
115 snd_ca0106_ptr_write(emu, SPDIF_SELECT1, 0, 0xf);
James Courtier-Dutton1f829412005-05-21 16:23:37 +0200116 snd_ca0106_ptr_write(emu, SPDIF_SELECT2, 0, 0x000f0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 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);
121 }
122 }
123 return change;
124}
125
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100126static int snd_ca0106_capture_source_info(struct snd_kcontrol *kcontrol,
127 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Takashi Iwai95a98262005-11-17 10:40:18 +0100129 static char *texts[6] = {
James Courtier-Dutton39596dc2005-12-16 21:59:59 +0100130 "IEC958 out", "i2s mixer out", "IEC958 in", "i2s in", "AC97 in", "SRC out"
Takashi Iwai95a98262005-11-17 10:40:18 +0100131 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
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;
140}
141
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100142static int snd_ca0106_capture_source_get(struct snd_kcontrol *kcontrol,
143 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100145 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 ucontrol->value.enumerated.item[0] = emu->capture_source;
148 return 0;
149}
150
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100151static int snd_ca0106_capture_source_put(struct snd_kcontrol *kcontrol,
152 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100154 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 unsigned int val;
156 int change = 0;
157 u32 mask;
158 u32 source;
159
160 val = ucontrol->value.enumerated.item[0] ;
Takashi Iwai5fe619f2007-11-15 14:42:34 +0100161 if (val >= 6)
162 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 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);
169 }
170 return change;
171}
172
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100173static int snd_ca0106_i2c_capture_source_info(struct snd_kcontrol *kcontrol,
174 struct snd_ctl_elem_info *uinfo)
175{
176 static char *texts[6] = {
177 "Phone", "Mic", "Line in", "Aux"
178 };
179
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;
187}
188
189static int snd_ca0106_i2c_capture_source_get(struct snd_kcontrol *kcontrol,
190 struct snd_ctl_elem_value *ucontrol)
191{
192 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
193
194 ucontrol->value.enumerated.item[0] = emu->i2c_capture_source;
195 return 0;
196}
197
198static int snd_ca0106_i2c_capture_source_put(struct snd_kcontrol *kcontrol,
199 struct snd_ctl_elem_value *ucontrol)
200{
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.
209 */
210 source_id = ucontrol->value.enumerated.item[0] ;
Takashi Iwai5fe619f2007-11-15 14:42:34 +0100211 if (source_id >= 4)
212 return -EINVAL;
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100213 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;
227 }
228 return change;
229}
230
James Courtier-Duttonbe0b7b02006-04-09 20:48:44 +0100231static int snd_ca0106_capture_line_in_side_out_info(struct snd_kcontrol *kcontrol,
232 struct snd_ctl_elem_info *uinfo)
233{
234 static char *texts[2] = { "Side out", "Line in" };
235
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;
243}
244
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100245static int snd_ca0106_capture_mic_line_in_info(struct snd_kcontrol *kcontrol,
246 struct snd_ctl_elem_info *uinfo)
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200247{
248 static char *texts[2] = { "Line in", "Mic in" };
249
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;
257}
258
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100259static int snd_ca0106_capture_mic_line_in_get(struct snd_kcontrol *kcontrol,
260 struct snd_ctl_elem_value *ucontrol)
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200261{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100262 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200263
264 ucontrol->value.enumerated.item[0] = emu->capture_mic_line_in;
265 return 0;
266}
267
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100268static int snd_ca0106_capture_mic_line_in_put(struct snd_kcontrol *kcontrol,
269 struct snd_ctl_elem_value *ucontrol)
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200270{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100271 struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200272 unsigned int val;
273 int change = 0;
274 u32 tmp;
275
276 val = ucontrol->value.enumerated.item[0] ;
Takashi Iwai5fe619f2007-11-15 14:42:34 +0100277 if (val > 1)
278 return -EINVAL;
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200279 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)
Takashi Iwai5fe619f2007-11-15 14:42:34 +0100451 return -EINVAL;
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100452 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)
Takashi Iwai5fe619f2007-11-15 14:42:34 +0100461 return -EINVAL;
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100462 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
Trent Piephob18cd532007-07-24 12:06:16 +0200472#define spi_mute_info snd_ctl_boolean_mono_info
473
474static int spi_mute_get(struct snd_kcontrol *kcontrol,
475 struct snd_ctl_elem_value *ucontrol)
476{
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;
480
481 ucontrol->value.integer.value[0] = !(emu->spi_dac_reg[reg] & bit);
482 return 0;
483}
484
485static int spi_mute_put(struct snd_kcontrol *kcontrol,
486 struct snd_ctl_elem_value *ucontrol)
487{
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;
492
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;
502 }
503
504 ret = snd_ca0106_spi_write(emu, emu->spi_dac_reg[reg]);
Takashi Iwai5fe619f2007-11-15 14:42:34 +0100505 return ret ? -EINVAL : 1;
Trent Piephob18cd532007-07-24 12:06:16 +0200506}
507
Takashi Iwai95a98262005-11-17 10:40:18 +0100508#define CA_VOLUME(xname,chid,reg) \
509{ \
510 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
Jaroslav Kysela302e9c52006-07-05 17:39:49 +0200511 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
512 SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100513 .info = snd_ca0106_volume_info, \
514 .get = snd_ca0106_volume_get, \
515 .put = snd_ca0106_volume_put, \
Takashi Iwai7cf0a952006-08-17 16:23:07 +0200516 .tlv = { .p = snd_ca0106_db_scale1 }, \
Takashi Iwai95a98262005-11-17 10:40:18 +0100517 .private_value = ((chid) << 8) | (reg) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100520static struct snd_kcontrol_new snd_ca0106_volume_ctls[] __devinitdata = {
Takashi Iwai95a98262005-11-17 10:40:18 +0100521 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),
529
James Courtier-Dutton39596dc2005-12-16 21:59:59 +0100530 CA_VOLUME("IEC958 Front Playback Volume",
Takashi Iwai95a98262005-11-17 10:40:18 +0100531 CONTROL_FRONT_CHANNEL, PLAYBACK_VOLUME1),
James Courtier-Dutton39596dc2005-12-16 21:59:59 +0100532 CA_VOLUME("IEC958 Rear Playback Volume",
Takashi Iwai95a98262005-11-17 10:40:18 +0100533 CONTROL_REAR_CHANNEL, PLAYBACK_VOLUME1),
James Courtier-Dutton39596dc2005-12-16 21:59:59 +0100534 CA_VOLUME("IEC958 Center/LFE Playback Volume",
Takashi Iwai95a98262005-11-17 10:40:18 +0100535 CONTROL_CENTER_LFE_CHANNEL, PLAYBACK_VOLUME1),
James Courtier-Dutton39596dc2005-12-16 21:59:59 +0100536 CA_VOLUME("IEC958 Unknown Playback Volume",
Takashi Iwai95a98262005-11-17 10:40:18 +0100537 CONTROL_UNKNOWN_CHANNEL, PLAYBACK_VOLUME1),
538
539 CA_VOLUME("CAPTURE feedback Playback Volume",
540 1, CAPTURE_CONTROL),
541
542 {
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
549 },
550 {
551 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
James Courtier-Dutton39596dc2005-12-16 21:59:59 +0100552 .name = "IEC958 Playback Switch",
Takashi Iwai95a98262005-11-17 10:40:18 +0100553 .info = snd_ca0106_shared_spdif_info,
554 .get = snd_ca0106_shared_spdif_get,
555 .put = snd_ca0106_shared_spdif_put
556 },
557 {
558 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
James Courtier-Duttone6327cf2006-11-11 10:52:06 +0000559 .name = "Digital Source Capture Enum",
Takashi Iwai95a98262005-11-17 10:40:18 +0100560 .info = snd_ca0106_capture_source_info,
561 .get = snd_ca0106_capture_source_get,
562 .put = snd_ca0106_capture_source_put
563 },
564 {
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100565 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
James Courtier-Duttone6327cf2006-11-11 10:52:06 +0000566 .name = "Analog Source Capture Enum",
James Courtier-Dutton6129daa2006-04-09 13:01:34 +0100567 .info = snd_ca0106_i2c_capture_source_info,
568 .get = snd_ca0106_i2c_capture_source_get,
569 .put = snd_ca0106_i2c_capture_source_put
570 },
571 {
Takashi Iwai95a98262005-11-17 10:40:18 +0100572 .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
578 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579};
580
James Courtier-Dutton7c157062006-12-10 00:00:38 +0000581#define I2C_VOLUME(xname,chid) \
582{ \
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 \
591}
592
593static 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),
598};
599
Trent Piephob18cd532007-07-24 12:06:16 +0200600#define SPI_SWITCH(xname,reg,bit) \
601{ \
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, \
Trent Piepho18b5d322007-07-25 18:40:39 +0200607 .private_value = (reg<<SPI_REG_SHIFT) | (bit) \
Trent Piephob18cd532007-07-24 12:06:16 +0200608}
609
610static 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),
620};
621
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100622static int __devinit remove_ctl(struct snd_card *card, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100624 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 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);
629}
630
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100631static struct snd_kcontrol __devinit *ctl_find(struct snd_card *card, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100633 struct snd_ctl_elem_id sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 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);
639}
640
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100641static int __devinit rename_ctl(struct snd_card *card, const char *src, const char *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100643 struct snd_kcontrol *kctl = ctl_find(card, src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (kctl) {
645 strcpy(kctl->id.name, dst);
646 return 0;
647 }
648 return -ENOENT;
649}
650
Trent Piephofca7f382007-07-24 12:10:34 +0200651#define ADD_CTLS(emu, ctls) \
652 do { \
Harvey Harrisonbed515b2008-02-28 12:02:56 +0100653 int i, _err; \
Trent Piephofca7f382007-07-24 12:10:34 +0200654 for (i = 0; i < ARRAY_SIZE(ctls); i++) { \
Harvey Harrisonbed515b2008-02-28 12:02:56 +0100655 _err = snd_ctl_add(card, snd_ctl_new1(&ctls[i], emu)); \
656 if (_err < 0) \
657 return _err; \
Trent Piephofca7f382007-07-24 12:10:34 +0200658 } \
659 } while (0)
660
Takashi Iwai49c88b82008-02-18 13:06:49 +0100661static __devinitdata
662DECLARE_TLV_DB_SCALE(snd_ca0106_master_db_scale, -6375, 50, 1);
663
664static 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
675};
676
677static 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
684};
685
686static void __devinit add_slaves(struct snd_card *card,
687 struct snd_kcontrol *master, char **list)
688{
689 for (; *list; list++) {
690 struct snd_kcontrol *slave = ctl_find(card, *list);
691 if (slave)
692 snd_ctl_add_slave(master, slave);
693 }
694}
695
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100696int __devinit snd_ca0106_mixer(struct snd_ca0106 *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Trent Piephofca7f382007-07-24 12:10:34 +0200698 int err;
Takashi Iwaie4a3d142005-11-17 14:55:40 +0100699 struct snd_card *card = emu->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 char **c;
Takashi Iwai49c88b82008-02-18 13:06:49 +0100701 struct snd_kcontrol *vmaster;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 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
725 };
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
738 };
739#if 1
Takashi Iwai95a98262005-11-17 10:40:18 +0100740 for (c = ca0106_remove_ctls; *c; c++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 remove_ctl(card, *c);
Takashi Iwai95a98262005-11-17 10:40:18 +0100742 for (c = ca0106_rename_ctls; *c; c += 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 rename_ctl(card, c[0], c[1]);
744#endif
745
Trent Piephofca7f382007-07-24 12:10:34 +0200746 ADD_CTLS(emu, snd_ca0106_volume_ctls);
Takashi Iwai95a98262005-11-17 10:40:18 +0100747 if (emu->details->i2c_adc == 1) {
Trent Piephofca7f382007-07-24 12:10:34 +0200748 ADD_CTLS(emu, snd_ca0106_volume_i2c_adc_ctls);
James Courtier-Duttonbe0b7b02006-04-09 20:48:44 +0100749 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));
Takashi Iwai95a98262005-11-17 10:40:18 +0100753 if (err < 0)
754 return err;
755 }
Trent Piephofca7f382007-07-24 12:10:34 +0200756 if (emu->details->spi_dac == 1)
757 ADD_CTLS(emu, snd_ca0106_volume_spi_dac_ctls);
Takashi Iwai49c88b82008-02-18 13:06:49 +0100758
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);
765
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);
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return 0;
774}
775