Merge branch 'media_fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[cris-mirror.git] / sound / pci / ctxfi / ctatc.c
blob1bff80cde0a2f88000eaa1711867b882adcae433
1 /**
2 * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
4 * This source file is released under GPL v2 license (no other versions).
5 * See the COPYING file included in the main directory of this source
6 * distribution for the license terms and conditions.
8 * @File ctatc.c
10 * @Brief
11 * This file contains the implementation of the device resource management
12 * object.
14 * @Author Liu Chun
15 * @Date Mar 28 2008
18 #include "ctatc.h"
19 #include "ctpcm.h"
20 #include "ctmixer.h"
21 #include "cthardware.h"
22 #include "ctsrc.h"
23 #include "ctamixer.h"
24 #include "ctdaio.h"
25 #include "cttimer.h"
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <sound/pcm.h>
29 #include <sound/control.h>
30 #include <sound/asoundef.h>
32 #define MONO_SUM_SCALE 0x19a8 /* 2^(-0.5) in 14-bit floating format */
33 #define DAIONUM 7
34 #define MAX_MULTI_CHN 8
36 #define IEC958_DEFAULT_CON ((IEC958_AES0_NONAUDIO \
37 | IEC958_AES0_CON_NOT_COPYRIGHT) \
38 | ((IEC958_AES1_CON_MIXER \
39 | IEC958_AES1_CON_ORIGINAL) << 8) \
40 | (0x10 << 16) \
41 | ((IEC958_AES3_CON_FS_48000) << 24))
43 static struct snd_pci_quirk __devinitdata subsys_20k1_list[] = {
44 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0022, "SB055x", CTSB055X),
45 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x002f, "SB055x", CTSB055X),
46 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0029, "SB073x", CTSB073X),
47 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0031, "SB073x", CTSB073X),
48 SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000, 0x6000,
49 "UAA", CTUAA),
50 { } /* terminator */
53 static struct snd_pci_quirk __devinitdata subsys_20k2_list[] = {
54 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB0760,
55 "SB0760", CTSB0760),
56 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08801,
57 "SB0880", CTSB0880),
58 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08802,
59 "SB0880", CTSB0880),
60 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08803,
61 "SB0880", CTSB0880),
62 SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000,
63 PCI_SUBDEVICE_ID_CREATIVE_HENDRIX, "HENDRIX",
64 CTHENDRIX),
65 { } /* terminator */
68 static const char *ct_subsys_name[NUM_CTCARDS] = {
69 /* 20k1 models */
70 [CTSB055X] = "SB055x",
71 [CTSB073X] = "SB073x",
72 [CTUAA] = "UAA",
73 [CT20K1_UNKNOWN] = "Unknown",
74 /* 20k2 models */
75 [CTSB0760] = "SB076x",
76 [CTHENDRIX] = "Hendrix",
77 [CTSB0880] = "SB0880",
78 [CT20K2_UNKNOWN] = "Unknown",
81 static struct {
82 int (*create)(struct ct_atc *atc,
83 enum CTALSADEVS device, const char *device_name);
84 int (*destroy)(void *alsa_dev);
85 const char *public_name;
86 } alsa_dev_funcs[NUM_CTALSADEVS] = {
87 [FRONT] = { .create = ct_alsa_pcm_create,
88 .destroy = NULL,
89 .public_name = "Front/WaveIn"},
90 [SURROUND] = { .create = ct_alsa_pcm_create,
91 .destroy = NULL,
92 .public_name = "Surround"},
93 [CLFE] = { .create = ct_alsa_pcm_create,
94 .destroy = NULL,
95 .public_name = "Center/LFE"},
96 [SIDE] = { .create = ct_alsa_pcm_create,
97 .destroy = NULL,
98 .public_name = "Side"},
99 [IEC958] = { .create = ct_alsa_pcm_create,
100 .destroy = NULL,
101 .public_name = "IEC958 Non-audio"},
103 [MIXER] = { .create = ct_alsa_mix_create,
104 .destroy = NULL,
105 .public_name = "Mixer"}
108 typedef int (*create_t)(void *, void **);
109 typedef int (*destroy_t)(void *);
111 static struct {
112 int (*create)(void *hw, void **rmgr);
113 int (*destroy)(void *mgr);
114 } rsc_mgr_funcs[NUM_RSCTYP] = {
115 [SRC] = { .create = (create_t)src_mgr_create,
116 .destroy = (destroy_t)src_mgr_destroy },
117 [SRCIMP] = { .create = (create_t)srcimp_mgr_create,
118 .destroy = (destroy_t)srcimp_mgr_destroy },
119 [AMIXER] = { .create = (create_t)amixer_mgr_create,
120 .destroy = (destroy_t)amixer_mgr_destroy },
121 [SUM] = { .create = (create_t)sum_mgr_create,
122 .destroy = (destroy_t)sum_mgr_destroy },
123 [DAIO] = { .create = (create_t)daio_mgr_create,
124 .destroy = (destroy_t)daio_mgr_destroy }
127 static int
128 atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
130 /* *
131 * Only mono and interleaved modes are supported now.
132 * Always allocates a contiguous channel block.
133 * */
135 static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
137 struct snd_pcm_runtime *runtime;
138 struct ct_vm *vm;
140 if (!apcm->substream)
141 return 0;
143 runtime = apcm->substream->runtime;
144 vm = atc->vm;
146 apcm->vm_block = vm->map(vm, apcm->substream, runtime->dma_bytes);
148 if (!apcm->vm_block)
149 return -ENOENT;
151 return 0;
154 static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
156 struct ct_vm *vm;
158 if (!apcm->vm_block)
159 return;
161 vm = atc->vm;
163 vm->unmap(vm, apcm->vm_block);
165 apcm->vm_block = NULL;
168 static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
170 return atc->vm->get_ptp_phys(atc->vm, index);
173 static unsigned int convert_format(snd_pcm_format_t snd_format)
175 switch (snd_format) {
176 case SNDRV_PCM_FORMAT_U8:
177 return SRC_SF_U8;
178 case SNDRV_PCM_FORMAT_S16_LE:
179 return SRC_SF_S16;
180 case SNDRV_PCM_FORMAT_S24_3LE:
181 return SRC_SF_S24;
182 case SNDRV_PCM_FORMAT_S32_LE:
183 return SRC_SF_S32;
184 case SNDRV_PCM_FORMAT_FLOAT_LE:
185 return SRC_SF_F32;
186 default:
187 printk(KERN_ERR "ctxfi: not recognized snd format is %d \n",
188 snd_format);
189 return SRC_SF_S16;
193 static unsigned int
194 atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
196 unsigned int pitch;
197 int b;
199 /* get pitch and convert to fixed-point 8.24 format. */
200 pitch = (input_rate / output_rate) << 24;
201 input_rate %= output_rate;
202 input_rate /= 100;
203 output_rate /= 100;
204 for (b = 31; ((b >= 0) && !(input_rate >> b)); )
205 b--;
207 if (b >= 0) {
208 input_rate <<= (31 - b);
209 input_rate /= output_rate;
210 b = 24 - (31 - b);
211 if (b >= 0)
212 input_rate <<= b;
213 else
214 input_rate >>= -b;
216 pitch |= input_rate;
219 return pitch;
222 static int select_rom(unsigned int pitch)
224 if (pitch > 0x00428f5c && pitch < 0x01b851ec) {
225 /* 0.26 <= pitch <= 1.72 */
226 return 1;
227 } else if (pitch == 0x01d66666 || pitch == 0x01d66667) {
228 /* pitch == 1.8375 */
229 return 2;
230 } else if (pitch == 0x02000000) {
231 /* pitch == 2 */
232 return 3;
233 } else if (pitch <= 0x08000000) {
234 /* 0 <= pitch <= 8 */
235 return 0;
236 } else {
237 return -ENOENT;
241 static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
243 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
244 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
245 struct src_desc desc = {0};
246 struct amixer_desc mix_dsc = {0};
247 struct src *src;
248 struct amixer *amixer;
249 int err;
250 int n_amixer = apcm->substream->runtime->channels, i = 0;
251 int device = apcm->substream->pcm->device;
252 unsigned int pitch;
254 /* first release old resources */
255 atc_pcm_release_resources(atc, apcm);
257 /* Get SRC resource */
258 desc.multi = apcm->substream->runtime->channels;
259 desc.msr = atc->msr;
260 desc.mode = MEMRD;
261 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
262 if (err)
263 goto error1;
265 pitch = atc_get_pitch(apcm->substream->runtime->rate,
266 (atc->rsr * atc->msr));
267 src = apcm->src;
268 src->ops->set_pitch(src, pitch);
269 src->ops->set_rom(src, select_rom(pitch));
270 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
271 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
273 /* Get AMIXER resource */
274 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
275 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
276 if (!apcm->amixers) {
277 err = -ENOMEM;
278 goto error1;
280 mix_dsc.msr = atc->msr;
281 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
282 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
283 (struct amixer **)&apcm->amixers[i]);
284 if (err)
285 goto error1;
287 apcm->n_amixer++;
290 /* Set up device virtual mem map */
291 err = ct_map_audio_buffer(atc, apcm);
292 if (err < 0)
293 goto error1;
295 /* Connect resources */
296 src = apcm->src;
297 for (i = 0; i < n_amixer; i++) {
298 amixer = apcm->amixers[i];
299 mutex_lock(&atc->atc_mutex);
300 amixer->ops->setup(amixer, &src->rsc,
301 INIT_VOL, atc->pcm[i+device*2]);
302 mutex_unlock(&atc->atc_mutex);
303 src = src->ops->next_interleave(src);
304 if (!src)
305 src = apcm->src;
308 ct_timer_prepare(apcm->timer);
310 return 0;
312 error1:
313 atc_pcm_release_resources(atc, apcm);
314 return err;
317 static int
318 atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
320 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
321 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
322 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
323 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
324 struct srcimp *srcimp;
325 int i;
327 if (apcm->srcimps) {
328 for (i = 0; i < apcm->n_srcimp; i++) {
329 srcimp = apcm->srcimps[i];
330 srcimp->ops->unmap(srcimp);
331 srcimp_mgr->put_srcimp(srcimp_mgr, srcimp);
332 apcm->srcimps[i] = NULL;
334 kfree(apcm->srcimps);
335 apcm->srcimps = NULL;
338 if (apcm->srccs) {
339 for (i = 0; i < apcm->n_srcc; i++) {
340 src_mgr->put_src(src_mgr, apcm->srccs[i]);
341 apcm->srccs[i] = NULL;
343 kfree(apcm->srccs);
344 apcm->srccs = NULL;
347 if (apcm->amixers) {
348 for (i = 0; i < apcm->n_amixer; i++) {
349 amixer_mgr->put_amixer(amixer_mgr, apcm->amixers[i]);
350 apcm->amixers[i] = NULL;
352 kfree(apcm->amixers);
353 apcm->amixers = NULL;
356 if (apcm->mono) {
357 sum_mgr->put_sum(sum_mgr, apcm->mono);
358 apcm->mono = NULL;
361 if (apcm->src) {
362 src_mgr->put_src(src_mgr, apcm->src);
363 apcm->src = NULL;
366 if (apcm->vm_block) {
367 /* Undo device virtual mem map */
368 ct_unmap_audio_buffer(atc, apcm);
369 apcm->vm_block = NULL;
372 return 0;
375 static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
377 unsigned int max_cisz;
378 struct src *src = apcm->src;
380 if (apcm->started)
381 return 0;
382 apcm->started = 1;
384 max_cisz = src->multi * src->rsc.msr;
385 max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
387 src->ops->set_sa(src, apcm->vm_block->addr);
388 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
389 src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
390 src->ops->set_cisz(src, max_cisz);
392 src->ops->set_bm(src, 1);
393 src->ops->set_state(src, SRC_STATE_INIT);
394 src->ops->commit_write(src);
396 ct_timer_start(apcm->timer);
397 return 0;
400 static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
402 struct src *src;
403 int i;
405 ct_timer_stop(apcm->timer);
407 src = apcm->src;
408 src->ops->set_bm(src, 0);
409 src->ops->set_state(src, SRC_STATE_OFF);
410 src->ops->commit_write(src);
412 if (apcm->srccs) {
413 for (i = 0; i < apcm->n_srcc; i++) {
414 src = apcm->srccs[i];
415 src->ops->set_bm(src, 0);
416 src->ops->set_state(src, SRC_STATE_OFF);
417 src->ops->commit_write(src);
421 apcm->started = 0;
423 return 0;
426 static int
427 atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
429 struct src *src = apcm->src;
430 u32 size, max_cisz;
431 int position;
433 if (!src)
434 return 0;
435 position = src->ops->get_ca(src);
437 size = apcm->vm_block->size;
438 max_cisz = src->multi * src->rsc.msr;
439 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
441 return (position + size - max_cisz - apcm->vm_block->addr) % size;
444 struct src_node_conf_t {
445 unsigned int pitch;
446 unsigned int msr:8;
447 unsigned int mix_msr:8;
448 unsigned int imp_msr:8;
449 unsigned int vo:1;
452 static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
453 struct src_node_conf_t *conf, int *n_srcc)
455 unsigned int pitch;
457 /* get pitch and convert to fixed-point 8.24 format. */
458 pitch = atc_get_pitch((atc->rsr * atc->msr),
459 apcm->substream->runtime->rate);
460 *n_srcc = 0;
462 if (1 == atc->msr) {
463 *n_srcc = apcm->substream->runtime->channels;
464 conf[0].pitch = pitch;
465 conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
466 conf[0].vo = 1;
467 } else if (2 == atc->msr) {
468 if (0x8000000 < pitch) {
469 /* Need two-stage SRCs, SRCIMPs and
470 * AMIXERs for converting format */
471 conf[0].pitch = (atc->msr << 24);
472 conf[0].msr = conf[0].mix_msr = 1;
473 conf[0].imp_msr = atc->msr;
474 conf[0].vo = 0;
475 conf[1].pitch = atc_get_pitch(atc->rsr,
476 apcm->substream->runtime->rate);
477 conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
478 conf[1].vo = 1;
479 *n_srcc = apcm->substream->runtime->channels * 2;
480 } else if (0x1000000 < pitch) {
481 /* Need one-stage SRCs, SRCIMPs and
482 * AMIXERs for converting format */
483 conf[0].pitch = pitch;
484 conf[0].msr = conf[0].mix_msr
485 = conf[0].imp_msr = atc->msr;
486 conf[0].vo = 1;
487 *n_srcc = apcm->substream->runtime->channels;
492 static int
493 atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
495 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
496 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
497 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
498 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
499 struct src_desc src_dsc = {0};
500 struct src *src;
501 struct srcimp_desc srcimp_dsc = {0};
502 struct srcimp *srcimp;
503 struct amixer_desc mix_dsc = {0};
504 struct sum_desc sum_dsc = {0};
505 unsigned int pitch;
506 int multi, err, i;
507 int n_srcimp, n_amixer, n_srcc, n_sum;
508 struct src_node_conf_t src_node_conf[2] = {{0} };
510 /* first release old resources */
511 atc_pcm_release_resources(atc, apcm);
513 /* The numbers of converting SRCs and SRCIMPs should be determined
514 * by pitch value. */
516 multi = apcm->substream->runtime->channels;
518 /* get pitch and convert to fixed-point 8.24 format. */
519 pitch = atc_get_pitch((atc->rsr * atc->msr),
520 apcm->substream->runtime->rate);
522 setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
523 n_sum = (1 == multi) ? 1 : 0;
524 n_amixer = n_sum * 2 + n_srcc;
525 n_srcimp = n_srcc;
526 if ((multi > 1) && (0x8000000 >= pitch)) {
527 /* Need extra AMIXERs and SRCIMPs for special treatment
528 * of interleaved recording of conjugate channels */
529 n_amixer += multi * atc->msr;
530 n_srcimp += multi * atc->msr;
531 } else {
532 n_srcimp += multi;
535 if (n_srcc) {
536 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
537 if (!apcm->srccs)
538 return -ENOMEM;
540 if (n_amixer) {
541 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
542 if (!apcm->amixers) {
543 err = -ENOMEM;
544 goto error1;
547 apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
548 if (!apcm->srcimps) {
549 err = -ENOMEM;
550 goto error1;
553 /* Allocate SRCs for sample rate conversion if needed */
554 src_dsc.multi = 1;
555 src_dsc.mode = ARCRW;
556 for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
557 src_dsc.msr = src_node_conf[i/multi].msr;
558 err = src_mgr->get_src(src_mgr, &src_dsc,
559 (struct src **)&apcm->srccs[i]);
560 if (err)
561 goto error1;
563 src = apcm->srccs[i];
564 pitch = src_node_conf[i/multi].pitch;
565 src->ops->set_pitch(src, pitch);
566 src->ops->set_rom(src, select_rom(pitch));
567 src->ops->set_vo(src, src_node_conf[i/multi].vo);
569 apcm->n_srcc++;
572 /* Allocate AMIXERs for routing SRCs of conversion if needed */
573 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
574 if (i < (n_sum*2))
575 mix_dsc.msr = atc->msr;
576 else if (i < (n_sum*2+n_srcc))
577 mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
578 else
579 mix_dsc.msr = 1;
581 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
582 (struct amixer **)&apcm->amixers[i]);
583 if (err)
584 goto error1;
586 apcm->n_amixer++;
589 /* Allocate a SUM resource to mix all input channels together */
590 sum_dsc.msr = atc->msr;
591 err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
592 if (err)
593 goto error1;
595 pitch = atc_get_pitch((atc->rsr * atc->msr),
596 apcm->substream->runtime->rate);
597 /* Allocate SRCIMP resources */
598 for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
599 if (i < (n_srcc))
600 srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
601 else if (1 == multi)
602 srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
603 else
604 srcimp_dsc.msr = 1;
606 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
607 if (err)
608 goto error1;
610 apcm->srcimps[i] = srcimp;
611 apcm->n_srcimp++;
614 /* Allocate a SRC for writing data to host memory */
615 src_dsc.multi = apcm->substream->runtime->channels;
616 src_dsc.msr = 1;
617 src_dsc.mode = MEMWR;
618 err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
619 if (err)
620 goto error1;
622 src = apcm->src;
623 src->ops->set_pitch(src, pitch);
625 /* Set up device virtual mem map */
626 err = ct_map_audio_buffer(atc, apcm);
627 if (err < 0)
628 goto error1;
630 return 0;
632 error1:
633 atc_pcm_release_resources(atc, apcm);
634 return err;
637 static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
639 struct src *src;
640 struct amixer *amixer;
641 struct srcimp *srcimp;
642 struct ct_mixer *mixer = atc->mixer;
643 struct sum *mono;
644 struct rsc *out_ports[8] = {NULL};
645 int err, i, j, n_sum, multi;
646 unsigned int pitch;
647 int mix_base = 0, imp_base = 0;
649 atc_pcm_release_resources(atc, apcm);
651 /* Get needed resources. */
652 err = atc_pcm_capture_get_resources(atc, apcm);
653 if (err)
654 return err;
656 /* Connect resources */
657 mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
658 &out_ports[0], &out_ports[1]);
660 multi = apcm->substream->runtime->channels;
661 if (1 == multi) {
662 mono = apcm->mono;
663 for (i = 0; i < 2; i++) {
664 amixer = apcm->amixers[i];
665 amixer->ops->setup(amixer, out_ports[i],
666 MONO_SUM_SCALE, mono);
668 out_ports[0] = &mono->rsc;
669 n_sum = 1;
670 mix_base = n_sum * 2;
673 for (i = 0; i < apcm->n_srcc; i++) {
674 src = apcm->srccs[i];
675 srcimp = apcm->srcimps[imp_base+i];
676 amixer = apcm->amixers[mix_base+i];
677 srcimp->ops->map(srcimp, src, out_ports[i%multi]);
678 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
679 out_ports[i%multi] = &amixer->rsc;
682 pitch = atc_get_pitch((atc->rsr * atc->msr),
683 apcm->substream->runtime->rate);
685 if ((multi > 1) && (pitch <= 0x8000000)) {
686 /* Special connection for interleaved
687 * recording with conjugate channels */
688 for (i = 0; i < multi; i++) {
689 out_ports[i]->ops->master(out_ports[i]);
690 for (j = 0; j < atc->msr; j++) {
691 amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
692 amixer->ops->set_input(amixer, out_ports[i]);
693 amixer->ops->set_scale(amixer, INIT_VOL);
694 amixer->ops->set_sum(amixer, NULL);
695 amixer->ops->commit_raw_write(amixer);
696 out_ports[i]->ops->next_conj(out_ports[i]);
698 srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
699 srcimp->ops->map(srcimp, apcm->src,
700 &amixer->rsc);
703 } else {
704 for (i = 0; i < multi; i++) {
705 srcimp = apcm->srcimps[apcm->n_srcc+i];
706 srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
710 ct_timer_prepare(apcm->timer);
712 return 0;
715 static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
717 struct src *src;
718 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
719 int i, multi;
721 if (apcm->started)
722 return 0;
724 apcm->started = 1;
725 multi = apcm->substream->runtime->channels;
726 /* Set up converting SRCs */
727 for (i = 0; i < apcm->n_srcc; i++) {
728 src = apcm->srccs[i];
729 src->ops->set_pm(src, ((i%multi) != (multi-1)));
730 src_mgr->src_disable(src_mgr, src);
733 /* Set up recording SRC */
734 src = apcm->src;
735 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
736 src->ops->set_sa(src, apcm->vm_block->addr);
737 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
738 src->ops->set_ca(src, apcm->vm_block->addr);
739 src_mgr->src_disable(src_mgr, src);
741 /* Disable relevant SRCs firstly */
742 src_mgr->commit_write(src_mgr);
744 /* Enable SRCs respectively */
745 for (i = 0; i < apcm->n_srcc; i++) {
746 src = apcm->srccs[i];
747 src->ops->set_state(src, SRC_STATE_RUN);
748 src->ops->commit_write(src);
749 src_mgr->src_enable_s(src_mgr, src);
751 src = apcm->src;
752 src->ops->set_bm(src, 1);
753 src->ops->set_state(src, SRC_STATE_RUN);
754 src->ops->commit_write(src);
755 src_mgr->src_enable_s(src_mgr, src);
757 /* Enable relevant SRCs synchronously */
758 src_mgr->commit_write(src_mgr);
760 ct_timer_start(apcm->timer);
761 return 0;
764 static int
765 atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
767 struct src *src = apcm->src;
769 if (!src)
770 return 0;
771 return src->ops->get_ca(src) - apcm->vm_block->addr;
774 static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
775 struct ct_atc_pcm *apcm)
777 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
778 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
779 struct src_desc desc = {0};
780 struct amixer_desc mix_dsc = {0};
781 struct src *src;
782 int err;
783 int n_amixer = apcm->substream->runtime->channels, i;
784 unsigned int pitch, rsr = atc->pll_rate;
786 /* first release old resources */
787 atc_pcm_release_resources(atc, apcm);
789 /* Get SRC resource */
790 desc.multi = apcm->substream->runtime->channels;
791 desc.msr = 1;
792 while (apcm->substream->runtime->rate > (rsr * desc.msr))
793 desc.msr <<= 1;
795 desc.mode = MEMRD;
796 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
797 if (err)
798 goto error1;
800 pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
801 src = apcm->src;
802 src->ops->set_pitch(src, pitch);
803 src->ops->set_rom(src, select_rom(pitch));
804 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
805 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
806 src->ops->set_bp(src, 1);
808 /* Get AMIXER resource */
809 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
810 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
811 if (!apcm->amixers) {
812 err = -ENOMEM;
813 goto error1;
815 mix_dsc.msr = desc.msr;
816 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
817 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
818 (struct amixer **)&apcm->amixers[i]);
819 if (err)
820 goto error1;
822 apcm->n_amixer++;
825 /* Set up device virtual mem map */
826 err = ct_map_audio_buffer(atc, apcm);
827 if (err < 0)
828 goto error1;
830 return 0;
832 error1:
833 atc_pcm_release_resources(atc, apcm);
834 return err;
837 static int atc_pll_init(struct ct_atc *atc, int rate)
839 struct hw *hw = atc->hw;
840 int err;
841 err = hw->pll_init(hw, rate);
842 atc->pll_rate = err ? 0 : rate;
843 return err;
846 static int
847 spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
849 struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
850 unsigned int rate = apcm->substream->runtime->rate;
851 unsigned int status;
852 int err = 0;
853 unsigned char iec958_con_fs;
855 switch (rate) {
856 case 48000:
857 iec958_con_fs = IEC958_AES3_CON_FS_48000;
858 break;
859 case 44100:
860 iec958_con_fs = IEC958_AES3_CON_FS_44100;
861 break;
862 case 32000:
863 iec958_con_fs = IEC958_AES3_CON_FS_32000;
864 break;
865 default:
866 return -ENOENT;
869 mutex_lock(&atc->atc_mutex);
870 dao->ops->get_spos(dao, &status);
871 if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
872 status &= ((~IEC958_AES3_CON_FS) << 24);
873 status |= (iec958_con_fs << 24);
874 dao->ops->set_spos(dao, status);
875 dao->ops->commit_write(dao);
877 if ((rate != atc->pll_rate) && (32000 != rate))
878 err = atc_pll_init(atc, rate);
879 mutex_unlock(&atc->atc_mutex);
881 return err;
884 static int
885 spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
887 struct src *src;
888 struct amixer *amixer;
889 struct dao *dao;
890 int err;
891 int i;
893 atc_pcm_release_resources(atc, apcm);
895 /* Configure SPDIFOO and PLL to passthrough mode;
896 * determine pll_rate. */
897 err = spdif_passthru_playback_setup(atc, apcm);
898 if (err)
899 return err;
901 /* Get needed resources. */
902 err = spdif_passthru_playback_get_resources(atc, apcm);
903 if (err)
904 return err;
906 /* Connect resources */
907 src = apcm->src;
908 for (i = 0; i < apcm->n_amixer; i++) {
909 amixer = apcm->amixers[i];
910 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
911 src = src->ops->next_interleave(src);
912 if (!src)
913 src = apcm->src;
915 /* Connect to SPDIFOO */
916 mutex_lock(&atc->atc_mutex);
917 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
918 amixer = apcm->amixers[0];
919 dao->ops->set_left_input(dao, &amixer->rsc);
920 amixer = apcm->amixers[1];
921 dao->ops->set_right_input(dao, &amixer->rsc);
922 mutex_unlock(&atc->atc_mutex);
924 ct_timer_prepare(apcm->timer);
926 return 0;
929 static int atc_select_line_in(struct ct_atc *atc)
931 struct hw *hw = atc->hw;
932 struct ct_mixer *mixer = atc->mixer;
933 struct src *src;
935 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
936 return 0;
938 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
939 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
941 hw->select_adc_source(hw, ADC_LINEIN);
943 src = atc->srcs[2];
944 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
945 src = atc->srcs[3];
946 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
948 return 0;
951 static int atc_select_mic_in(struct ct_atc *atc)
953 struct hw *hw = atc->hw;
954 struct ct_mixer *mixer = atc->mixer;
955 struct src *src;
957 if (hw->is_adc_source_selected(hw, ADC_MICIN))
958 return 0;
960 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
961 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
963 hw->select_adc_source(hw, ADC_MICIN);
965 src = atc->srcs[2];
966 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
967 src = atc->srcs[3];
968 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
970 return 0;
973 static int atc_have_digit_io_switch(struct ct_atc *atc)
975 struct hw *hw = atc->hw;
977 return hw->have_digit_io_switch(hw);
980 static int atc_select_digit_io(struct ct_atc *atc)
982 struct hw *hw = atc->hw;
984 if (hw->is_adc_source_selected(hw, ADC_NONE))
985 return 0;
987 hw->select_adc_source(hw, ADC_NONE);
989 return 0;
992 static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
994 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
996 if (state)
997 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
998 else
999 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
1001 daio_mgr->commit_write(daio_mgr);
1003 return 0;
1006 static int
1007 atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
1009 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1010 return dao->ops->get_spos(dao, status);
1013 static int
1014 atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
1016 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1018 dao->ops->set_spos(dao, status);
1019 dao->ops->commit_write(dao);
1020 return 0;
1023 static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1025 return atc_daio_unmute(atc, state, LINEO1);
1028 static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1030 return atc_daio_unmute(atc, state, LINEO2);
1033 static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1035 return atc_daio_unmute(atc, state, LINEO3);
1038 static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1040 return atc_daio_unmute(atc, state, LINEO4);
1043 static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1045 return atc_daio_unmute(atc, state, LINEIM);
1048 static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1050 return atc_daio_unmute(atc, state, SPDIFOO);
1053 static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1055 return atc_daio_unmute(atc, state, SPDIFIO);
1058 static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1060 return atc_dao_get_status(atc, status, SPDIFOO);
1063 static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1065 return atc_dao_set_status(atc, status, SPDIFOO);
1068 static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1070 struct dao_desc da_dsc = {0};
1071 struct dao *dao;
1072 int err;
1073 struct ct_mixer *mixer = atc->mixer;
1074 struct rsc *rscs[2] = {NULL};
1075 unsigned int spos = 0;
1077 mutex_lock(&atc->atc_mutex);
1078 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1079 da_dsc.msr = state ? 1 : atc->msr;
1080 da_dsc.passthru = state ? 1 : 0;
1081 err = dao->ops->reinit(dao, &da_dsc);
1082 if (state) {
1083 spos = IEC958_DEFAULT_CON;
1084 } else {
1085 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1086 &rscs[0], &rscs[1]);
1087 dao->ops->set_left_input(dao, rscs[0]);
1088 dao->ops->set_right_input(dao, rscs[1]);
1089 /* Restore PLL to atc->rsr if needed. */
1090 if (atc->pll_rate != atc->rsr)
1091 err = atc_pll_init(atc, atc->rsr);
1093 dao->ops->set_spos(dao, spos);
1094 dao->ops->commit_write(dao);
1095 mutex_unlock(&atc->atc_mutex);
1097 return err;
1100 static int atc_release_resources(struct ct_atc *atc)
1102 int i;
1103 struct daio_mgr *daio_mgr = NULL;
1104 struct dao *dao = NULL;
1105 struct dai *dai = NULL;
1106 struct daio *daio = NULL;
1107 struct sum_mgr *sum_mgr = NULL;
1108 struct src_mgr *src_mgr = NULL;
1109 struct srcimp_mgr *srcimp_mgr = NULL;
1110 struct srcimp *srcimp = NULL;
1111 struct ct_mixer *mixer = NULL;
1113 /* disconnect internal mixer objects */
1114 if (atc->mixer) {
1115 mixer = atc->mixer;
1116 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1117 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1118 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1119 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1120 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1121 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
1124 if (atc->daios) {
1125 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1126 for (i = 0; i < atc->n_daio; i++) {
1127 daio = atc->daios[i];
1128 if (daio->type < LINEIM) {
1129 dao = container_of(daio, struct dao, daio);
1130 dao->ops->clear_left_input(dao);
1131 dao->ops->clear_right_input(dao);
1132 } else {
1133 dai = container_of(daio, struct dai, daio);
1134 /* some thing to do for dai ... */
1136 daio_mgr->put_daio(daio_mgr, daio);
1138 kfree(atc->daios);
1139 atc->daios = NULL;
1142 if (atc->pcm) {
1143 sum_mgr = atc->rsc_mgrs[SUM];
1144 for (i = 0; i < atc->n_pcm; i++)
1145 sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1147 kfree(atc->pcm);
1148 atc->pcm = NULL;
1151 if (atc->srcs) {
1152 src_mgr = atc->rsc_mgrs[SRC];
1153 for (i = 0; i < atc->n_src; i++)
1154 src_mgr->put_src(src_mgr, atc->srcs[i]);
1156 kfree(atc->srcs);
1157 atc->srcs = NULL;
1160 if (atc->srcimps) {
1161 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1162 for (i = 0; i < atc->n_srcimp; i++) {
1163 srcimp = atc->srcimps[i];
1164 srcimp->ops->unmap(srcimp);
1165 srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1167 kfree(atc->srcimps);
1168 atc->srcimps = NULL;
1171 return 0;
1174 static int ct_atc_destroy(struct ct_atc *atc)
1176 int i = 0;
1178 if (!atc)
1179 return 0;
1181 if (atc->timer) {
1182 ct_timer_free(atc->timer);
1183 atc->timer = NULL;
1186 atc_release_resources(atc);
1188 /* Destroy internal mixer objects */
1189 if (atc->mixer)
1190 ct_mixer_destroy(atc->mixer);
1192 for (i = 0; i < NUM_RSCTYP; i++) {
1193 if (rsc_mgr_funcs[i].destroy && atc->rsc_mgrs[i])
1194 rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1198 if (atc->hw)
1199 destroy_hw_obj((struct hw *)atc->hw);
1201 /* Destroy device virtual memory manager object */
1202 if (atc->vm) {
1203 ct_vm_destroy(atc->vm);
1204 atc->vm = NULL;
1207 kfree(atc);
1209 return 0;
1212 static int atc_dev_free(struct snd_device *dev)
1214 struct ct_atc *atc = dev->device_data;
1215 return ct_atc_destroy(atc);
1218 static int __devinit atc_identify_card(struct ct_atc *atc, unsigned int ssid)
1220 const struct snd_pci_quirk *p;
1221 const struct snd_pci_quirk *list;
1222 u16 vendor_id, device_id;
1224 switch (atc->chip_type) {
1225 case ATC20K1:
1226 atc->chip_name = "20K1";
1227 list = subsys_20k1_list;
1228 break;
1229 case ATC20K2:
1230 atc->chip_name = "20K2";
1231 list = subsys_20k2_list;
1232 break;
1233 default:
1234 return -ENOENT;
1236 if (ssid) {
1237 vendor_id = ssid >> 16;
1238 device_id = ssid & 0xffff;
1239 } else {
1240 vendor_id = atc->pci->subsystem_vendor;
1241 device_id = atc->pci->subsystem_device;
1243 p = snd_pci_quirk_lookup_id(vendor_id, device_id, list);
1244 if (p) {
1245 if (p->value < 0) {
1246 printk(KERN_ERR "ctxfi: "
1247 "Device %04x:%04x is black-listed\n",
1248 vendor_id, device_id);
1249 return -ENOENT;
1251 atc->model = p->value;
1252 } else {
1253 if (atc->chip_type == ATC20K1)
1254 atc->model = CT20K1_UNKNOWN;
1255 else
1256 atc->model = CT20K2_UNKNOWN;
1258 atc->model_name = ct_subsys_name[atc->model];
1259 snd_printd("ctxfi: chip %s model %s (%04x:%04x) is found\n",
1260 atc->chip_name, atc->model_name,
1261 vendor_id, device_id);
1262 return 0;
1265 int __devinit ct_atc_create_alsa_devs(struct ct_atc *atc)
1267 enum CTALSADEVS i;
1268 int err;
1270 alsa_dev_funcs[MIXER].public_name = atc->chip_name;
1272 for (i = 0; i < NUM_CTALSADEVS; i++) {
1273 if (!alsa_dev_funcs[i].create)
1274 continue;
1276 err = alsa_dev_funcs[i].create(atc, i,
1277 alsa_dev_funcs[i].public_name);
1278 if (err) {
1279 printk(KERN_ERR "ctxfi: "
1280 "Creating alsa device %d failed!\n", i);
1281 return err;
1285 return 0;
1288 static int __devinit atc_create_hw_devs(struct ct_atc *atc)
1290 struct hw *hw;
1291 struct card_conf info = {0};
1292 int i, err;
1294 err = create_hw_obj(atc->pci, atc->chip_type, atc->model, &hw);
1295 if (err) {
1296 printk(KERN_ERR "Failed to create hw obj!!!\n");
1297 return err;
1299 atc->hw = hw;
1301 /* Initialize card hardware. */
1302 info.rsr = atc->rsr;
1303 info.msr = atc->msr;
1304 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1305 err = hw->card_init(hw, &info);
1306 if (err < 0)
1307 return err;
1309 for (i = 0; i < NUM_RSCTYP; i++) {
1310 if (!rsc_mgr_funcs[i].create)
1311 continue;
1313 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1314 if (err) {
1315 printk(KERN_ERR "ctxfi: "
1316 "Failed to create rsc_mgr %d!!!\n", i);
1317 return err;
1321 return 0;
1324 static int atc_get_resources(struct ct_atc *atc)
1326 struct daio_desc da_desc = {0};
1327 struct daio_mgr *daio_mgr;
1328 struct src_desc src_dsc = {0};
1329 struct src_mgr *src_mgr;
1330 struct srcimp_desc srcimp_dsc = {0};
1331 struct srcimp_mgr *srcimp_mgr;
1332 struct sum_desc sum_dsc = {0};
1333 struct sum_mgr *sum_mgr;
1334 int err, i;
1336 atc->daios = kzalloc(sizeof(void *)*(DAIONUM), GFP_KERNEL);
1337 if (!atc->daios)
1338 return -ENOMEM;
1340 atc->srcs = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1341 if (!atc->srcs)
1342 return -ENOMEM;
1344 atc->srcimps = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1345 if (!atc->srcimps)
1346 return -ENOMEM;
1348 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
1349 if (!atc->pcm)
1350 return -ENOMEM;
1352 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1353 da_desc.msr = atc->msr;
1354 for (i = 0, atc->n_daio = 0; i < DAIONUM-1; i++) {
1355 da_desc.type = i;
1356 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1357 (struct daio **)&atc->daios[i]);
1358 if (err) {
1359 printk(KERN_ERR "ctxfi: Failed to get DAIO "
1360 "resource %d!!!\n", i);
1361 return err;
1363 atc->n_daio++;
1365 if (atc->model == CTSB073X)
1366 da_desc.type = SPDIFI1;
1367 else
1368 da_desc.type = SPDIFIO;
1369 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1370 (struct daio **)&atc->daios[i]);
1371 if (err) {
1372 printk(KERN_ERR "ctxfi: Failed to get S/PDIF-in resource!!!\n");
1373 return err;
1375 atc->n_daio++;
1377 src_mgr = atc->rsc_mgrs[SRC];
1378 src_dsc.multi = 1;
1379 src_dsc.msr = atc->msr;
1380 src_dsc.mode = ARCRW;
1381 for (i = 0, atc->n_src = 0; i < (2*2); i++) {
1382 err = src_mgr->get_src(src_mgr, &src_dsc,
1383 (struct src **)&atc->srcs[i]);
1384 if (err)
1385 return err;
1387 atc->n_src++;
1390 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1391 srcimp_dsc.msr = 8; /* SRCIMPs for S/PDIFIn SRT */
1392 for (i = 0, atc->n_srcimp = 0; i < (2*1); i++) {
1393 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1394 (struct srcimp **)&atc->srcimps[i]);
1395 if (err)
1396 return err;
1398 atc->n_srcimp++;
1400 srcimp_dsc.msr = 8; /* SRCIMPs for LINE/MICIn SRT */
1401 for (i = 0; i < (2*1); i++) {
1402 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1403 (struct srcimp **)&atc->srcimps[2*1+i]);
1404 if (err)
1405 return err;
1407 atc->n_srcimp++;
1410 sum_mgr = atc->rsc_mgrs[SUM];
1411 sum_dsc.msr = atc->msr;
1412 for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1413 err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1414 (struct sum **)&atc->pcm[i]);
1415 if (err)
1416 return err;
1418 atc->n_pcm++;
1421 return 0;
1424 static void
1425 atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1426 struct src **srcs, struct srcimp **srcimps)
1428 struct rsc *rscs[2] = {NULL};
1429 struct src *src;
1430 struct srcimp *srcimp;
1431 int i = 0;
1433 rscs[0] = &dai->daio.rscl;
1434 rscs[1] = &dai->daio.rscr;
1435 for (i = 0; i < 2; i++) {
1436 src = srcs[i];
1437 srcimp = srcimps[i];
1438 srcimp->ops->map(srcimp, src, rscs[i]);
1439 src_mgr->src_disable(src_mgr, src);
1442 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1444 src = srcs[0];
1445 src->ops->set_pm(src, 1);
1446 for (i = 0; i < 2; i++) {
1447 src = srcs[i];
1448 src->ops->set_state(src, SRC_STATE_RUN);
1449 src->ops->commit_write(src);
1450 src_mgr->src_enable_s(src_mgr, src);
1453 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1454 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1456 dai->ops->set_enb_src(dai, 1);
1457 dai->ops->set_enb_srt(dai, 1);
1458 dai->ops->commit_write(dai);
1460 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1463 static void atc_connect_resources(struct ct_atc *atc)
1465 struct dai *dai;
1466 struct dao *dao;
1467 struct src *src;
1468 struct sum *sum;
1469 struct ct_mixer *mixer;
1470 struct rsc *rscs[2] = {NULL};
1471 int i, j;
1473 mixer = atc->mixer;
1475 for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1476 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1477 dao = container_of(atc->daios[j], struct dao, daio);
1478 dao->ops->set_left_input(dao, rscs[0]);
1479 dao->ops->set_right_input(dao, rscs[1]);
1482 dai = container_of(atc->daios[LINEIM], struct dai, daio);
1483 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1484 (struct src **)&atc->srcs[2],
1485 (struct srcimp **)&atc->srcimps[2]);
1486 src = atc->srcs[2];
1487 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1488 src = atc->srcs[3];
1489 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1491 dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1492 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1493 (struct src **)&atc->srcs[0],
1494 (struct srcimp **)&atc->srcimps[0]);
1496 src = atc->srcs[0];
1497 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1498 src = atc->srcs[1];
1499 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1501 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1502 sum = atc->pcm[j];
1503 mixer->set_input_left(mixer, i, &sum->rsc);
1504 sum = atc->pcm[j+1];
1505 mixer->set_input_right(mixer, i, &sum->rsc);
1509 #ifdef CONFIG_PM
1510 static int atc_suspend(struct ct_atc *atc, pm_message_t state)
1512 int i;
1513 struct hw *hw = atc->hw;
1515 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D3hot);
1517 for (i = FRONT; i < NUM_PCMS; i++) {
1518 if (!atc->pcms[i])
1519 continue;
1521 snd_pcm_suspend_all(atc->pcms[i]);
1524 atc_release_resources(atc);
1526 hw->suspend(hw, state);
1528 return 0;
1531 static int atc_hw_resume(struct ct_atc *atc)
1533 struct hw *hw = atc->hw;
1534 struct card_conf info = {0};
1536 /* Re-initialize card hardware. */
1537 info.rsr = atc->rsr;
1538 info.msr = atc->msr;
1539 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1540 return hw->resume(hw, &info);
1543 static int atc_resources_resume(struct ct_atc *atc)
1545 struct ct_mixer *mixer;
1546 int err = 0;
1548 /* Get resources */
1549 err = atc_get_resources(atc);
1550 if (err < 0) {
1551 atc_release_resources(atc);
1552 return err;
1555 /* Build topology */
1556 atc_connect_resources(atc);
1558 mixer = atc->mixer;
1559 mixer->resume(mixer);
1561 return 0;
1564 static int atc_resume(struct ct_atc *atc)
1566 int err = 0;
1568 /* Do hardware resume. */
1569 err = atc_hw_resume(atc);
1570 if (err < 0) {
1571 printk(KERN_ERR "ctxfi: pci_enable_device failed, "
1572 "disabling device\n");
1573 snd_card_disconnect(atc->card);
1574 return err;
1577 err = atc_resources_resume(atc);
1578 if (err < 0)
1579 return err;
1581 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D0);
1583 return 0;
1585 #endif
1587 static struct ct_atc atc_preset __devinitdata = {
1588 .map_audio_buffer = ct_map_audio_buffer,
1589 .unmap_audio_buffer = ct_unmap_audio_buffer,
1590 .pcm_playback_prepare = atc_pcm_playback_prepare,
1591 .pcm_release_resources = atc_pcm_release_resources,
1592 .pcm_playback_start = atc_pcm_playback_start,
1593 .pcm_playback_stop = atc_pcm_stop,
1594 .pcm_playback_position = atc_pcm_playback_position,
1595 .pcm_capture_prepare = atc_pcm_capture_prepare,
1596 .pcm_capture_start = atc_pcm_capture_start,
1597 .pcm_capture_stop = atc_pcm_stop,
1598 .pcm_capture_position = atc_pcm_capture_position,
1599 .spdif_passthru_playback_prepare = spdif_passthru_playback_prepare,
1600 .get_ptp_phys = atc_get_ptp_phys,
1601 .select_line_in = atc_select_line_in,
1602 .select_mic_in = atc_select_mic_in,
1603 .select_digit_io = atc_select_digit_io,
1604 .line_front_unmute = atc_line_front_unmute,
1605 .line_surround_unmute = atc_line_surround_unmute,
1606 .line_clfe_unmute = atc_line_clfe_unmute,
1607 .line_rear_unmute = atc_line_rear_unmute,
1608 .line_in_unmute = atc_line_in_unmute,
1609 .spdif_out_unmute = atc_spdif_out_unmute,
1610 .spdif_in_unmute = atc_spdif_in_unmute,
1611 .spdif_out_get_status = atc_spdif_out_get_status,
1612 .spdif_out_set_status = atc_spdif_out_set_status,
1613 .spdif_out_passthru = atc_spdif_out_passthru,
1614 .have_digit_io_switch = atc_have_digit_io_switch,
1615 #ifdef CONFIG_PM
1616 .suspend = atc_suspend,
1617 .resume = atc_resume,
1618 #endif
1622 * ct_atc_create - create and initialize a hardware manager
1623 * @card: corresponding alsa card object
1624 * @pci: corresponding kernel pci device object
1625 * @ratc: return created object address in it
1627 * Creates and initializes a hardware manager.
1629 * Creates kmallocated ct_atc structure. Initializes hardware.
1630 * Returns 0 if suceeds, or negative error code if fails.
1633 int __devinit ct_atc_create(struct snd_card *card, struct pci_dev *pci,
1634 unsigned int rsr, unsigned int msr,
1635 int chip_type, unsigned int ssid,
1636 struct ct_atc **ratc)
1638 struct ct_atc *atc;
1639 static struct snd_device_ops ops = {
1640 .dev_free = atc_dev_free,
1642 int err;
1644 *ratc = NULL;
1646 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
1647 if (!atc)
1648 return -ENOMEM;
1650 /* Set operations */
1651 *atc = atc_preset;
1653 atc->card = card;
1654 atc->pci = pci;
1655 atc->rsr = rsr;
1656 atc->msr = msr;
1657 atc->chip_type = chip_type;
1659 mutex_init(&atc->atc_mutex);
1661 /* Find card model */
1662 err = atc_identify_card(atc, ssid);
1663 if (err < 0) {
1664 printk(KERN_ERR "ctatc: Card not recognised\n");
1665 goto error1;
1668 /* Set up device virtual memory management object */
1669 err = ct_vm_create(&atc->vm, pci);
1670 if (err < 0)
1671 goto error1;
1673 /* Create all atc hw devices */
1674 err = atc_create_hw_devs(atc);
1675 if (err < 0)
1676 goto error1;
1678 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1679 if (err) {
1680 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
1681 goto error1;
1684 /* Get resources */
1685 err = atc_get_resources(atc);
1686 if (err < 0)
1687 goto error1;
1689 /* Build topology */
1690 atc_connect_resources(atc);
1692 atc->timer = ct_timer_new(atc);
1693 if (!atc->timer)
1694 goto error1;
1696 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1697 if (err < 0)
1698 goto error1;
1700 snd_card_set_dev(card, &pci->dev);
1702 *ratc = atc;
1703 return 0;
1705 error1:
1706 ct_atc_destroy(atc);
1707 printk(KERN_ERR "ctxfi: Something wrong!!!\n");
1708 return err;