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.
11 * This file contains the implementation of the device resource management
21 #include "cthardware.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 */
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) \
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,
49 SND_PCI_QUIRK_VENDOR(PCI_VENDOR_ID_CREATIVE,
50 "Unknown", CT20K1_UNKNOWN),
54 static struct snd_pci_quirk __devinitdata subsys_20k2_list[] = {
55 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB0760,
57 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08801,
59 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08802,
61 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08803,
63 SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000,
64 PCI_SUBDEVICE_ID_CREATIVE_HENDRIX, "HENDRIX",
69 static const char *ct_subsys_name[NUM_CTCARDS] = {
70 [CTSB055X] = "SB055x",
71 [CTSB073X] = "SB073x",
72 [CTSB0760] = "SB076x",
74 [CT20K1_UNKNOWN] = "Unknown",
75 [CTHENDRIX] = "Hendrix",
76 [CTSB0880] = "SB0880",
80 int (*create)(struct ct_atc *atc,
81 enum CTALSADEVS device, const char *device_name);
82 int (*destroy)(void *alsa_dev);
83 const char *public_name;
84 } alsa_dev_funcs[NUM_CTALSADEVS] = {
85 [FRONT] = { .create = ct_alsa_pcm_create,
87 .public_name = "Front/WaveIn"},
88 [SURROUND] = { .create = ct_alsa_pcm_create,
90 .public_name = "Surround"},
91 [CLFE] = { .create = ct_alsa_pcm_create,
93 .public_name = "Center/LFE"},
94 [SIDE] = { .create = ct_alsa_pcm_create,
96 .public_name = "Side"},
97 [IEC958] = { .create = ct_alsa_pcm_create,
99 .public_name = "IEC958 Non-audio"},
101 [MIXER] = { .create = ct_alsa_mix_create,
103 .public_name = "Mixer"}
106 typedef int (*create_t)(void *, void **);
107 typedef int (*destroy_t)(void *);
110 int (*create)(void *hw, void **rmgr);
111 int (*destroy)(void *mgr);
112 } rsc_mgr_funcs[NUM_RSCTYP] = {
113 [SRC] = { .create = (create_t)src_mgr_create,
114 .destroy = (destroy_t)src_mgr_destroy },
115 [SRCIMP] = { .create = (create_t)srcimp_mgr_create,
116 .destroy = (destroy_t)srcimp_mgr_destroy },
117 [AMIXER] = { .create = (create_t)amixer_mgr_create,
118 .destroy = (destroy_t)amixer_mgr_destroy },
119 [SUM] = { .create = (create_t)sum_mgr_create,
120 .destroy = (destroy_t)sum_mgr_destroy },
121 [DAIO] = { .create = (create_t)daio_mgr_create,
122 .destroy = (destroy_t)daio_mgr_destroy }
126 atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
129 * Only mono and interleaved modes are supported now.
130 * Always allocates a contiguous channel block.
133 static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
135 struct snd_pcm_runtime *runtime;
138 if (NULL == apcm->substream)
141 runtime = apcm->substream->runtime;
144 apcm->vm_block = vm->map(vm, apcm->substream, runtime->dma_bytes);
146 if (NULL == apcm->vm_block)
152 static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
156 if (NULL == apcm->vm_block)
161 vm->unmap(vm, apcm->vm_block);
163 apcm->vm_block = NULL;
166 static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
170 unsigned long phys_addr;
173 kvirt_addr = vm->get_ptp_virt(vm, index);
174 if (kvirt_addr == NULL)
177 phys_addr = virt_to_phys(kvirt_addr);
182 static unsigned int convert_format(snd_pcm_format_t snd_format)
184 switch (snd_format) {
185 case SNDRV_PCM_FORMAT_U8:
187 case SNDRV_PCM_FORMAT_S16_LE:
189 case SNDRV_PCM_FORMAT_S24_3LE:
191 case SNDRV_PCM_FORMAT_S32_LE:
193 case SNDRV_PCM_FORMAT_FLOAT_LE:
196 printk(KERN_ERR "ctxfi: not recognized snd format is %d \n",
203 atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
208 /* get pitch and convert to fixed-point 8.24 format. */
209 pitch = (input_rate / output_rate) << 24;
210 input_rate %= output_rate;
213 for (b = 31; ((b >= 0) && !(input_rate >> b)); )
217 input_rate <<= (31 - b);
218 input_rate /= output_rate;
231 static int select_rom(unsigned int pitch)
233 if ((pitch > 0x00428f5c) && (pitch < 0x01b851ec)) {
234 /* 0.26 <= pitch <= 1.72 */
236 } else if ((0x01d66666 == pitch) || (0x01d66667 == pitch)) {
237 /* pitch == 1.8375 */
239 } else if (0x02000000 == pitch) {
242 } else if ((pitch >= 0x0) && (pitch <= 0x08000000)) {
243 /* 0 <= pitch <= 8 */
250 static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
252 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
253 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
254 struct src_desc desc = {0};
255 struct amixer_desc mix_dsc = {0};
257 struct amixer *amixer;
259 int n_amixer = apcm->substream->runtime->channels, i = 0;
260 int device = apcm->substream->pcm->device;
264 if (NULL != apcm->src) {
265 /* Prepared pcm playback */
269 /* first release old resources */
270 atc->pcm_release_resources(atc, apcm);
272 /* Get SRC resource */
273 desc.multi = apcm->substream->runtime->channels;
276 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
280 pitch = atc_get_pitch(apcm->substream->runtime->rate,
281 (atc->rsr * atc->msr));
283 src->ops->set_pitch(src, pitch);
284 src->ops->set_rom(src, select_rom(pitch));
285 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
286 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
288 /* Get AMIXER resource */
289 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
290 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
291 if (NULL == apcm->amixers) {
295 mix_dsc.msr = atc->msr;
296 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
297 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
298 (struct amixer **)&apcm->amixers[i]);
305 /* Set up device virtual mem map */
306 err = ct_map_audio_buffer(atc, apcm);
310 /* Connect resources */
312 for (i = 0; i < n_amixer; i++) {
313 amixer = apcm->amixers[i];
314 spin_lock_irqsave(&atc->atc_lock, flags);
315 amixer->ops->setup(amixer, &src->rsc,
316 INIT_VOL, atc->pcm[i+device*2]);
317 spin_unlock_irqrestore(&atc->atc_lock, flags);
318 src = src->ops->next_interleave(src);
323 ct_timer_prepare(apcm->timer);
328 atc_pcm_release_resources(atc, apcm);
333 atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
335 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
336 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
337 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
338 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
339 struct srcimp *srcimp;
342 if (NULL != apcm->srcimps) {
343 for (i = 0; i < apcm->n_srcimp; i++) {
344 srcimp = apcm->srcimps[i];
345 srcimp->ops->unmap(srcimp);
346 srcimp_mgr->put_srcimp(srcimp_mgr, srcimp);
347 apcm->srcimps[i] = NULL;
349 kfree(apcm->srcimps);
350 apcm->srcimps = NULL;
353 if (NULL != apcm->srccs) {
354 for (i = 0; i < apcm->n_srcc; i++) {
355 src_mgr->put_src(src_mgr, apcm->srccs[i]);
356 apcm->srccs[i] = NULL;
362 if (NULL != apcm->amixers) {
363 for (i = 0; i < apcm->n_amixer; i++) {
364 amixer_mgr->put_amixer(amixer_mgr, apcm->amixers[i]);
365 apcm->amixers[i] = NULL;
367 kfree(apcm->amixers);
368 apcm->amixers = NULL;
371 if (NULL != apcm->mono) {
372 sum_mgr->put_sum(sum_mgr, apcm->mono);
376 if (NULL != apcm->src) {
377 src_mgr->put_src(src_mgr, apcm->src);
381 if (NULL != apcm->vm_block) {
382 /* Undo device virtual mem map */
383 ct_unmap_audio_buffer(atc, apcm);
384 apcm->vm_block = NULL;
390 static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
392 unsigned int max_cisz;
393 struct src *src = apcm->src;
395 max_cisz = src->multi * src->rsc.msr;
396 max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
398 src->ops->set_sa(src, apcm->vm_block->addr);
399 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
400 src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
401 src->ops->set_cisz(src, max_cisz);
403 src->ops->set_bm(src, 1);
404 src->ops->set_state(src, SRC_STATE_INIT);
405 src->ops->commit_write(src);
407 ct_timer_start(apcm->timer);
411 static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
416 ct_timer_stop(apcm->timer);
419 src->ops->set_bm(src, 0);
420 src->ops->set_state(src, SRC_STATE_OFF);
421 src->ops->commit_write(src);
423 if (NULL != apcm->srccs) {
424 for (i = 0; i < apcm->n_srcc; i++) {
425 src = apcm->srccs[i];
426 src->ops->set_bm(src, 0);
427 src->ops->set_state(src, SRC_STATE_OFF);
428 src->ops->commit_write(src);
438 atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
440 struct src *src = apcm->src;
444 position = src->ops->get_ca(src);
446 size = apcm->vm_block->size;
447 max_cisz = src->multi * src->rsc.msr;
448 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
450 return (position + size - max_cisz - apcm->vm_block->addr) % size;
453 struct src_node_conf_t {
456 unsigned int mix_msr:8;
457 unsigned int imp_msr:8;
461 static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
462 struct src_node_conf_t *conf, int *n_srcc)
466 /* get pitch and convert to fixed-point 8.24 format. */
467 pitch = atc_get_pitch((atc->rsr * atc->msr),
468 apcm->substream->runtime->rate);
472 *n_srcc = apcm->substream->runtime->channels;
473 conf[0].pitch = pitch;
474 conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
476 } else if (2 == atc->msr) {
477 if (0x8000000 < pitch) {
478 /* Need two-stage SRCs, SRCIMPs and
479 * AMIXERs for converting format */
480 conf[0].pitch = (atc->msr << 24);
481 conf[0].msr = conf[0].mix_msr = 1;
482 conf[0].imp_msr = atc->msr;
484 conf[1].pitch = atc_get_pitch(atc->rsr,
485 apcm->substream->runtime->rate);
486 conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
488 *n_srcc = apcm->substream->runtime->channels * 2;
489 } else if (0x1000000 < pitch) {
490 /* Need one-stage SRCs, SRCIMPs and
491 * AMIXERs for converting format */
492 conf[0].pitch = pitch;
493 conf[0].msr = conf[0].mix_msr
494 = conf[0].imp_msr = atc->msr;
496 *n_srcc = apcm->substream->runtime->channels;
502 atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
504 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
505 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
506 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
507 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
508 struct src_desc src_dsc = {0};
510 struct srcimp_desc srcimp_dsc = {0};
511 struct srcimp *srcimp;
512 struct amixer_desc mix_dsc = {0};
513 struct sum_desc sum_dsc = {0};
516 int n_srcimp, n_amixer, n_srcc, n_sum;
517 struct src_node_conf_t src_node_conf[2] = {{0} };
519 /* first release old resources */
520 atc->pcm_release_resources(atc, apcm);
522 /* The numbers of converting SRCs and SRCIMPs should be determined
525 multi = apcm->substream->runtime->channels;
527 /* get pitch and convert to fixed-point 8.24 format. */
528 pitch = atc_get_pitch((atc->rsr * atc->msr),
529 apcm->substream->runtime->rate);
531 setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
532 n_sum = (1 == multi) ? 1 : 0;
533 n_amixer = n_sum * 2 + n_srcc;
535 if ((multi > 1) && (0x8000000 >= pitch)) {
536 /* Need extra AMIXERs and SRCIMPs for special treatment
537 * of interleaved recording of conjugate channels */
538 n_amixer += multi * atc->msr;
539 n_srcimp += multi * atc->msr;
545 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
546 if (NULL == apcm->srccs)
550 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
551 if (NULL == apcm->amixers) {
556 apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
557 if (NULL == apcm->srcimps) {
562 /* Allocate SRCs for sample rate conversion if needed */
564 src_dsc.mode = ARCRW;
565 for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
566 src_dsc.msr = src_node_conf[i/multi].msr;
567 err = src_mgr->get_src(src_mgr, &src_dsc,
568 (struct src **)&apcm->srccs[i]);
572 src = apcm->srccs[i];
573 pitch = src_node_conf[i/multi].pitch;
574 src->ops->set_pitch(src, pitch);
575 src->ops->set_rom(src, select_rom(pitch));
576 src->ops->set_vo(src, src_node_conf[i/multi].vo);
581 /* Allocate AMIXERs for routing SRCs of conversion if needed */
582 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
584 mix_dsc.msr = atc->msr;
585 else if (i < (n_sum*2+n_srcc))
586 mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
590 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
591 (struct amixer **)&apcm->amixers[i]);
598 /* Allocate a SUM resource to mix all input channels together */
599 sum_dsc.msr = atc->msr;
600 err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
604 pitch = atc_get_pitch((atc->rsr * atc->msr),
605 apcm->substream->runtime->rate);
606 /* Allocate SRCIMP resources */
607 for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
609 srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
611 srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
615 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
619 apcm->srcimps[i] = srcimp;
623 /* Allocate a SRC for writing data to host memory */
624 src_dsc.multi = apcm->substream->runtime->channels;
626 src_dsc.mode = MEMWR;
627 err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
632 src->ops->set_pitch(src, pitch);
634 /* Set up device virtual mem map */
635 err = ct_map_audio_buffer(atc, apcm);
642 atc_pcm_release_resources(atc, apcm);
646 static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
649 struct amixer *amixer;
650 struct srcimp *srcimp;
651 struct ct_mixer *mixer = atc->mixer;
653 struct rsc *out_ports[8] = {NULL};
654 int err, i, j, n_sum, multi;
656 int mix_base = 0, imp_base = 0;
658 if (NULL != apcm->src) {
659 /* Prepared pcm capture */
663 /* Get needed resources. */
664 err = atc_pcm_capture_get_resources(atc, apcm);
668 /* Connect resources */
669 mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
670 &out_ports[0], &out_ports[1]);
672 multi = apcm->substream->runtime->channels;
675 for (i = 0; i < 2; i++) {
676 amixer = apcm->amixers[i];
677 amixer->ops->setup(amixer, out_ports[i],
678 MONO_SUM_SCALE, mono);
680 out_ports[0] = &mono->rsc;
682 mix_base = n_sum * 2;
685 for (i = 0; i < apcm->n_srcc; i++) {
686 src = apcm->srccs[i];
687 srcimp = apcm->srcimps[imp_base+i];
688 amixer = apcm->amixers[mix_base+i];
689 srcimp->ops->map(srcimp, src, out_ports[i%multi]);
690 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
691 out_ports[i%multi] = &amixer->rsc;
694 pitch = atc_get_pitch((atc->rsr * atc->msr),
695 apcm->substream->runtime->rate);
697 if ((multi > 1) && (pitch <= 0x8000000)) {
698 /* Special connection for interleaved
699 * recording with conjugate channels */
700 for (i = 0; i < multi; i++) {
701 out_ports[i]->ops->master(out_ports[i]);
702 for (j = 0; j < atc->msr; j++) {
703 amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
704 amixer->ops->set_input(amixer, out_ports[i]);
705 amixer->ops->set_scale(amixer, INIT_VOL);
706 amixer->ops->set_sum(amixer, NULL);
707 amixer->ops->commit_raw_write(amixer);
708 out_ports[i]->ops->next_conj(out_ports[i]);
710 srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
711 srcimp->ops->map(srcimp, apcm->src,
716 for (i = 0; i < multi; i++) {
717 srcimp = apcm->srcimps[apcm->n_srcc+i];
718 srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
722 ct_timer_prepare(apcm->timer);
727 static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
730 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
737 multi = apcm->substream->runtime->channels;
738 /* Set up converting SRCs */
739 for (i = 0; i < apcm->n_srcc; i++) {
740 src = apcm->srccs[i];
741 src->ops->set_pm(src, ((i%multi) != (multi-1)));
742 src_mgr->src_disable(src_mgr, src);
745 /* Set up recording SRC */
747 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
748 src->ops->set_sa(src, apcm->vm_block->addr);
749 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
750 src->ops->set_ca(src, apcm->vm_block->addr);
751 src_mgr->src_disable(src_mgr, src);
753 /* Disable relevant SRCs firstly */
754 src_mgr->commit_write(src_mgr);
756 /* Enable SRCs respectively */
757 for (i = 0; i < apcm->n_srcc; i++) {
758 src = apcm->srccs[i];
759 src->ops->set_state(src, SRC_STATE_RUN);
760 src->ops->commit_write(src);
761 src_mgr->src_enable_s(src_mgr, src);
764 src->ops->set_bm(src, 1);
765 src->ops->set_state(src, SRC_STATE_RUN);
766 src->ops->commit_write(src);
767 src_mgr->src_enable_s(src_mgr, src);
769 /* Enable relevant SRCs synchronously */
770 src_mgr->commit_write(src_mgr);
772 ct_timer_start(apcm->timer);
777 atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
779 struct src *src = apcm->src;
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};
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;
802 while (apcm->substream->runtime->rate > (rsr * desc.msr))
806 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
810 pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
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) {
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]);
835 /* Set up device virtual mem map */
836 err = ct_map_audio_buffer(atc, apcm);
843 atc_pcm_release_resources(atc, apcm);
847 static int atc_pll_init(struct ct_atc *atc, int rate)
849 struct hw *hw = atc->hw;
851 err = hw->pll_init(hw, rate);
852 atc->pll_rate = err ? 0 : rate;
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);
861 unsigned int rate = apcm->substream->runtime->rate;
864 unsigned char iec958_con_fs;
868 iec958_con_fs = IEC958_AES3_CON_FS_48000;
871 iec958_con_fs = IEC958_AES3_CON_FS_44100;
874 iec958_con_fs = IEC958_AES3_CON_FS_32000;
880 spin_lock_irqsave(&atc->atc_lock, flags);
881 dao->ops->get_spos(dao, &status);
882 if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
883 status &= ((~IEC958_AES3_CON_FS) << 24);
884 status |= (iec958_con_fs << 24);
885 dao->ops->set_spos(dao, status);
886 dao->ops->commit_write(dao);
888 if ((rate != atc->pll_rate) && (32000 != rate))
889 err = atc_pll_init(atc, rate);
890 spin_unlock_irqrestore(&atc->atc_lock, flags);
896 spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
899 struct amixer *amixer;
905 if (NULL != apcm->src)
908 /* Configure SPDIFOO and PLL to passthrough mode;
909 * determine pll_rate. */
910 err = spdif_passthru_playback_setup(atc, apcm);
914 /* Get needed resources. */
915 err = spdif_passthru_playback_get_resources(atc, apcm);
919 /* Connect resources */
921 for (i = 0; i < apcm->n_amixer; i++) {
922 amixer = apcm->amixers[i];
923 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
924 src = src->ops->next_interleave(src);
928 /* Connect to SPDIFOO */
929 spin_lock_irqsave(&atc->atc_lock, flags);
930 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
931 amixer = apcm->amixers[0];
932 dao->ops->set_left_input(dao, &amixer->rsc);
933 amixer = apcm->amixers[1];
934 dao->ops->set_right_input(dao, &amixer->rsc);
935 spin_unlock_irqrestore(&atc->atc_lock, flags);
937 ct_timer_prepare(apcm->timer);
942 static int atc_select_line_in(struct ct_atc *atc)
944 struct hw *hw = atc->hw;
945 struct ct_mixer *mixer = atc->mixer;
948 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
951 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
952 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
954 hw->select_adc_source(hw, ADC_LINEIN);
957 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
959 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
964 static int atc_select_mic_in(struct ct_atc *atc)
966 struct hw *hw = atc->hw;
967 struct ct_mixer *mixer = atc->mixer;
970 if (hw->is_adc_source_selected(hw, ADC_MICIN))
973 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
974 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
976 hw->select_adc_source(hw, ADC_MICIN);
979 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
981 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
986 static int atc_have_digit_io_switch(struct ct_atc *atc)
988 struct hw *hw = atc->hw;
990 return hw->have_digit_io_switch(hw);
993 static int atc_select_digit_io(struct ct_atc *atc)
995 struct hw *hw = atc->hw;
997 if (hw->is_adc_source_selected(hw, ADC_NONE))
1000 hw->select_adc_source(hw, ADC_NONE);
1005 static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
1007 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
1010 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
1012 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
1014 daio_mgr->commit_write(daio_mgr);
1020 atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
1022 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1023 return dao->ops->get_spos(dao, status);
1027 atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
1029 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1031 dao->ops->set_spos(dao, status);
1032 dao->ops->commit_write(dao);
1036 static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1038 return atc_daio_unmute(atc, state, LINEO1);
1041 static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1043 return atc_daio_unmute(atc, state, LINEO4);
1046 static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1048 return atc_daio_unmute(atc, state, LINEO3);
1051 static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1053 return atc_daio_unmute(atc, state, LINEO2);
1056 static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1058 return atc_daio_unmute(atc, state, LINEIM);
1061 static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1063 return atc_daio_unmute(atc, state, SPDIFOO);
1066 static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1068 return atc_daio_unmute(atc, state, SPDIFIO);
1071 static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1073 return atc_dao_get_status(atc, status, SPDIFOO);
1076 static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1078 return atc_dao_set_status(atc, status, SPDIFOO);
1081 static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1083 unsigned long flags;
1084 struct dao_desc da_dsc = {0};
1087 struct ct_mixer *mixer = atc->mixer;
1088 struct rsc *rscs[2] = {NULL};
1089 unsigned int spos = 0;
1091 spin_lock_irqsave(&atc->atc_lock, flags);
1092 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1093 da_dsc.msr = state ? 1 : atc->msr;
1094 da_dsc.passthru = state ? 1 : 0;
1095 err = dao->ops->reinit(dao, &da_dsc);
1097 spos = IEC958_DEFAULT_CON;
1099 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1100 &rscs[0], &rscs[1]);
1101 dao->ops->set_left_input(dao, rscs[0]);
1102 dao->ops->set_right_input(dao, rscs[1]);
1103 /* Restore PLL to atc->rsr if needed. */
1104 if (atc->pll_rate != atc->rsr)
1105 err = atc_pll_init(atc, atc->rsr);
1107 dao->ops->set_spos(dao, spos);
1108 dao->ops->commit_write(dao);
1109 spin_unlock_irqrestore(&atc->atc_lock, flags);
1114 static int ct_atc_destroy(struct ct_atc *atc)
1116 struct daio_mgr *daio_mgr;
1120 struct sum_mgr *sum_mgr;
1121 struct src_mgr *src_mgr;
1122 struct srcimp_mgr *srcimp_mgr;
1123 struct srcimp *srcimp;
1124 struct ct_mixer *mixer;
1131 ct_timer_free(atc->timer);
1135 /* Stop hardware and disable all interrupts */
1136 if (NULL != atc->hw)
1137 ((struct hw *)atc->hw)->card_stop(atc->hw);
1139 /* Destroy internal mixer objects */
1140 if (NULL != atc->mixer) {
1142 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1143 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1144 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1145 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1146 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1147 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
1148 ct_mixer_destroy(atc->mixer);
1151 if (NULL != atc->daios) {
1152 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1153 for (i = 0; i < atc->n_daio; i++) {
1154 daio = atc->daios[i];
1155 if (daio->type < LINEIM) {
1156 dao = container_of(daio, struct dao, daio);
1157 dao->ops->clear_left_input(dao);
1158 dao->ops->clear_right_input(dao);
1160 dai = container_of(daio, struct dai, daio);
1161 /* some thing to do for dai ... */
1163 daio_mgr->put_daio(daio_mgr, daio);
1168 if (NULL != atc->pcm) {
1169 sum_mgr = atc->rsc_mgrs[SUM];
1170 for (i = 0; i < atc->n_pcm; i++)
1171 sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1176 if (NULL != atc->srcs) {
1177 src_mgr = atc->rsc_mgrs[SRC];
1178 for (i = 0; i < atc->n_src; i++)
1179 src_mgr->put_src(src_mgr, atc->srcs[i]);
1184 if (NULL != atc->srcimps) {
1185 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1186 for (i = 0; i < atc->n_srcimp; i++) {
1187 srcimp = atc->srcimps[i];
1188 srcimp->ops->unmap(srcimp);
1189 srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1191 kfree(atc->srcimps);
1194 for (i = 0; i < NUM_RSCTYP; i++) {
1195 if ((NULL != rsc_mgr_funcs[i].destroy) &&
1196 (NULL != atc->rsc_mgrs[i]))
1197 rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1201 if (NULL != atc->hw)
1202 destroy_hw_obj((struct hw *)atc->hw);
1204 /* Destroy device virtual memory manager object */
1205 if (NULL != atc->vm) {
1206 ct_vm_destroy(atc->vm);
1215 static int atc_dev_free(struct snd_device *dev)
1217 struct ct_atc *atc = dev->device_data;
1218 return ct_atc_destroy(atc);
1221 static int __devinit atc_identify_card(struct ct_atc *atc)
1223 const struct snd_pci_quirk *p;
1224 const struct snd_pci_quirk *list;
1226 switch (atc->chip_type) {
1228 atc->chip_name = "20K1";
1229 list = subsys_20k1_list;
1232 atc->chip_name = "20K2";
1233 list = subsys_20k2_list;
1238 p = snd_pci_quirk_lookup(atc->pci, list);
1241 atc->model = p->value;
1242 atc->model_name = ct_subsys_name[atc->model];
1243 snd_printd("ctxfi: chip %s model %s (%04x:%04x) is found\n",
1244 atc->chip_name, atc->model_name,
1245 atc->pci->subsystem_vendor,
1246 atc->pci->subsystem_device);
1250 int __devinit ct_atc_create_alsa_devs(struct ct_atc *atc)
1255 alsa_dev_funcs[MIXER].public_name = atc->chip_name;
1257 for (i = 0; i < NUM_CTALSADEVS; i++) {
1258 if (NULL == alsa_dev_funcs[i].create)
1261 err = alsa_dev_funcs[i].create(atc, i,
1262 alsa_dev_funcs[i].public_name);
1264 printk(KERN_ERR "ctxfi: "
1265 "Creating alsa device %d failed!\n", i);
1273 static int __devinit atc_create_hw_devs(struct ct_atc *atc)
1276 struct card_conf info = {0};
1279 err = create_hw_obj(atc->pci, atc->chip_type, atc->model, &hw);
1281 printk(KERN_ERR "Failed to create hw obj!!!\n");
1286 /* Initialize card hardware. */
1287 info.rsr = atc->rsr;
1288 info.msr = atc->msr;
1289 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1290 err = hw->card_init(hw, &info);
1294 for (i = 0; i < NUM_RSCTYP; i++) {
1295 if (NULL == rsc_mgr_funcs[i].create)
1298 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1300 printk(KERN_ERR "ctxfi: "
1301 "Failed to create rsc_mgr %d!!!\n", i);
1309 static int __devinit atc_get_resources(struct ct_atc *atc)
1311 struct daio_desc da_desc = {0};
1312 struct daio_mgr *daio_mgr;
1313 struct src_desc src_dsc = {0};
1314 struct src_mgr *src_mgr;
1315 struct srcimp_desc srcimp_dsc = {0};
1316 struct srcimp_mgr *srcimp_mgr;
1317 struct sum_desc sum_dsc = {0};
1318 struct sum_mgr *sum_mgr;
1321 atc->daios = kzalloc(sizeof(void *)*(DAIONUM), GFP_KERNEL);
1322 if (NULL == atc->daios)
1325 atc->srcs = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1326 if (NULL == atc->srcs)
1329 atc->srcimps = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1330 if (NULL == atc->srcimps)
1333 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
1334 if (NULL == atc->pcm)
1337 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1338 da_desc.msr = atc->msr;
1339 for (i = 0, atc->n_daio = 0; i < DAIONUM-1; i++) {
1341 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1342 (struct daio **)&atc->daios[i]);
1344 printk(KERN_ERR "ctxfi: Failed to get DAIO "
1345 "resource %d!!!\n", i);
1350 if (atc->model == CTSB073X)
1351 da_desc.type = SPDIFI1;
1353 da_desc.type = SPDIFIO;
1354 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1355 (struct daio **)&atc->daios[i]);
1357 printk(KERN_ERR "ctxfi: Failed to get S/PDIF-in resource!!!\n");
1362 src_mgr = atc->rsc_mgrs[SRC];
1364 src_dsc.msr = atc->msr;
1365 src_dsc.mode = ARCRW;
1366 for (i = 0, atc->n_src = 0; i < (2*2); i++) {
1367 err = src_mgr->get_src(src_mgr, &src_dsc,
1368 (struct src **)&atc->srcs[i]);
1375 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1376 srcimp_dsc.msr = 8; /* SRCIMPs for S/PDIFIn SRT */
1377 for (i = 0, atc->n_srcimp = 0; i < (2*1); i++) {
1378 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1379 (struct srcimp **)&atc->srcimps[i]);
1385 srcimp_dsc.msr = 8; /* SRCIMPs for LINE/MICIn SRT */
1386 for (i = 0; i < (2*1); i++) {
1387 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1388 (struct srcimp **)&atc->srcimps[2*1+i]);
1395 sum_mgr = atc->rsc_mgrs[SUM];
1396 sum_dsc.msr = atc->msr;
1397 for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1398 err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1399 (struct sum **)&atc->pcm[i]);
1406 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1408 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
1415 static void __devinit
1416 atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1417 struct src **srcs, struct srcimp **srcimps)
1419 struct rsc *rscs[2] = {NULL};
1421 struct srcimp *srcimp;
1424 rscs[0] = &dai->daio.rscl;
1425 rscs[1] = &dai->daio.rscr;
1426 for (i = 0; i < 2; i++) {
1428 srcimp = srcimps[i];
1429 srcimp->ops->map(srcimp, src, rscs[i]);
1430 src_mgr->src_disable(src_mgr, src);
1433 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1436 src->ops->set_pm(src, 1);
1437 for (i = 0; i < 2; i++) {
1439 src->ops->set_state(src, SRC_STATE_RUN);
1440 src->ops->commit_write(src);
1441 src_mgr->src_enable_s(src_mgr, src);
1444 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1445 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1447 dai->ops->set_enb_src(dai, 1);
1448 dai->ops->set_enb_srt(dai, 1);
1449 dai->ops->commit_write(dai);
1451 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1454 static void __devinit atc_connect_resources(struct ct_atc *atc)
1460 struct ct_mixer *mixer;
1461 struct rsc *rscs[2] = {NULL};
1466 for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1467 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1468 dao = container_of(atc->daios[j], struct dao, daio);
1469 dao->ops->set_left_input(dao, rscs[0]);
1470 dao->ops->set_right_input(dao, rscs[1]);
1473 dai = container_of(atc->daios[LINEIM], struct dai, daio);
1474 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1475 (struct src **)&atc->srcs[2],
1476 (struct srcimp **)&atc->srcimps[2]);
1478 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1480 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1482 dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1483 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1484 (struct src **)&atc->srcs[0],
1485 (struct srcimp **)&atc->srcimps[0]);
1488 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1490 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1492 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1494 mixer->set_input_left(mixer, i, &sum->rsc);
1495 sum = atc->pcm[j+1];
1496 mixer->set_input_right(mixer, i, &sum->rsc);
1500 static struct ct_atc atc_preset __devinitdata = {
1501 .map_audio_buffer = ct_map_audio_buffer,
1502 .unmap_audio_buffer = ct_unmap_audio_buffer,
1503 .pcm_playback_prepare = atc_pcm_playback_prepare,
1504 .pcm_release_resources = atc_pcm_release_resources,
1505 .pcm_playback_start = atc_pcm_playback_start,
1506 .pcm_playback_stop = atc_pcm_stop,
1507 .pcm_playback_position = atc_pcm_playback_position,
1508 .pcm_capture_prepare = atc_pcm_capture_prepare,
1509 .pcm_capture_start = atc_pcm_capture_start,
1510 .pcm_capture_stop = atc_pcm_stop,
1511 .pcm_capture_position = atc_pcm_capture_position,
1512 .spdif_passthru_playback_prepare = spdif_passthru_playback_prepare,
1513 .get_ptp_phys = atc_get_ptp_phys,
1514 .select_line_in = atc_select_line_in,
1515 .select_mic_in = atc_select_mic_in,
1516 .select_digit_io = atc_select_digit_io,
1517 .line_front_unmute = atc_line_front_unmute,
1518 .line_surround_unmute = atc_line_surround_unmute,
1519 .line_clfe_unmute = atc_line_clfe_unmute,
1520 .line_rear_unmute = atc_line_rear_unmute,
1521 .line_in_unmute = atc_line_in_unmute,
1522 .spdif_out_unmute = atc_spdif_out_unmute,
1523 .spdif_in_unmute = atc_spdif_in_unmute,
1524 .spdif_out_get_status = atc_spdif_out_get_status,
1525 .spdif_out_set_status = atc_spdif_out_set_status,
1526 .spdif_out_passthru = atc_spdif_out_passthru,
1527 .have_digit_io_switch = atc_have_digit_io_switch,
1531 * ct_atc_create - create and initialize a hardware manager
1532 * @card: corresponding alsa card object
1533 * @pci: corresponding kernel pci device object
1534 * @ratc: return created object address in it
1536 * Creates and initializes a hardware manager.
1538 * Creates kmallocated ct_atc structure. Initializes hardware.
1539 * Returns 0 if suceeds, or negative error code if fails.
1542 int __devinit ct_atc_create(struct snd_card *card, struct pci_dev *pci,
1543 unsigned int rsr, unsigned int msr,
1544 int chip_type, struct ct_atc **ratc)
1547 static struct snd_device_ops ops = {
1548 .dev_free = atc_dev_free,
1554 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
1558 /* Set operations */
1565 atc->chip_type = chip_type;
1567 spin_lock_init(&atc->atc_lock);
1569 /* Find card model */
1570 err = atc_identify_card(atc);
1572 printk(KERN_ERR "ctatc: Card not recognised\n");
1576 /* Set up device virtual memory management object */
1577 err = ct_vm_create(&atc->vm);
1581 /* Create all atc hw devices */
1582 err = atc_create_hw_devs(atc);
1587 err = atc_get_resources(atc);
1591 /* Build topology */
1592 atc_connect_resources(atc);
1594 atc->timer = ct_timer_new(atc);
1598 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1602 snd_card_set_dev(card, &pci->dev);
1608 ct_atc_destroy(atc);
1609 printk(KERN_ERR "ctxfi: Something wrong!!!\n");