blob: 0730dc7c66e570926138c4d6dcb5c7913269ba31 [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>
73
74#include "ca0106.h"
75
76static int snd_ca0106_shared_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
77{
78 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
79 uinfo->count = 1;
80 uinfo->value.integer.min = 0;
81 uinfo->value.integer.max = 1;
82 return 0;
83}
84
85static int snd_ca0106_shared_spdif_get(snd_kcontrol_t * kcontrol,
86 snd_ctl_elem_value_t * ucontrol)
87{
88 ca0106_t *emu = snd_kcontrol_chip(kcontrol);
89
90 ucontrol->value.enumerated.item[0] = emu->spdif_enable;
91 return 0;
92}
93
94static int snd_ca0106_shared_spdif_put(snd_kcontrol_t * kcontrol,
95 snd_ctl_elem_value_t * ucontrol)
96{
97 ca0106_t *emu = snd_kcontrol_chip(kcontrol);
98 unsigned int val;
99 int change = 0;
100 u32 mask;
101
102 val = ucontrol->value.enumerated.item[0] ;
103 change = (emu->spdif_enable != val);
104 if (change) {
105 emu->spdif_enable = val;
106 if (val == 1) {
107 /* Digital */
108 snd_ca0106_ptr_write(emu, SPDIF_SELECT1, 0, 0xf);
109 snd_ca0106_ptr_write(emu, SPDIF_SELECT2, 0, 0x0b000000);
110 snd_ca0106_ptr_write(emu, CAPTURE_CONTROL, 0,
111 snd_ca0106_ptr_read(emu, CAPTURE_CONTROL, 0) & ~0x1000);
112 mask = inl(emu->port + GPIO) & ~0x101;
113 outl(mask, emu->port + GPIO);
114
115 } else {
116 /* Analog */
117 snd_ca0106_ptr_write(emu, SPDIF_SELECT1, 0, 0xf);
James Courtier-Dutton1f829412005-05-21 16:23:37 +0200118 snd_ca0106_ptr_write(emu, SPDIF_SELECT2, 0, 0x000f0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 snd_ca0106_ptr_write(emu, CAPTURE_CONTROL, 0,
120 snd_ca0106_ptr_read(emu, CAPTURE_CONTROL, 0) | 0x1000);
121 mask = inl(emu->port + GPIO) | 0x101;
122 outl(mask, emu->port + GPIO);
123 }
124 }
125 return change;
126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static int snd_ca0106_capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
129{
Takashi Iwai95a98262005-11-17 10:40:18 +0100130 static char *texts[6] = {
131 "SPDIF out", "i2s mixer out", "SPDIF in", "i2s in", "AC97 in", "SRC out"
132 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
135 uinfo->count = 1;
136 uinfo->value.enumerated.items = 6;
137 if (uinfo->value.enumerated.item > 5)
138 uinfo->value.enumerated.item = 5;
139 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
140 return 0;
141}
142
143static int snd_ca0106_capture_source_get(snd_kcontrol_t * kcontrol,
144 snd_ctl_elem_value_t * ucontrol)
145{
146 ca0106_t *emu = snd_kcontrol_chip(kcontrol);
147
148 ucontrol->value.enumerated.item[0] = emu->capture_source;
149 return 0;
150}
151
152static int snd_ca0106_capture_source_put(snd_kcontrol_t * kcontrol,
153 snd_ctl_elem_value_t * ucontrol)
154{
155 ca0106_t *emu = snd_kcontrol_chip(kcontrol);
156 unsigned int val;
157 int change = 0;
158 u32 mask;
159 u32 source;
160
161 val = ucontrol->value.enumerated.item[0] ;
162 change = (emu->capture_source != val);
163 if (change) {
164 emu->capture_source = val;
165 source = (val << 28) | (val << 24) | (val << 20) | (val << 16);
166 mask = snd_ca0106_ptr_read(emu, CAPTURE_SOURCE, 0) & 0xffff;
167 snd_ca0106_ptr_write(emu, CAPTURE_SOURCE, 0, source | mask);
168 }
169 return change;
170}
171
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200172static int snd_ca0106_capture_mic_line_in_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
173{
174 static char *texts[2] = { "Line in", "Mic in" };
175
176 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
177 uinfo->count = 1;
178 uinfo->value.enumerated.items = 2;
179 if (uinfo->value.enumerated.item > 1)
180 uinfo->value.enumerated.item = 1;
181 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
182 return 0;
183}
184
185static int snd_ca0106_capture_mic_line_in_get(snd_kcontrol_t * kcontrol,
186 snd_ctl_elem_value_t * ucontrol)
187{
188 ca0106_t *emu = snd_kcontrol_chip(kcontrol);
189
190 ucontrol->value.enumerated.item[0] = emu->capture_mic_line_in;
191 return 0;
192}
193
194static int snd_ca0106_capture_mic_line_in_put(snd_kcontrol_t * kcontrol,
195 snd_ctl_elem_value_t * ucontrol)
196{
197 ca0106_t *emu = snd_kcontrol_chip(kcontrol);
198 unsigned int val;
199 int change = 0;
200 u32 tmp;
201
202 val = ucontrol->value.enumerated.item[0] ;
203 change = (emu->capture_mic_line_in != val);
204 if (change) {
205 emu->capture_mic_line_in = val;
206 if (val) {
207 snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_PHONE); /* Mute input */
208 tmp = inl(emu->port+GPIO) & ~0x400;
209 tmp = tmp | 0x400;
210 outl(tmp, emu->port+GPIO);
211 snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_MIC);
212 } else {
213 snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_PHONE); /* Mute input */
214 tmp = inl(emu->port+GPIO) & ~0x400;
215 outl(tmp, emu->port+GPIO);
216 snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_LINEIN);
217 }
218 }
219 return change;
220}
221
222static snd_kcontrol_new_t snd_ca0106_capture_mic_line_in __devinitdata =
223{
224 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
225 .name = "Mic/Line in Capture",
226 .info = snd_ca0106_capture_mic_line_in_info,
227 .get = snd_ca0106_capture_mic_line_in_get,
228 .put = snd_ca0106_capture_mic_line_in_put
229};
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static int snd_ca0106_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
232{
233 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
234 uinfo->count = 1;
235 return 0;
236}
237
238static int snd_ca0106_spdif_get(snd_kcontrol_t * kcontrol,
239 snd_ctl_elem_value_t * ucontrol)
240{
241 ca0106_t *emu = snd_kcontrol_chip(kcontrol);
242 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
243
244 ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
245 ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
246 ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
247 ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
248 return 0;
249}
250
251static int snd_ca0106_spdif_get_mask(snd_kcontrol_t * kcontrol,
252 snd_ctl_elem_value_t * ucontrol)
253{
254 ucontrol->value.iec958.status[0] = 0xff;
255 ucontrol->value.iec958.status[1] = 0xff;
256 ucontrol->value.iec958.status[2] = 0xff;
257 ucontrol->value.iec958.status[3] = 0xff;
258 return 0;
259}
260
261static int snd_ca0106_spdif_put(snd_kcontrol_t * kcontrol,
262 snd_ctl_elem_value_t * ucontrol)
263{
264 ca0106_t *emu = snd_kcontrol_chip(kcontrol);
265 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
266 int change;
267 unsigned int val;
268
269 val = (ucontrol->value.iec958.status[0] << 0) |
270 (ucontrol->value.iec958.status[1] << 8) |
271 (ucontrol->value.iec958.status[2] << 16) |
272 (ucontrol->value.iec958.status[3] << 24);
273 change = val != emu->spdif_bits[idx];
274 if (change) {
275 snd_ca0106_ptr_write(emu, SPCS0 + idx, 0, val);
276 emu->spdif_bits[idx] = val;
277 }
278 return change;
279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static int snd_ca0106_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
282{
283 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
284 uinfo->count = 2;
285 uinfo->value.integer.min = 0;
286 uinfo->value.integer.max = 255;
287 return 0;
288}
289
290static int snd_ca0106_volume_get(snd_kcontrol_t * kcontrol,
Takashi Iwai95a98262005-11-17 10:40:18 +0100291 snd_ctl_elem_value_t * ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 ca0106_t *emu = snd_kcontrol_chip(kcontrol);
294 unsigned int value;
Takashi Iwai95a98262005-11-17 10:40:18 +0100295 int channel_id, reg;
296
297 channel_id = (kcontrol->private_value >> 8) & 0xff;
298 reg = kcontrol->private_value & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 value = snd_ca0106_ptr_read(emu, reg, channel_id);
301 ucontrol->value.integer.value[0] = 0xff - ((value >> 24) & 0xff); /* Left */
302 ucontrol->value.integer.value[1] = 0xff - ((value >> 16) & 0xff); /* Right */
303 return 0;
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static int snd_ca0106_volume_put(snd_kcontrol_t * kcontrol,
Takashi Iwai95a98262005-11-17 10:40:18 +0100307 snd_ctl_elem_value_t * ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 ca0106_t *emu = snd_kcontrol_chip(kcontrol);
Takashi Iwai95a98262005-11-17 10:40:18 +0100310 unsigned int oval, nval;
311 int channel_id, reg;
312
313 channel_id = (kcontrol->private_value >> 8) & 0xff;
314 reg = kcontrol->private_value & 0xff;
315
316 oval = snd_ca0106_ptr_read(emu, reg, channel_id);
317 nval = ((0xff - ucontrol->value.integer.value[0]) << 24) |
318 ((0xff - ucontrol->value.integer.value[1]) << 16);
319 nval |= ((0xff - ucontrol->value.integer.value[0]) << 8) |
320 ((0xff - ucontrol->value.integer.value[1]) );
321 if (oval == nval)
322 return 0;
323 snd_ca0106_ptr_write(emu, reg, channel_id, nval);
324 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Takashi Iwai95a98262005-11-17 10:40:18 +0100327#define CA_VOLUME(xname,chid,reg) \
328{ \
329 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
330 .info = snd_ca0106_volume_info, \
331 .get = snd_ca0106_volume_get, \
332 .put = snd_ca0106_volume_put, \
333 .private_value = ((chid) << 8) | (reg) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Takashi Iwai95a98262005-11-17 10:40:18 +0100336
337static snd_kcontrol_new_t snd_ca0106_volume_ctls[] __devinitdata = {
338 CA_VOLUME("Analog Front Playback Volume",
339 CONTROL_FRONT_CHANNEL, PLAYBACK_VOLUME2),
340 CA_VOLUME("Analog Rear Playback Volume",
341 CONTROL_REAR_CHANNEL, PLAYBACK_VOLUME2),
342 CA_VOLUME("Analog Center/LFE Playback Volume",
343 CONTROL_CENTER_LFE_CHANNEL, PLAYBACK_VOLUME2),
344 CA_VOLUME("Analog Side Playback Volume",
345 CONTROL_UNKNOWN_CHANNEL, PLAYBACK_VOLUME2),
346
347 CA_VOLUME("SPDIF Front Playback Volume",
348 CONTROL_FRONT_CHANNEL, PLAYBACK_VOLUME1),
349 CA_VOLUME("SPDIF Rear Playback Volume",
350 CONTROL_REAR_CHANNEL, PLAYBACK_VOLUME1),
351 CA_VOLUME("SPDIF Center/LFE Playback Volume",
352 CONTROL_CENTER_LFE_CHANNEL, PLAYBACK_VOLUME1),
353 CA_VOLUME("SPDIF Unknown Playback Volume",
354 CONTROL_UNKNOWN_CHANNEL, PLAYBACK_VOLUME1),
355
356 CA_VOLUME("CAPTURE feedback Playback Volume",
357 1, CAPTURE_CONTROL),
358
359 {
360 .access = SNDRV_CTL_ELEM_ACCESS_READ,
361 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
362 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
363 .count = 4,
364 .info = snd_ca0106_spdif_info,
365 .get = snd_ca0106_spdif_get_mask
366 },
367 {
368 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
369 .name = "SPDIF Out",
370 .info = snd_ca0106_shared_spdif_info,
371 .get = snd_ca0106_shared_spdif_get,
372 .put = snd_ca0106_shared_spdif_put
373 },
374 {
375 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
376 .name = "Capture Source",
377 .info = snd_ca0106_capture_source_info,
378 .get = snd_ca0106_capture_source_get,
379 .put = snd_ca0106_capture_source_put
380 },
381 {
382 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
383 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
384 .count = 4,
385 .info = snd_ca0106_spdif_info,
386 .get = snd_ca0106_spdif_get,
387 .put = snd_ca0106_spdif_put
388 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389};
390
Takashi Iwai95a98262005-11-17 10:40:18 +0100391static int __devinit remove_ctl(snd_card_t *card, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 snd_ctl_elem_id_t id;
394 memset(&id, 0, sizeof(id));
395 strcpy(id.name, name);
396 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
397 return snd_ctl_remove_id(card, &id);
398}
399
Takashi Iwai95a98262005-11-17 10:40:18 +0100400static snd_kcontrol_t __devinit *ctl_find(snd_card_t *card, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 snd_ctl_elem_id_t sid;
403 memset(&sid, 0, sizeof(sid));
404 /* FIXME: strcpy is bad. */
405 strcpy(sid.name, name);
406 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
407 return snd_ctl_find_id(card, &sid);
408}
409
Takashi Iwai95a98262005-11-17 10:40:18 +0100410static int __devinit rename_ctl(snd_card_t *card, const char *src, const char *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 snd_kcontrol_t *kctl = ctl_find(card, src);
413 if (kctl) {
414 strcpy(kctl->id.name, dst);
415 return 0;
416 }
417 return -ENOENT;
418}
419
420int __devinit snd_ca0106_mixer(ca0106_t *emu)
421{
Takashi Iwai95a98262005-11-17 10:40:18 +0100422 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 snd_card_t *card = emu->card;
424 char **c;
425 static char *ca0106_remove_ctls[] = {
426 "Master Mono Playback Switch",
427 "Master Mono Playback Volume",
428 "3D Control - Switch",
429 "3D Control Sigmatel - Depth",
430 "PCM Playback Switch",
431 "PCM Playback Volume",
432 "CD Playback Switch",
433 "CD Playback Volume",
434 "Phone Playback Switch",
435 "Phone Playback Volume",
436 "Video Playback Switch",
437 "Video Playback Volume",
438 "PC Speaker Playback Switch",
439 "PC Speaker Playback Volume",
440 "Mono Output Select",
441 "Capture Source",
442 "Capture Switch",
443 "Capture Volume",
444 "External Amplifier",
445 "Sigmatel 4-Speaker Stereo Playback Switch",
446 "Sigmatel Surround Phase Inversion Playback ",
447 NULL
448 };
449 static char *ca0106_rename_ctls[] = {
450 "Master Playback Switch", "Capture Switch",
451 "Master Playback Volume", "Capture Volume",
452 "Line Playback Switch", "AC97 Line Capture Switch",
453 "Line Playback Volume", "AC97 Line Capture Volume",
454 "Aux Playback Switch", "AC97 Aux Capture Switch",
455 "Aux Playback Volume", "AC97 Aux Capture Volume",
456 "Mic Playback Switch", "AC97 Mic Capture Switch",
457 "Mic Playback Volume", "AC97 Mic Capture Volume",
458 "Mic Select", "AC97 Mic Select",
459 "Mic Boost (+20dB)", "AC97 Mic Boost (+20dB)",
460 NULL
461 };
462#if 1
Takashi Iwai95a98262005-11-17 10:40:18 +0100463 for (c = ca0106_remove_ctls; *c; c++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 remove_ctl(card, *c);
Takashi Iwai95a98262005-11-17 10:40:18 +0100465 for (c = ca0106_rename_ctls; *c; c += 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 rename_ctl(card, c[0], c[1]);
467#endif
468
Takashi Iwai95a98262005-11-17 10:40:18 +0100469 for (i = 0; i < ARRAY_SIZE(snd_ca0106_volume_ctls); i++) {
470 err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_volume_ctls[i], emu));
471 if (err < 0)
James Courtier-Duttoned144f32005-05-27 23:28:27 +0200472 return err;
473 }
Takashi Iwai95a98262005-11-17 10:40:18 +0100474 if (emu->details->i2c_adc == 1) {
475 err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_capture_mic_line_in, emu));
476 if (err < 0)
477 return err;
478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480}
481