PM / yenta: Split resume into early and late parts (rev. 4)
[linux/fpc-iii.git] / sound / pci / ctxfi / ctatc.c
bloba49c766473073e1a6d94b7ed75ed014fd0c2c293
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 <sound/pcm.h>
28 #include <sound/control.h>
29 #include <sound/asoundef.h>
31 #define MONO_SUM_SCALE 0x19a8 /* 2^(-0.5) in 14-bit floating format */
32 #define DAIONUM 7
33 #define MAX_MULTI_CHN 8
35 #define IEC958_DEFAULT_CON ((IEC958_AES0_NONAUDIO \
36 | IEC958_AES0_CON_NOT_COPYRIGHT) \
37 | ((IEC958_AES1_CON_MIXER \
38 | IEC958_AES1_CON_ORIGINAL) << 8) \
39 | (0x10 << 16) \
40 | ((IEC958_AES3_CON_FS_48000) << 24))
42 static struct snd_pci_quirk __devinitdata subsys_20k1_list[] = {
43 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0022, "SB055x", CTSB055X),
44 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x002f, "SB055x", CTSB055X),
45 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0029, "SB073x", CTSB073X),
46 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0031, "SB073x", CTSB073X),
47 SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000, 0x6000,
48 "UAA", CTUAA),
49 { } /* terminator */
52 static struct snd_pci_quirk __devinitdata subsys_20k2_list[] = {
53 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB0760,
54 "SB0760", CTSB0760),
55 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08801,
56 "SB0880", CTSB0880),
57 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08802,
58 "SB0880", CTSB0880),
59 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08803,
60 "SB0880", CTSB0880),
61 SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000,
62 PCI_SUBDEVICE_ID_CREATIVE_HENDRIX, "HENDRIX",
63 CTHENDRIX),
64 { } /* terminator */
67 static const char *ct_subsys_name[NUM_CTCARDS] = {
68 /* 20k1 models */
69 [CTSB055X] = "SB055x",
70 [CTSB073X] = "SB073x",
71 [CTUAA] = "UAA",
72 [CT20K1_UNKNOWN] = "Unknown",
73 /* 20k2 models */
74 [CTSB0760] = "SB076x",
75 [CTHENDRIX] = "Hendrix",
76 [CTSB0880] = "SB0880",
77 [CT20K2_UNKNOWN] = "Unknown",
80 static struct {
81 int (*create)(struct ct_atc *atc,
82 enum CTALSADEVS device, const char *device_name);
83 int (*destroy)(void *alsa_dev);
84 const char *public_name;
85 } alsa_dev_funcs[NUM_CTALSADEVS] = {
86 [FRONT] = { .create = ct_alsa_pcm_create,
87 .destroy = NULL,
88 .public_name = "Front/WaveIn"},
89 [SURROUND] = { .create = ct_alsa_pcm_create,
90 .destroy = NULL,
91 .public_name = "Surround"},
92 [CLFE] = { .create = ct_alsa_pcm_create,
93 .destroy = NULL,
94 .public_name = "Center/LFE"},
95 [SIDE] = { .create = ct_alsa_pcm_create,
96 .destroy = NULL,
97 .public_name = "Side"},
98 [IEC958] = { .create = ct_alsa_pcm_create,
99 .destroy = NULL,
100 .public_name = "IEC958 Non-audio"},
102 [MIXER] = { .create = ct_alsa_mix_create,
103 .destroy = NULL,
104 .public_name = "Mixer"}
107 typedef int (*create_t)(void *, void **);
108 typedef int (*destroy_t)(void *);
110 static struct {
111 int (*create)(void *hw, void **rmgr);
112 int (*destroy)(void *mgr);
113 } rsc_mgr_funcs[NUM_RSCTYP] = {
114 [SRC] = { .create = (create_t)src_mgr_create,
115 .destroy = (destroy_t)src_mgr_destroy },
116 [SRCIMP] = { .create = (create_t)srcimp_mgr_create,
117 .destroy = (destroy_t)srcimp_mgr_destroy },
118 [AMIXER] = { .create = (create_t)amixer_mgr_create,
119 .destroy = (destroy_t)amixer_mgr_destroy },
120 [SUM] = { .create = (create_t)sum_mgr_create,
121 .destroy = (destroy_t)sum_mgr_destroy },
122 [DAIO] = { .create = (create_t)daio_mgr_create,
123 .destroy = (destroy_t)daio_mgr_destroy }
126 static int
127 atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
129 /* *
130 * Only mono and interleaved modes are supported now.
131 * Always allocates a contiguous channel block.
132 * */
134 static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
136 struct snd_pcm_runtime *runtime;
137 struct ct_vm *vm;
139 if (NULL == apcm->substream)
140 return 0;
142 runtime = apcm->substream->runtime;
143 vm = atc->vm;
145 apcm->vm_block = vm->map(vm, apcm->substream, runtime->dma_bytes);
147 if (NULL == apcm->vm_block)
148 return -ENOENT;
150 return 0;
153 static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
155 struct ct_vm *vm;
157 if (NULL == apcm->vm_block)
158 return;
160 vm = atc->vm;
162 vm->unmap(vm, apcm->vm_block);
164 apcm->vm_block = NULL;
167 static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
169 struct ct_vm *vm;
170 void *kvirt_addr;
171 unsigned long phys_addr;
173 vm = atc->vm;
174 kvirt_addr = vm->get_ptp_virt(vm, index);
175 if (kvirt_addr == NULL)
176 phys_addr = (~0UL);
177 else
178 phys_addr = virt_to_phys(kvirt_addr);
180 return phys_addr;
183 static unsigned int convert_format(snd_pcm_format_t snd_format)
185 switch (snd_format) {
186 case SNDRV_PCM_FORMAT_U8:
187 return SRC_SF_U8;
188 case SNDRV_PCM_FORMAT_S16_LE:
189 return SRC_SF_S16;
190 case SNDRV_PCM_FORMAT_S24_3LE:
191 return SRC_SF_S24;
192 case SNDRV_PCM_FORMAT_S32_LE:
193 return SRC_SF_S32;
194 case SNDRV_PCM_FORMAT_FLOAT_LE:
195 return SRC_SF_F32;
196 default:
197 printk(KERN_ERR "ctxfi: not recognized snd format is %d \n",
198 snd_format);
199 return SRC_SF_S16;
203 static unsigned int
204 atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
206 unsigned int pitch;
207 int b;
209 /* get pitch and convert to fixed-point 8.24 format. */
210 pitch = (input_rate / output_rate) << 24;
211 input_rate %= output_rate;
212 input_rate /= 100;
213 output_rate /= 100;
214 for (b = 31; ((b >= 0) && !(input_rate >> b)); )
215 b--;
217 if (b >= 0) {
218 input_rate <<= (31 - b);
219 input_rate /= output_rate;
220 b = 24 - (31 - b);
221 if (b >= 0)
222 input_rate <<= b;
223 else
224 input_rate >>= -b;
226 pitch |= input_rate;
229 return pitch;
232 static int select_rom(unsigned int pitch)
234 if ((pitch > 0x00428f5c) && (pitch < 0x01b851ec)) {
235 /* 0.26 <= pitch <= 1.72 */
236 return 1;
237 } else if ((0x01d66666 == pitch) || (0x01d66667 == pitch)) {
238 /* pitch == 1.8375 */
239 return 2;
240 } else if (0x02000000 == pitch) {
241 /* pitch == 2 */
242 return 3;
243 } else if ((pitch >= 0x0) && (pitch <= 0x08000000)) {
244 /* 0 <= pitch <= 8 */
245 return 0;
246 } else {
247 return -ENOENT;
251 static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
253 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
254 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
255 struct src_desc desc = {0};
256 struct amixer_desc mix_dsc = {0};
257 struct src *src;
258 struct amixer *amixer;
259 int err;
260 int n_amixer = apcm->substream->runtime->channels, i = 0;
261 int device = apcm->substream->pcm->device;
262 unsigned int pitch;
264 /* first release old resources */
265 atc_pcm_release_resources(atc, apcm);
267 /* Get SRC resource */
268 desc.multi = apcm->substream->runtime->channels;
269 desc.msr = atc->msr;
270 desc.mode = MEMRD;
271 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
272 if (err)
273 goto error1;
275 pitch = atc_get_pitch(apcm->substream->runtime->rate,
276 (atc->rsr * atc->msr));
277 src = apcm->src;
278 src->ops->set_pitch(src, pitch);
279 src->ops->set_rom(src, select_rom(pitch));
280 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
281 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
283 /* Get AMIXER resource */
284 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
285 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
286 if (NULL == apcm->amixers) {
287 err = -ENOMEM;
288 goto error1;
290 mix_dsc.msr = atc->msr;
291 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
292 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
293 (struct amixer **)&apcm->amixers[i]);
294 if (err)
295 goto error1;
297 apcm->n_amixer++;
300 /* Set up device virtual mem map */
301 err = ct_map_audio_buffer(atc, apcm);
302 if (err < 0)
303 goto error1;
305 /* Connect resources */
306 src = apcm->src;
307 for (i = 0; i < n_amixer; i++) {
308 amixer = apcm->amixers[i];
309 mutex_lock(&atc->atc_mutex);
310 amixer->ops->setup(amixer, &src->rsc,
311 INIT_VOL, atc->pcm[i+device*2]);
312 mutex_unlock(&atc->atc_mutex);
313 src = src->ops->next_interleave(src);
314 if (NULL == src)
315 src = apcm->src;
318 ct_timer_prepare(apcm->timer);
320 return 0;
322 error1:
323 atc_pcm_release_resources(atc, apcm);
324 return err;
327 static int
328 atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
330 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
331 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
332 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
333 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
334 struct srcimp *srcimp;
335 int i;
337 if (NULL != apcm->srcimps) {
338 for (i = 0; i < apcm->n_srcimp; i++) {
339 srcimp = apcm->srcimps[i];
340 srcimp->ops->unmap(srcimp);
341 srcimp_mgr->put_srcimp(srcimp_mgr, srcimp);
342 apcm->srcimps[i] = NULL;
344 kfree(apcm->srcimps);
345 apcm->srcimps = NULL;
348 if (NULL != apcm->srccs) {
349 for (i = 0; i < apcm->n_srcc; i++) {
350 src_mgr->put_src(src_mgr, apcm->srccs[i]);
351 apcm->srccs[i] = NULL;
353 kfree(apcm->srccs);
354 apcm->srccs = NULL;
357 if (NULL != apcm->amixers) {
358 for (i = 0; i < apcm->n_amixer; i++) {
359 amixer_mgr->put_amixer(amixer_mgr, apcm->amixers[i]);
360 apcm->amixers[i] = NULL;
362 kfree(apcm->amixers);
363 apcm->amixers = NULL;
366 if (NULL != apcm->mono) {
367 sum_mgr->put_sum(sum_mgr, apcm->mono);
368 apcm->mono = NULL;
371 if (NULL != apcm->src) {
372 src_mgr->put_src(src_mgr, apcm->src);
373 apcm->src = NULL;
376 if (NULL != apcm->vm_block) {
377 /* Undo device virtual mem map */
378 ct_unmap_audio_buffer(atc, apcm);
379 apcm->vm_block = NULL;
382 return 0;
385 static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
387 unsigned int max_cisz;
388 struct src *src = apcm->src;
390 if (apcm->started)
391 return 0;
392 apcm->started = 1;
394 max_cisz = src->multi * src->rsc.msr;
395 max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
397 src->ops->set_sa(src, apcm->vm_block->addr);
398 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
399 src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
400 src->ops->set_cisz(src, max_cisz);
402 src->ops->set_bm(src, 1);
403 src->ops->set_state(src, SRC_STATE_INIT);
404 src->ops->commit_write(src);
406 ct_timer_start(apcm->timer);
407 return 0;
410 static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
412 struct src *src;
413 int i;
415 ct_timer_stop(apcm->timer);
417 src = apcm->src;
418 src->ops->set_bm(src, 0);
419 src->ops->set_state(src, SRC_STATE_OFF);
420 src->ops->commit_write(src);
422 if (NULL != apcm->srccs) {
423 for (i = 0; i < apcm->n_srcc; i++) {
424 src = apcm->srccs[i];
425 src->ops->set_bm(src, 0);
426 src->ops->set_state(src, SRC_STATE_OFF);
427 src->ops->commit_write(src);
431 apcm->started = 0;
433 return 0;
436 static int
437 atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
439 struct src *src = apcm->src;
440 u32 size, max_cisz;
441 int position;
443 if (!src)
444 return 0;
445 position = src->ops->get_ca(src);
447 size = apcm->vm_block->size;
448 max_cisz = src->multi * src->rsc.msr;
449 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
451 return (position + size - max_cisz - apcm->vm_block->addr) % size;
454 struct src_node_conf_t {
455 unsigned int pitch;
456 unsigned int msr:8;
457 unsigned int mix_msr:8;
458 unsigned int imp_msr:8;
459 unsigned int vo:1;
462 static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
463 struct src_node_conf_t *conf, int *n_srcc)
465 unsigned int pitch;
467 /* get pitch and convert to fixed-point 8.24 format. */
468 pitch = atc_get_pitch((atc->rsr * atc->msr),
469 apcm->substream->runtime->rate);
470 *n_srcc = 0;
472 if (1 == atc->msr) {
473 *n_srcc = apcm->substream->runtime->channels;
474 conf[0].pitch = pitch;
475 conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
476 conf[0].vo = 1;
477 } else if (2 == atc->msr) {
478 if (0x8000000 < pitch) {
479 /* Need two-stage SRCs, SRCIMPs and
480 * AMIXERs for converting format */
481 conf[0].pitch = (atc->msr << 24);
482 conf[0].msr = conf[0].mix_msr = 1;
483 conf[0].imp_msr = atc->msr;
484 conf[0].vo = 0;
485 conf[1].pitch = atc_get_pitch(atc->rsr,
486 apcm->substream->runtime->rate);
487 conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
488 conf[1].vo = 1;
489 *n_srcc = apcm->substream->runtime->channels * 2;
490 } else if (0x1000000 < pitch) {
491 /* Need one-stage SRCs, SRCIMPs and
492 * AMIXERs for converting format */
493 conf[0].pitch = pitch;
494 conf[0].msr = conf[0].mix_msr
495 = conf[0].imp_msr = atc->msr;
496 conf[0].vo = 1;
497 *n_srcc = apcm->substream->runtime->channels;
502 static int
503 atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
505 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
506 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
507 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
508 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
509 struct src_desc src_dsc = {0};
510 struct src *src;
511 struct srcimp_desc srcimp_dsc = {0};
512 struct srcimp *srcimp;
513 struct amixer_desc mix_dsc = {0};
514 struct sum_desc sum_dsc = {0};
515 unsigned int pitch;
516 int multi, err, i;
517 int n_srcimp, n_amixer, n_srcc, n_sum;
518 struct src_node_conf_t src_node_conf[2] = {{0} };
520 /* first release old resources */
521 atc_pcm_release_resources(atc, apcm);
523 /* The numbers of converting SRCs and SRCIMPs should be determined
524 * by pitch value. */
526 multi = apcm->substream->runtime->channels;
528 /* get pitch and convert to fixed-point 8.24 format. */
529 pitch = atc_get_pitch((atc->rsr * atc->msr),
530 apcm->substream->runtime->rate);
532 setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
533 n_sum = (1 == multi) ? 1 : 0;
534 n_amixer = n_sum * 2 + n_srcc;
535 n_srcimp = n_srcc;
536 if ((multi > 1) && (0x8000000 >= pitch)) {
537 /* Need extra AMIXERs and SRCIMPs for special treatment
538 * of interleaved recording of conjugate channels */
539 n_amixer += multi * atc->msr;
540 n_srcimp += multi * atc->msr;
541 } else {
542 n_srcimp += multi;
545 if (n_srcc) {
546 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
547 if (NULL == apcm->srccs)
548 return -ENOMEM;
550 if (n_amixer) {
551 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
552 if (NULL == apcm->amixers) {
553 err = -ENOMEM;
554 goto error1;
557 apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
558 if (NULL == apcm->srcimps) {
559 err = -ENOMEM;
560 goto error1;
563 /* Allocate SRCs for sample rate conversion if needed */
564 src_dsc.multi = 1;
565 src_dsc.mode = ARCRW;
566 for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
567 src_dsc.msr = src_node_conf[i/multi].msr;
568 err = src_mgr->get_src(src_mgr, &src_dsc,
569 (struct src **)&apcm->srccs[i]);
570 if (err)
571 goto error1;
573 src = apcm->srccs[i];
574 pitch = src_node_conf[i/multi].pitch;
575 src->ops->set_pitch(src, pitch);
576 src->ops->set_rom(src, select_rom(pitch));
577 src->ops->set_vo(src, src_node_conf[i/multi].vo);
579 apcm->n_srcc++;
582 /* Allocate AMIXERs for routing SRCs of conversion if needed */
583 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
584 if (i < (n_sum*2))
585 mix_dsc.msr = atc->msr;
586 else if (i < (n_sum*2+n_srcc))
587 mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
588 else
589 mix_dsc.msr = 1;
591 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
592 (struct amixer **)&apcm->amixers[i]);
593 if (err)
594 goto error1;
596 apcm->n_amixer++;
599 /* Allocate a SUM resource to mix all input channels together */
600 sum_dsc.msr = atc->msr;
601 err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
602 if (err)
603 goto error1;
605 pitch = atc_get_pitch((atc->rsr * atc->msr),
606 apcm->substream->runtime->rate);
607 /* Allocate SRCIMP resources */
608 for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
609 if (i < (n_srcc))
610 srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
611 else if (1 == multi)
612 srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
613 else
614 srcimp_dsc.msr = 1;
616 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
617 if (err)
618 goto error1;
620 apcm->srcimps[i] = srcimp;
621 apcm->n_srcimp++;
624 /* Allocate a SRC for writing data to host memory */
625 src_dsc.multi = apcm->substream->runtime->channels;
626 src_dsc.msr = 1;
627 src_dsc.mode = MEMWR;
628 err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
629 if (err)
630 goto error1;
632 src = apcm->src;
633 src->ops->set_pitch(src, pitch);
635 /* Set up device virtual mem map */
636 err = ct_map_audio_buffer(atc, apcm);
637 if (err < 0)
638 goto error1;
640 return 0;
642 error1:
643 atc_pcm_release_resources(atc, apcm);
644 return err;
647 static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
649 struct src *src;
650 struct amixer *amixer;
651 struct srcimp *srcimp;
652 struct ct_mixer *mixer = atc->mixer;
653 struct sum *mono;
654 struct rsc *out_ports[8] = {NULL};
655 int err, i, j, n_sum, multi;
656 unsigned int pitch;
657 int mix_base = 0, imp_base = 0;
659 atc_pcm_release_resources(atc, apcm);
661 /* Get needed resources. */
662 err = atc_pcm_capture_get_resources(atc, apcm);
663 if (err)
664 return err;
666 /* Connect resources */
667 mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
668 &out_ports[0], &out_ports[1]);
670 multi = apcm->substream->runtime->channels;
671 if (1 == multi) {
672 mono = apcm->mono;
673 for (i = 0; i < 2; i++) {
674 amixer = apcm->amixers[i];
675 amixer->ops->setup(amixer, out_ports[i],
676 MONO_SUM_SCALE, mono);
678 out_ports[0] = &mono->rsc;
679 n_sum = 1;
680 mix_base = n_sum * 2;
683 for (i = 0; i < apcm->n_srcc; i++) {
684 src = apcm->srccs[i];
685 srcimp = apcm->srcimps[imp_base+i];
686 amixer = apcm->amixers[mix_base+i];
687 srcimp->ops->map(srcimp, src, out_ports[i%multi]);
688 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
689 out_ports[i%multi] = &amixer->rsc;
692 pitch = atc_get_pitch((atc->rsr * atc->msr),
693 apcm->substream->runtime->rate);
695 if ((multi > 1) && (pitch <= 0x8000000)) {
696 /* Special connection for interleaved
697 * recording with conjugate channels */
698 for (i = 0; i < multi; i++) {
699 out_ports[i]->ops->master(out_ports[i]);
700 for (j = 0; j < atc->msr; j++) {
701 amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
702 amixer->ops->set_input(amixer, out_ports[i]);
703 amixer->ops->set_scale(amixer, INIT_VOL);
704 amixer->ops->set_sum(amixer, NULL);
705 amixer->ops->commit_raw_write(amixer);
706 out_ports[i]->ops->next_conj(out_ports[i]);
708 srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
709 srcimp->ops->map(srcimp, apcm->src,
710 &amixer->rsc);
713 } else {
714 for (i = 0; i < multi; i++) {
715 srcimp = apcm->srcimps[apcm->n_srcc+i];
716 srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
720 ct_timer_prepare(apcm->timer);
722 return 0;
725 static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
727 struct src *src;
728 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
729 int i, multi;
731 if (apcm->started)
732 return 0;
734 apcm->started = 1;
735 multi = apcm->substream->runtime->channels;
736 /* Set up converting SRCs */
737 for (i = 0; i < apcm->n_srcc; i++) {
738 src = apcm->srccs[i];
739 src->ops->set_pm(src, ((i%multi) != (multi-1)));
740 src_mgr->src_disable(src_mgr, src);
743 /* Set up recording SRC */
744 src = apcm->src;
745 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
746 src->ops->set_sa(src, apcm->vm_block->addr);
747 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
748 src->ops->set_ca(src, apcm->vm_block->addr);
749 src_mgr->src_disable(src_mgr, src);
751 /* Disable relevant SRCs firstly */
752 src_mgr->commit_write(src_mgr);
754 /* Enable SRCs respectively */
755 for (i = 0; i < apcm->n_srcc; i++) {
756 src = apcm->srccs[i];
757 src->ops->set_state(src, SRC_STATE_RUN);
758 src->ops->commit_write(src);
759 src_mgr->src_enable_s(src_mgr, src);
761 src = apcm->src;
762 src->ops->set_bm(src, 1);
763 src->ops->set_state(src, SRC_STATE_RUN);
764 src->ops->commit_write(src);
765 src_mgr->src_enable_s(src_mgr, src);
767 /* Enable relevant SRCs synchronously */
768 src_mgr->commit_write(src_mgr);
770 ct_timer_start(apcm->timer);
771 return 0;
774 static int
775 atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
777 struct src *src = apcm->src;
779 if (!src)
780 return 0;
781 return src->ops->get_ca(src) - apcm->vm_block->addr;
784 static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
785 struct ct_atc_pcm *apcm)
787 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
788 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
789 struct src_desc desc = {0};
790 struct amixer_desc mix_dsc = {0};
791 struct src *src;
792 int err;
793 int n_amixer = apcm->substream->runtime->channels, i;
794 unsigned int pitch, rsr = atc->pll_rate;
796 /* first release old resources */
797 atc_pcm_release_resources(atc, apcm);
799 /* Get SRC resource */
800 desc.multi = apcm->substream->runtime->channels;
801 desc.msr = 1;
802 while (apcm->substream->runtime->rate > (rsr * desc.msr))
803 desc.msr <<= 1;
805 desc.mode = MEMRD;
806 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
807 if (err)
808 goto error1;
810 pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
811 src = apcm->src;
812 src->ops->set_pitch(src, pitch);
813 src->ops->set_rom(src, select_rom(pitch));
814 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
815 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
816 src->ops->set_bp(src, 1);
818 /* Get AMIXER resource */
819 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
820 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
821 if (NULL == apcm->amixers) {
822 err = -ENOMEM;
823 goto error1;
825 mix_dsc.msr = desc.msr;
826 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
827 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
828 (struct amixer **)&apcm->amixers[i]);
829 if (err)
830 goto error1;
832 apcm->n_amixer++;
835 /* Set up device virtual mem map */
836 err = ct_map_audio_buffer(atc, apcm);
837 if (err < 0)
838 goto error1;
840 return 0;
842 error1:
843 atc_pcm_release_resources(atc, apcm);
844 return err;
847 static int atc_pll_init(struct ct_atc *atc, int rate)
849 struct hw *hw = atc->hw;
850 int err;
851 err = hw->pll_init(hw, rate);
852 atc->pll_rate = err ? 0 : rate;
853 return err;
856 static int
857 spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
859 struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
860 unsigned int rate = apcm->substream->runtime->rate;
861 unsigned int status;
862 int err = 0;
863 unsigned char iec958_con_fs;
865 switch (rate) {
866 case 48000:
867 iec958_con_fs = IEC958_AES3_CON_FS_48000;
868 break;
869 case 44100:
870 iec958_con_fs = IEC958_AES3_CON_FS_44100;
871 break;
872 case 32000:
873 iec958_con_fs = IEC958_AES3_CON_FS_32000;
874 break;
875 default:
876 return -ENOENT;
879 mutex_lock(&atc->atc_mutex);
880 dao->ops->get_spos(dao, &status);
881 if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
882 status &= ((~IEC958_AES3_CON_FS) << 24);
883 status |= (iec958_con_fs << 24);
884 dao->ops->set_spos(dao, status);
885 dao->ops->commit_write(dao);
887 if ((rate != atc->pll_rate) && (32000 != rate))
888 err = atc_pll_init(atc, rate);
889 mutex_unlock(&atc->atc_mutex);
891 return err;
894 static int
895 spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
897 struct src *src;
898 struct amixer *amixer;
899 struct dao *dao;
900 int err;
901 int i;
903 atc_pcm_release_resources(atc, apcm);
905 /* Configure SPDIFOO and PLL to passthrough mode;
906 * determine pll_rate. */
907 err = spdif_passthru_playback_setup(atc, apcm);
908 if (err)
909 return err;
911 /* Get needed resources. */
912 err = spdif_passthru_playback_get_resources(atc, apcm);
913 if (err)
914 return err;
916 /* Connect resources */
917 src = apcm->src;
918 for (i = 0; i < apcm->n_amixer; i++) {
919 amixer = apcm->amixers[i];
920 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
921 src = src->ops->next_interleave(src);
922 if (NULL == src)
923 src = apcm->src;
925 /* Connect to SPDIFOO */
926 mutex_lock(&atc->atc_mutex);
927 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
928 amixer = apcm->amixers[0];
929 dao->ops->set_left_input(dao, &amixer->rsc);
930 amixer = apcm->amixers[1];
931 dao->ops->set_right_input(dao, &amixer->rsc);
932 mutex_unlock(&atc->atc_mutex);
934 ct_timer_prepare(apcm->timer);
936 return 0;
939 static int atc_select_line_in(struct ct_atc *atc)
941 struct hw *hw = atc->hw;
942 struct ct_mixer *mixer = atc->mixer;
943 struct src *src;
945 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
946 return 0;
948 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
949 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
951 hw->select_adc_source(hw, ADC_LINEIN);
953 src = atc->srcs[2];
954 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
955 src = atc->srcs[3];
956 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
958 return 0;
961 static int atc_select_mic_in(struct ct_atc *atc)
963 struct hw *hw = atc->hw;
964 struct ct_mixer *mixer = atc->mixer;
965 struct src *src;
967 if (hw->is_adc_source_selected(hw, ADC_MICIN))
968 return 0;
970 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
971 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
973 hw->select_adc_source(hw, ADC_MICIN);
975 src = atc->srcs[2];
976 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
977 src = atc->srcs[3];
978 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
980 return 0;
983 static int atc_have_digit_io_switch(struct ct_atc *atc)
985 struct hw *hw = atc->hw;
987 return hw->have_digit_io_switch(hw);
990 static int atc_select_digit_io(struct ct_atc *atc)
992 struct hw *hw = atc->hw;
994 if (hw->is_adc_source_selected(hw, ADC_NONE))
995 return 0;
997 hw->select_adc_source(hw, ADC_NONE);
999 return 0;
1002 static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
1004 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
1006 if (state)
1007 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
1008 else
1009 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
1011 daio_mgr->commit_write(daio_mgr);
1013 return 0;
1016 static int
1017 atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
1019 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1020 return dao->ops->get_spos(dao, status);
1023 static int
1024 atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
1026 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1028 dao->ops->set_spos(dao, status);
1029 dao->ops->commit_write(dao);
1030 return 0;
1033 static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1035 return atc_daio_unmute(atc, state, LINEO1);
1038 static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1040 return atc_daio_unmute(atc, state, LINEO4);
1043 static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1045 return atc_daio_unmute(atc, state, LINEO3);
1048 static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1050 return atc_daio_unmute(atc, state, LINEO2);
1053 static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1055 return atc_daio_unmute(atc, state, LINEIM);
1058 static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1060 return atc_daio_unmute(atc, state, SPDIFOO);
1063 static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1065 return atc_daio_unmute(atc, state, SPDIFIO);
1068 static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1070 return atc_dao_get_status(atc, status, SPDIFOO);
1073 static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1075 return atc_dao_set_status(atc, status, SPDIFOO);
1078 static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1080 struct dao_desc da_dsc = {0};
1081 struct dao *dao;
1082 int err;
1083 struct ct_mixer *mixer = atc->mixer;
1084 struct rsc *rscs[2] = {NULL};
1085 unsigned int spos = 0;
1087 mutex_lock(&atc->atc_mutex);
1088 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1089 da_dsc.msr = state ? 1 : atc->msr;
1090 da_dsc.passthru = state ? 1 : 0;
1091 err = dao->ops->reinit(dao, &da_dsc);
1092 if (state) {
1093 spos = IEC958_DEFAULT_CON;
1094 } else {
1095 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1096 &rscs[0], &rscs[1]);
1097 dao->ops->set_left_input(dao, rscs[0]);
1098 dao->ops->set_right_input(dao, rscs[1]);
1099 /* Restore PLL to atc->rsr if needed. */
1100 if (atc->pll_rate != atc->rsr)
1101 err = atc_pll_init(atc, atc->rsr);
1103 dao->ops->set_spos(dao, spos);
1104 dao->ops->commit_write(dao);
1105 mutex_unlock(&atc->atc_mutex);
1107 return err;
1110 static int atc_release_resources(struct ct_atc *atc)
1112 int i;
1113 struct daio_mgr *daio_mgr = NULL;
1114 struct dao *dao = NULL;
1115 struct dai *dai = NULL;
1116 struct daio *daio = NULL;
1117 struct sum_mgr *sum_mgr = NULL;
1118 struct src_mgr *src_mgr = NULL;
1119 struct srcimp_mgr *srcimp_mgr = NULL;
1120 struct srcimp *srcimp = NULL;
1121 struct ct_mixer *mixer = NULL;
1123 /* disconnect internal mixer objects */
1124 if (NULL != atc->mixer) {
1125 mixer = atc->mixer;
1126 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1127 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1128 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1129 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1130 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1131 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
1134 if (NULL != atc->daios) {
1135 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1136 for (i = 0; i < atc->n_daio; i++) {
1137 daio = atc->daios[i];
1138 if (daio->type < LINEIM) {
1139 dao = container_of(daio, struct dao, daio);
1140 dao->ops->clear_left_input(dao);
1141 dao->ops->clear_right_input(dao);
1142 } else {
1143 dai = container_of(daio, struct dai, daio);
1144 /* some thing to do for dai ... */
1146 daio_mgr->put_daio(daio_mgr, daio);
1148 kfree(atc->daios);
1149 atc->daios = NULL;
1152 if (NULL != atc->pcm) {
1153 sum_mgr = atc->rsc_mgrs[SUM];
1154 for (i = 0; i < atc->n_pcm; i++)
1155 sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1157 kfree(atc->pcm);
1158 atc->pcm = NULL;
1161 if (NULL != atc->srcs) {
1162 src_mgr = atc->rsc_mgrs[SRC];
1163 for (i = 0; i < atc->n_src; i++)
1164 src_mgr->put_src(src_mgr, atc->srcs[i]);
1166 kfree(atc->srcs);
1167 atc->srcs = NULL;
1170 if (NULL != atc->srcimps) {
1171 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1172 for (i = 0; i < atc->n_srcimp; i++) {
1173 srcimp = atc->srcimps[i];
1174 srcimp->ops->unmap(srcimp);
1175 srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1177 kfree(atc->srcimps);
1178 atc->srcimps = NULL;
1181 return 0;
1184 static int ct_atc_destroy(struct ct_atc *atc)
1186 int i = 0;
1188 if (NULL == atc)
1189 return 0;
1191 if (atc->timer) {
1192 ct_timer_free(atc->timer);
1193 atc->timer = NULL;
1196 atc_release_resources(atc);
1198 /* Destroy internal mixer objects */
1199 if (NULL != atc->mixer)
1200 ct_mixer_destroy(atc->mixer);
1202 for (i = 0; i < NUM_RSCTYP; i++) {
1203 if ((NULL != rsc_mgr_funcs[i].destroy) &&
1204 (NULL != atc->rsc_mgrs[i]))
1205 rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1209 if (NULL != atc->hw)
1210 destroy_hw_obj((struct hw *)atc->hw);
1212 /* Destroy device virtual memory manager object */
1213 if (NULL != atc->vm) {
1214 ct_vm_destroy(atc->vm);
1215 atc->vm = NULL;
1218 kfree(atc);
1220 return 0;
1223 static int atc_dev_free(struct snd_device *dev)
1225 struct ct_atc *atc = dev->device_data;
1226 return ct_atc_destroy(atc);
1229 static int __devinit atc_identify_card(struct ct_atc *atc)
1231 const struct snd_pci_quirk *p;
1232 const struct snd_pci_quirk *list;
1234 switch (atc->chip_type) {
1235 case ATC20K1:
1236 atc->chip_name = "20K1";
1237 list = subsys_20k1_list;
1238 break;
1239 case ATC20K2:
1240 atc->chip_name = "20K2";
1241 list = subsys_20k2_list;
1242 break;
1243 default:
1244 return -ENOENT;
1246 p = snd_pci_quirk_lookup(atc->pci, list);
1247 if (p) {
1248 if (p->value < 0) {
1249 printk(KERN_ERR "ctxfi: "
1250 "Device %04x:%04x is black-listed\n",
1251 atc->pci->subsystem_vendor,
1252 atc->pci->subsystem_device);
1253 return -ENOENT;
1255 atc->model = p->value;
1256 } else {
1257 if (atc->chip_type == ATC20K1)
1258 atc->model = CT20K1_UNKNOWN;
1259 else
1260 atc->model = CT20K2_UNKNOWN;
1262 atc->model_name = ct_subsys_name[atc->model];
1263 snd_printd("ctxfi: chip %s model %s (%04x:%04x) is found\n",
1264 atc->chip_name, atc->model_name,
1265 atc->pci->subsystem_vendor,
1266 atc->pci->subsystem_device);
1267 return 0;
1270 int __devinit ct_atc_create_alsa_devs(struct ct_atc *atc)
1272 enum CTALSADEVS i;
1273 int err;
1275 alsa_dev_funcs[MIXER].public_name = atc->chip_name;
1277 for (i = 0; i < NUM_CTALSADEVS; i++) {
1278 if (NULL == alsa_dev_funcs[i].create)
1279 continue;
1281 err = alsa_dev_funcs[i].create(atc, i,
1282 alsa_dev_funcs[i].public_name);
1283 if (err) {
1284 printk(KERN_ERR "ctxfi: "
1285 "Creating alsa device %d failed!\n", i);
1286 return err;
1290 return 0;
1293 static int __devinit atc_create_hw_devs(struct ct_atc *atc)
1295 struct hw *hw;
1296 struct card_conf info = {0};
1297 int i, err;
1299 err = create_hw_obj(atc->pci, atc->chip_type, atc->model, &hw);
1300 if (err) {
1301 printk(KERN_ERR "Failed to create hw obj!!!\n");
1302 return err;
1304 atc->hw = hw;
1306 /* Initialize card hardware. */
1307 info.rsr = atc->rsr;
1308 info.msr = atc->msr;
1309 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1310 err = hw->card_init(hw, &info);
1311 if (err < 0)
1312 return err;
1314 for (i = 0; i < NUM_RSCTYP; i++) {
1315 if (NULL == rsc_mgr_funcs[i].create)
1316 continue;
1318 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1319 if (err) {
1320 printk(KERN_ERR "ctxfi: "
1321 "Failed to create rsc_mgr %d!!!\n", i);
1322 return err;
1326 return 0;
1329 static int atc_get_resources(struct ct_atc *atc)
1331 struct daio_desc da_desc = {0};
1332 struct daio_mgr *daio_mgr;
1333 struct src_desc src_dsc = {0};
1334 struct src_mgr *src_mgr;
1335 struct srcimp_desc srcimp_dsc = {0};
1336 struct srcimp_mgr *srcimp_mgr;
1337 struct sum_desc sum_dsc = {0};
1338 struct sum_mgr *sum_mgr;
1339 int err, i;
1341 atc->daios = kzalloc(sizeof(void *)*(DAIONUM), GFP_KERNEL);
1342 if (NULL == atc->daios)
1343 return -ENOMEM;
1345 atc->srcs = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1346 if (NULL == atc->srcs)
1347 return -ENOMEM;
1349 atc->srcimps = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1350 if (NULL == atc->srcimps)
1351 return -ENOMEM;
1353 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
1354 if (NULL == atc->pcm)
1355 return -ENOMEM;
1357 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1358 da_desc.msr = atc->msr;
1359 for (i = 0, atc->n_daio = 0; i < DAIONUM-1; i++) {
1360 da_desc.type = i;
1361 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1362 (struct daio **)&atc->daios[i]);
1363 if (err) {
1364 printk(KERN_ERR "ctxfi: Failed to get DAIO "
1365 "resource %d!!!\n", i);
1366 return err;
1368 atc->n_daio++;
1370 if (atc->model == CTSB073X)
1371 da_desc.type = SPDIFI1;
1372 else
1373 da_desc.type = SPDIFIO;
1374 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1375 (struct daio **)&atc->daios[i]);
1376 if (err) {
1377 printk(KERN_ERR "ctxfi: Failed to get S/PDIF-in resource!!!\n");
1378 return err;
1380 atc->n_daio++;
1382 src_mgr = atc->rsc_mgrs[SRC];
1383 src_dsc.multi = 1;
1384 src_dsc.msr = atc->msr;
1385 src_dsc.mode = ARCRW;
1386 for (i = 0, atc->n_src = 0; i < (2*2); i++) {
1387 err = src_mgr->get_src(src_mgr, &src_dsc,
1388 (struct src **)&atc->srcs[i]);
1389 if (err)
1390 return err;
1392 atc->n_src++;
1395 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1396 srcimp_dsc.msr = 8; /* SRCIMPs for S/PDIFIn SRT */
1397 for (i = 0, atc->n_srcimp = 0; i < (2*1); i++) {
1398 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1399 (struct srcimp **)&atc->srcimps[i]);
1400 if (err)
1401 return err;
1403 atc->n_srcimp++;
1405 srcimp_dsc.msr = 8; /* SRCIMPs for LINE/MICIn SRT */
1406 for (i = 0; i < (2*1); i++) {
1407 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1408 (struct srcimp **)&atc->srcimps[2*1+i]);
1409 if (err)
1410 return err;
1412 atc->n_srcimp++;
1415 sum_mgr = atc->rsc_mgrs[SUM];
1416 sum_dsc.msr = atc->msr;
1417 for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1418 err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1419 (struct sum **)&atc->pcm[i]);
1420 if (err)
1421 return err;
1423 atc->n_pcm++;
1426 return 0;
1429 static void
1430 atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1431 struct src **srcs, struct srcimp **srcimps)
1433 struct rsc *rscs[2] = {NULL};
1434 struct src *src;
1435 struct srcimp *srcimp;
1436 int i = 0;
1438 rscs[0] = &dai->daio.rscl;
1439 rscs[1] = &dai->daio.rscr;
1440 for (i = 0; i < 2; i++) {
1441 src = srcs[i];
1442 srcimp = srcimps[i];
1443 srcimp->ops->map(srcimp, src, rscs[i]);
1444 src_mgr->src_disable(src_mgr, src);
1447 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1449 src = srcs[0];
1450 src->ops->set_pm(src, 1);
1451 for (i = 0; i < 2; i++) {
1452 src = srcs[i];
1453 src->ops->set_state(src, SRC_STATE_RUN);
1454 src->ops->commit_write(src);
1455 src_mgr->src_enable_s(src_mgr, src);
1458 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1459 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1461 dai->ops->set_enb_src(dai, 1);
1462 dai->ops->set_enb_srt(dai, 1);
1463 dai->ops->commit_write(dai);
1465 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1468 static void atc_connect_resources(struct ct_atc *atc)
1470 struct dai *dai;
1471 struct dao *dao;
1472 struct src *src;
1473 struct sum *sum;
1474 struct ct_mixer *mixer;
1475 struct rsc *rscs[2] = {NULL};
1476 int i, j;
1478 mixer = atc->mixer;
1480 for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1481 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1482 dao = container_of(atc->daios[j], struct dao, daio);
1483 dao->ops->set_left_input(dao, rscs[0]);
1484 dao->ops->set_right_input(dao, rscs[1]);
1487 dai = container_of(atc->daios[LINEIM], struct dai, daio);
1488 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1489 (struct src **)&atc->srcs[2],
1490 (struct srcimp **)&atc->srcimps[2]);
1491 src = atc->srcs[2];
1492 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1493 src = atc->srcs[3];
1494 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1496 dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1497 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1498 (struct src **)&atc->srcs[0],
1499 (struct srcimp **)&atc->srcimps[0]);
1501 src = atc->srcs[0];
1502 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1503 src = atc->srcs[1];
1504 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1506 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1507 sum = atc->pcm[j];
1508 mixer->set_input_left(mixer, i, &sum->rsc);
1509 sum = atc->pcm[j+1];
1510 mixer->set_input_right(mixer, i, &sum->rsc);
1514 #ifdef CONFIG_PM
1515 static int atc_suspend(struct ct_atc *atc, pm_message_t state)
1517 int i;
1518 struct hw *hw = atc->hw;
1520 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D3hot);
1522 for (i = FRONT; i < NUM_PCMS; i++) {
1523 if (!atc->pcms[i])
1524 continue;
1526 snd_pcm_suspend_all(atc->pcms[i]);
1529 atc_release_resources(atc);
1531 hw->suspend(hw, state);
1533 return 0;
1536 static int atc_hw_resume(struct ct_atc *atc)
1538 struct hw *hw = atc->hw;
1539 struct card_conf info = {0};
1541 /* Re-initialize card hardware. */
1542 info.rsr = atc->rsr;
1543 info.msr = atc->msr;
1544 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1545 return hw->resume(hw, &info);
1548 static int atc_resources_resume(struct ct_atc *atc)
1550 struct ct_mixer *mixer;
1551 int err = 0;
1553 /* Get resources */
1554 err = atc_get_resources(atc);
1555 if (err < 0) {
1556 atc_release_resources(atc);
1557 return err;
1560 /* Build topology */
1561 atc_connect_resources(atc);
1563 mixer = atc->mixer;
1564 mixer->resume(mixer);
1566 return 0;
1569 static int atc_resume(struct ct_atc *atc)
1571 int err = 0;
1573 /* Do hardware resume. */
1574 err = atc_hw_resume(atc);
1575 if (err < 0) {
1576 printk(KERN_ERR "ctxfi: pci_enable_device failed, "
1577 "disabling device\n");
1578 snd_card_disconnect(atc->card);
1579 return err;
1582 err = atc_resources_resume(atc);
1583 if (err < 0)
1584 return err;
1586 snd_power_change_state(atc->card, SNDRV_CTL_POWER_D0);
1588 return 0;
1590 #endif
1592 static struct ct_atc atc_preset __devinitdata = {
1593 .map_audio_buffer = ct_map_audio_buffer,
1594 .unmap_audio_buffer = ct_unmap_audio_buffer,
1595 .pcm_playback_prepare = atc_pcm_playback_prepare,
1596 .pcm_release_resources = atc_pcm_release_resources,
1597 .pcm_playback_start = atc_pcm_playback_start,
1598 .pcm_playback_stop = atc_pcm_stop,
1599 .pcm_playback_position = atc_pcm_playback_position,
1600 .pcm_capture_prepare = atc_pcm_capture_prepare,
1601 .pcm_capture_start = atc_pcm_capture_start,
1602 .pcm_capture_stop = atc_pcm_stop,
1603 .pcm_capture_position = atc_pcm_capture_position,
1604 .spdif_passthru_playback_prepare = spdif_passthru_playback_prepare,
1605 .get_ptp_phys = atc_get_ptp_phys,
1606 .select_line_in = atc_select_line_in,
1607 .select_mic_in = atc_select_mic_in,
1608 .select_digit_io = atc_select_digit_io,
1609 .line_front_unmute = atc_line_front_unmute,
1610 .line_surround_unmute = atc_line_surround_unmute,
1611 .line_clfe_unmute = atc_line_clfe_unmute,
1612 .line_rear_unmute = atc_line_rear_unmute,
1613 .line_in_unmute = atc_line_in_unmute,
1614 .spdif_out_unmute = atc_spdif_out_unmute,
1615 .spdif_in_unmute = atc_spdif_in_unmute,
1616 .spdif_out_get_status = atc_spdif_out_get_status,
1617 .spdif_out_set_status = atc_spdif_out_set_status,
1618 .spdif_out_passthru = atc_spdif_out_passthru,
1619 .have_digit_io_switch = atc_have_digit_io_switch,
1620 #ifdef CONFIG_PM
1621 .suspend = atc_suspend,
1622 .resume = atc_resume,
1623 #endif
1627 * ct_atc_create - create and initialize a hardware manager
1628 * @card: corresponding alsa card object
1629 * @pci: corresponding kernel pci device object
1630 * @ratc: return created object address in it
1632 * Creates and initializes a hardware manager.
1634 * Creates kmallocated ct_atc structure. Initializes hardware.
1635 * Returns 0 if suceeds, or negative error code if fails.
1638 int __devinit ct_atc_create(struct snd_card *card, struct pci_dev *pci,
1639 unsigned int rsr, unsigned int msr,
1640 int chip_type, struct ct_atc **ratc)
1642 struct ct_atc *atc;
1643 static struct snd_device_ops ops = {
1644 .dev_free = atc_dev_free,
1646 int err;
1648 *ratc = NULL;
1650 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
1651 if (NULL == atc)
1652 return -ENOMEM;
1654 /* Set operations */
1655 *atc = atc_preset;
1657 atc->card = card;
1658 atc->pci = pci;
1659 atc->rsr = rsr;
1660 atc->msr = msr;
1661 atc->chip_type = chip_type;
1663 mutex_init(&atc->atc_mutex);
1665 /* Find card model */
1666 err = atc_identify_card(atc);
1667 if (err < 0) {
1668 printk(KERN_ERR "ctatc: Card not recognised\n");
1669 goto error1;
1672 /* Set up device virtual memory management object */
1673 err = ct_vm_create(&atc->vm);
1674 if (err < 0)
1675 goto error1;
1677 /* Create all atc hw devices */
1678 err = atc_create_hw_devs(atc);
1679 if (err < 0)
1680 goto error1;
1682 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1683 if (err) {
1684 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
1685 goto error1;
1688 /* Get resources */
1689 err = atc_get_resources(atc);
1690 if (err < 0)
1691 goto error1;
1693 /* Build topology */
1694 atc_connect_resources(atc);
1696 atc->timer = ct_timer_new(atc);
1697 if (!atc->timer)
1698 goto error1;
1700 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1701 if (err < 0)
1702 goto error1;
1704 snd_card_set_dev(card, &pci->dev);
1706 *ratc = atc;
1707 return 0;
1709 error1:
1710 ct_atc_destroy(atc);
1711 printk(KERN_ERR "ctxfi: Something wrong!!!\n");
1712 return err;