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,
52 static struct snd_pci_quirk __devinitdata subsys_20k2_list[] = {
53 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB0760,
55 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08801,
57 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08802,
59 SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08803,
61 SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000,
62 PCI_SUBDEVICE_ID_CREATIVE_HENDRIX, "HENDRIX",
67 static const char *ct_subsys_name[NUM_CTCARDS] = {
69 [CTSB055X] = "SB055x",
70 [CTSB073X] = "SB073x",
72 [CT20K1_UNKNOWN] = "Unknown",
74 [CTSB0760] = "SB076x",
75 [CTHENDRIX] = "Hendrix",
76 [CTSB0880] = "SB0880",
77 [CT20K2_UNKNOWN] = "Unknown",
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,
88 .public_name = "Front/WaveIn"},
89 [SURROUND] = { .create = ct_alsa_pcm_create,
91 .public_name = "Surround"},
92 [CLFE] = { .create = ct_alsa_pcm_create,
94 .public_name = "Center/LFE"},
95 [SIDE] = { .create = ct_alsa_pcm_create,
97 .public_name = "Side"},
98 [IEC958] = { .create = ct_alsa_pcm_create,
100 .public_name = "IEC958 Non-audio"},
102 [MIXER] = { .create = ct_alsa_mix_create,
104 .public_name = "Mixer"}
107 typedef int (*create_t)(void *, void **);
108 typedef int (*destroy_t)(void *);
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 }
127 atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
130 * Only mono and interleaved modes are supported now.
131 * Always allocates a contiguous channel block.
134 static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
136 struct snd_pcm_runtime *runtime;
139 if (NULL == apcm->substream)
142 runtime = apcm->substream->runtime;
145 apcm->vm_block = vm->map(vm, apcm->substream, runtime->dma_bytes);
147 if (NULL == apcm->vm_block)
153 static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
157 if (NULL == apcm->vm_block)
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)
171 unsigned long phys_addr;
174 kvirt_addr = vm->get_ptp_virt(vm, index);
175 if (kvirt_addr == NULL)
178 phys_addr = virt_to_phys(kvirt_addr);
183 static unsigned int convert_format(snd_pcm_format_t snd_format)
185 switch (snd_format) {
186 case SNDRV_PCM_FORMAT_U8:
188 case SNDRV_PCM_FORMAT_S16_LE:
190 case SNDRV_PCM_FORMAT_S24_3LE:
192 case SNDRV_PCM_FORMAT_S32_LE:
194 case SNDRV_PCM_FORMAT_FLOAT_LE:
197 printk(KERN_ERR "ctxfi: not recognized snd format is %d \n",
204 atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
209 /* get pitch and convert to fixed-point 8.24 format. */
210 pitch = (input_rate / output_rate) << 24;
211 input_rate %= output_rate;
214 for (b = 31; ((b >= 0) && !(input_rate >> b)); )
218 input_rate <<= (31 - b);
219 input_rate /= output_rate;
232 static int select_rom(unsigned int pitch)
234 if ((pitch > 0x00428f5c) && (pitch < 0x01b851ec)) {
235 /* 0.26 <= pitch <= 1.72 */
237 } else if ((0x01d66666 == pitch) || (0x01d66667 == pitch)) {
238 /* pitch == 1.8375 */
240 } else if (0x02000000 == pitch) {
243 } else if ((pitch >= 0x0) && (pitch <= 0x08000000)) {
244 /* 0 <= pitch <= 8 */
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};
258 struct amixer *amixer;
260 int n_amixer = apcm->substream->runtime->channels, i = 0;
261 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 mutex_lock(&atc->atc_mutex);
315 amixer->ops->setup(amixer, &src->rsc,
316 INIT_VOL, atc->pcm[i+device*2]);
317 mutex_unlock(&atc->atc_mutex);
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;
399 max_cisz = src->multi * src->rsc.msr;
400 max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
402 src->ops->set_sa(src, apcm->vm_block->addr);
403 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
404 src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
405 src->ops->set_cisz(src, max_cisz);
407 src->ops->set_bm(src, 1);
408 src->ops->set_state(src, SRC_STATE_INIT);
409 src->ops->commit_write(src);
411 ct_timer_start(apcm->timer);
415 static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
420 ct_timer_stop(apcm->timer);
423 src->ops->set_bm(src, 0);
424 src->ops->set_state(src, SRC_STATE_OFF);
425 src->ops->commit_write(src);
427 if (NULL != apcm->srccs) {
428 for (i = 0; i < apcm->n_srcc; i++) {
429 src = apcm->srccs[i];
430 src->ops->set_bm(src, 0);
431 src->ops->set_state(src, SRC_STATE_OFF);
432 src->ops->commit_write(src);
442 atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
444 struct src *src = apcm->src;
450 position = src->ops->get_ca(src);
452 size = apcm->vm_block->size;
453 max_cisz = src->multi * src->rsc.msr;
454 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
456 return (position + size - max_cisz - apcm->vm_block->addr) % size;
459 struct src_node_conf_t {
462 unsigned int mix_msr:8;
463 unsigned int imp_msr:8;
467 static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
468 struct src_node_conf_t *conf, int *n_srcc)
472 /* get pitch and convert to fixed-point 8.24 format. */
473 pitch = atc_get_pitch((atc->rsr * atc->msr),
474 apcm->substream->runtime->rate);
478 *n_srcc = apcm->substream->runtime->channels;
479 conf[0].pitch = pitch;
480 conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
482 } else if (2 == atc->msr) {
483 if (0x8000000 < pitch) {
484 /* Need two-stage SRCs, SRCIMPs and
485 * AMIXERs for converting format */
486 conf[0].pitch = (atc->msr << 24);
487 conf[0].msr = conf[0].mix_msr = 1;
488 conf[0].imp_msr = atc->msr;
490 conf[1].pitch = atc_get_pitch(atc->rsr,
491 apcm->substream->runtime->rate);
492 conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
494 *n_srcc = apcm->substream->runtime->channels * 2;
495 } else if (0x1000000 < pitch) {
496 /* Need one-stage SRCs, SRCIMPs and
497 * AMIXERs for converting format */
498 conf[0].pitch = pitch;
499 conf[0].msr = conf[0].mix_msr
500 = conf[0].imp_msr = atc->msr;
502 *n_srcc = apcm->substream->runtime->channels;
508 atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
510 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
511 struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
512 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
513 struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
514 struct src_desc src_dsc = {0};
516 struct srcimp_desc srcimp_dsc = {0};
517 struct srcimp *srcimp;
518 struct amixer_desc mix_dsc = {0};
519 struct sum_desc sum_dsc = {0};
522 int n_srcimp, n_amixer, n_srcc, n_sum;
523 struct src_node_conf_t src_node_conf[2] = {{0} };
525 /* first release old resources */
526 atc_pcm_release_resources(atc, apcm);
528 /* The numbers of converting SRCs and SRCIMPs should be determined
531 multi = apcm->substream->runtime->channels;
533 /* get pitch and convert to fixed-point 8.24 format. */
534 pitch = atc_get_pitch((atc->rsr * atc->msr),
535 apcm->substream->runtime->rate);
537 setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
538 n_sum = (1 == multi) ? 1 : 0;
539 n_amixer = n_sum * 2 + n_srcc;
541 if ((multi > 1) && (0x8000000 >= pitch)) {
542 /* Need extra AMIXERs and SRCIMPs for special treatment
543 * of interleaved recording of conjugate channels */
544 n_amixer += multi * atc->msr;
545 n_srcimp += multi * atc->msr;
551 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
552 if (NULL == apcm->srccs)
556 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
557 if (NULL == apcm->amixers) {
562 apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
563 if (NULL == apcm->srcimps) {
568 /* Allocate SRCs for sample rate conversion if needed */
570 src_dsc.mode = ARCRW;
571 for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
572 src_dsc.msr = src_node_conf[i/multi].msr;
573 err = src_mgr->get_src(src_mgr, &src_dsc,
574 (struct src **)&apcm->srccs[i]);
578 src = apcm->srccs[i];
579 pitch = src_node_conf[i/multi].pitch;
580 src->ops->set_pitch(src, pitch);
581 src->ops->set_rom(src, select_rom(pitch));
582 src->ops->set_vo(src, src_node_conf[i/multi].vo);
587 /* Allocate AMIXERs for routing SRCs of conversion if needed */
588 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
590 mix_dsc.msr = atc->msr;
591 else if (i < (n_sum*2+n_srcc))
592 mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
596 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
597 (struct amixer **)&apcm->amixers[i]);
604 /* Allocate a SUM resource to mix all input channels together */
605 sum_dsc.msr = atc->msr;
606 err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
610 pitch = atc_get_pitch((atc->rsr * atc->msr),
611 apcm->substream->runtime->rate);
612 /* Allocate SRCIMP resources */
613 for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
615 srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
617 srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
621 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
625 apcm->srcimps[i] = srcimp;
629 /* Allocate a SRC for writing data to host memory */
630 src_dsc.multi = apcm->substream->runtime->channels;
632 src_dsc.mode = MEMWR;
633 err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
638 src->ops->set_pitch(src, pitch);
640 /* Set up device virtual mem map */
641 err = ct_map_audio_buffer(atc, apcm);
648 atc_pcm_release_resources(atc, apcm);
652 static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
655 struct amixer *amixer;
656 struct srcimp *srcimp;
657 struct ct_mixer *mixer = atc->mixer;
659 struct rsc *out_ports[8] = {NULL};
660 int err, i, j, n_sum, multi;
662 int mix_base = 0, imp_base = 0;
664 if (NULL != apcm->src) {
665 /* Prepared pcm capture */
669 /* Get needed resources. */
670 err = atc_pcm_capture_get_resources(atc, apcm);
674 /* Connect resources */
675 mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
676 &out_ports[0], &out_ports[1]);
678 multi = apcm->substream->runtime->channels;
681 for (i = 0; i < 2; i++) {
682 amixer = apcm->amixers[i];
683 amixer->ops->setup(amixer, out_ports[i],
684 MONO_SUM_SCALE, mono);
686 out_ports[0] = &mono->rsc;
688 mix_base = n_sum * 2;
691 for (i = 0; i < apcm->n_srcc; i++) {
692 src = apcm->srccs[i];
693 srcimp = apcm->srcimps[imp_base+i];
694 amixer = apcm->amixers[mix_base+i];
695 srcimp->ops->map(srcimp, src, out_ports[i%multi]);
696 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
697 out_ports[i%multi] = &amixer->rsc;
700 pitch = atc_get_pitch((atc->rsr * atc->msr),
701 apcm->substream->runtime->rate);
703 if ((multi > 1) && (pitch <= 0x8000000)) {
704 /* Special connection for interleaved
705 * recording with conjugate channels */
706 for (i = 0; i < multi; i++) {
707 out_ports[i]->ops->master(out_ports[i]);
708 for (j = 0; j < atc->msr; j++) {
709 amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
710 amixer->ops->set_input(amixer, out_ports[i]);
711 amixer->ops->set_scale(amixer, INIT_VOL);
712 amixer->ops->set_sum(amixer, NULL);
713 amixer->ops->commit_raw_write(amixer);
714 out_ports[i]->ops->next_conj(out_ports[i]);
716 srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
717 srcimp->ops->map(srcimp, apcm->src,
722 for (i = 0; i < multi; i++) {
723 srcimp = apcm->srcimps[apcm->n_srcc+i];
724 srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
728 ct_timer_prepare(apcm->timer);
733 static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
736 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
743 multi = apcm->substream->runtime->channels;
744 /* Set up converting SRCs */
745 for (i = 0; i < apcm->n_srcc; i++) {
746 src = apcm->srccs[i];
747 src->ops->set_pm(src, ((i%multi) != (multi-1)));
748 src_mgr->src_disable(src_mgr, src);
751 /* Set up recording SRC */
753 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
754 src->ops->set_sa(src, apcm->vm_block->addr);
755 src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
756 src->ops->set_ca(src, apcm->vm_block->addr);
757 src_mgr->src_disable(src_mgr, src);
759 /* Disable relevant SRCs firstly */
760 src_mgr->commit_write(src_mgr);
762 /* Enable SRCs respectively */
763 for (i = 0; i < apcm->n_srcc; i++) {
764 src = apcm->srccs[i];
765 src->ops->set_state(src, SRC_STATE_RUN);
766 src->ops->commit_write(src);
767 src_mgr->src_enable_s(src_mgr, src);
770 src->ops->set_bm(src, 1);
771 src->ops->set_state(src, SRC_STATE_RUN);
772 src->ops->commit_write(src);
773 src_mgr->src_enable_s(src_mgr, src);
775 /* Enable relevant SRCs synchronously */
776 src_mgr->commit_write(src_mgr);
778 ct_timer_start(apcm->timer);
783 atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
785 struct src *src = apcm->src;
789 return src->ops->get_ca(src) - apcm->vm_block->addr;
792 static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
793 struct ct_atc_pcm *apcm)
795 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
796 struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
797 struct src_desc desc = {0};
798 struct amixer_desc mix_dsc = {0};
801 int n_amixer = apcm->substream->runtime->channels, i;
802 unsigned int pitch, rsr = atc->pll_rate;
804 /* first release old resources */
805 atc_pcm_release_resources(atc, apcm);
807 /* Get SRC resource */
808 desc.multi = apcm->substream->runtime->channels;
810 while (apcm->substream->runtime->rate > (rsr * desc.msr))
814 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
818 pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
820 src->ops->set_pitch(src, pitch);
821 src->ops->set_rom(src, select_rom(pitch));
822 src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
823 src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
824 src->ops->set_bp(src, 1);
826 /* Get AMIXER resource */
827 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
828 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
829 if (NULL == apcm->amixers) {
833 mix_dsc.msr = desc.msr;
834 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
835 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
836 (struct amixer **)&apcm->amixers[i]);
843 /* Set up device virtual mem map */
844 err = ct_map_audio_buffer(atc, apcm);
851 atc_pcm_release_resources(atc, apcm);
855 static int atc_pll_init(struct ct_atc *atc, int rate)
857 struct hw *hw = atc->hw;
859 err = hw->pll_init(hw, rate);
860 atc->pll_rate = err ? 0 : rate;
865 spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
867 struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
868 unsigned int rate = apcm->substream->runtime->rate;
871 unsigned char iec958_con_fs;
875 iec958_con_fs = IEC958_AES3_CON_FS_48000;
878 iec958_con_fs = IEC958_AES3_CON_FS_44100;
881 iec958_con_fs = IEC958_AES3_CON_FS_32000;
887 mutex_lock(&atc->atc_mutex);
888 dao->ops->get_spos(dao, &status);
889 if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
890 status &= ((~IEC958_AES3_CON_FS) << 24);
891 status |= (iec958_con_fs << 24);
892 dao->ops->set_spos(dao, status);
893 dao->ops->commit_write(dao);
895 if ((rate != atc->pll_rate) && (32000 != rate))
896 err = atc_pll_init(atc, rate);
897 mutex_unlock(&atc->atc_mutex);
903 spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
906 struct amixer *amixer;
911 if (NULL != apcm->src)
914 /* Configure SPDIFOO and PLL to passthrough mode;
915 * determine pll_rate. */
916 err = spdif_passthru_playback_setup(atc, apcm);
920 /* Get needed resources. */
921 err = spdif_passthru_playback_get_resources(atc, apcm);
925 /* Connect resources */
927 for (i = 0; i < apcm->n_amixer; i++) {
928 amixer = apcm->amixers[i];
929 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
930 src = src->ops->next_interleave(src);
934 /* Connect to SPDIFOO */
935 mutex_lock(&atc->atc_mutex);
936 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
937 amixer = apcm->amixers[0];
938 dao->ops->set_left_input(dao, &amixer->rsc);
939 amixer = apcm->amixers[1];
940 dao->ops->set_right_input(dao, &amixer->rsc);
941 mutex_unlock(&atc->atc_mutex);
943 ct_timer_prepare(apcm->timer);
948 static int atc_select_line_in(struct ct_atc *atc)
950 struct hw *hw = atc->hw;
951 struct ct_mixer *mixer = atc->mixer;
954 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
957 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
958 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
960 hw->select_adc_source(hw, ADC_LINEIN);
963 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
965 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
970 static int atc_select_mic_in(struct ct_atc *atc)
972 struct hw *hw = atc->hw;
973 struct ct_mixer *mixer = atc->mixer;
976 if (hw->is_adc_source_selected(hw, ADC_MICIN))
979 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
980 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
982 hw->select_adc_source(hw, ADC_MICIN);
985 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
987 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
992 static int atc_have_digit_io_switch(struct ct_atc *atc)
994 struct hw *hw = atc->hw;
996 return hw->have_digit_io_switch(hw);
999 static int atc_select_digit_io(struct ct_atc *atc)
1001 struct hw *hw = atc->hw;
1003 if (hw->is_adc_source_selected(hw, ADC_NONE))
1006 hw->select_adc_source(hw, ADC_NONE);
1011 static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
1013 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
1016 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
1018 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
1020 daio_mgr->commit_write(daio_mgr);
1026 atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
1028 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1029 return dao->ops->get_spos(dao, status);
1033 atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
1035 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1037 dao->ops->set_spos(dao, status);
1038 dao->ops->commit_write(dao);
1042 static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1044 return atc_daio_unmute(atc, state, LINEO1);
1047 static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1049 return atc_daio_unmute(atc, state, LINEO4);
1052 static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1054 return atc_daio_unmute(atc, state, LINEO3);
1057 static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1059 return atc_daio_unmute(atc, state, LINEO2);
1062 static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1064 return atc_daio_unmute(atc, state, LINEIM);
1067 static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1069 return atc_daio_unmute(atc, state, SPDIFOO);
1072 static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1074 return atc_daio_unmute(atc, state, SPDIFIO);
1077 static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1079 return atc_dao_get_status(atc, status, SPDIFOO);
1082 static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1084 return atc_dao_set_status(atc, status, SPDIFOO);
1087 static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1089 struct dao_desc da_dsc = {0};
1092 struct ct_mixer *mixer = atc->mixer;
1093 struct rsc *rscs[2] = {NULL};
1094 unsigned int spos = 0;
1096 mutex_lock(&atc->atc_mutex);
1097 dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1098 da_dsc.msr = state ? 1 : atc->msr;
1099 da_dsc.passthru = state ? 1 : 0;
1100 err = dao->ops->reinit(dao, &da_dsc);
1102 spos = IEC958_DEFAULT_CON;
1104 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1105 &rscs[0], &rscs[1]);
1106 dao->ops->set_left_input(dao, rscs[0]);
1107 dao->ops->set_right_input(dao, rscs[1]);
1108 /* Restore PLL to atc->rsr if needed. */
1109 if (atc->pll_rate != atc->rsr)
1110 err = atc_pll_init(atc, atc->rsr);
1112 dao->ops->set_spos(dao, spos);
1113 dao->ops->commit_write(dao);
1114 mutex_unlock(&atc->atc_mutex);
1119 static int ct_atc_destroy(struct ct_atc *atc)
1121 struct daio_mgr *daio_mgr;
1125 struct sum_mgr *sum_mgr;
1126 struct src_mgr *src_mgr;
1127 struct srcimp_mgr *srcimp_mgr;
1128 struct srcimp *srcimp;
1129 struct ct_mixer *mixer;
1136 ct_timer_free(atc->timer);
1140 /* Stop hardware and disable all interrupts */
1141 if (NULL != atc->hw)
1142 ((struct hw *)atc->hw)->card_stop(atc->hw);
1144 /* Destroy internal mixer objects */
1145 if (NULL != atc->mixer) {
1147 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1148 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1149 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1150 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1151 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1152 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
1153 ct_mixer_destroy(atc->mixer);
1156 if (NULL != atc->daios) {
1157 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1158 for (i = 0; i < atc->n_daio; i++) {
1159 daio = atc->daios[i];
1160 if (daio->type < LINEIM) {
1161 dao = container_of(daio, struct dao, daio);
1162 dao->ops->clear_left_input(dao);
1163 dao->ops->clear_right_input(dao);
1165 dai = container_of(daio, struct dai, daio);
1166 /* some thing to do for dai ... */
1168 daio_mgr->put_daio(daio_mgr, daio);
1173 if (NULL != atc->pcm) {
1174 sum_mgr = atc->rsc_mgrs[SUM];
1175 for (i = 0; i < atc->n_pcm; i++)
1176 sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1181 if (NULL != atc->srcs) {
1182 src_mgr = atc->rsc_mgrs[SRC];
1183 for (i = 0; i < atc->n_src; i++)
1184 src_mgr->put_src(src_mgr, atc->srcs[i]);
1189 if (NULL != atc->srcimps) {
1190 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1191 for (i = 0; i < atc->n_srcimp; i++) {
1192 srcimp = atc->srcimps[i];
1193 srcimp->ops->unmap(srcimp);
1194 srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1196 kfree(atc->srcimps);
1199 for (i = 0; i < NUM_RSCTYP; i++) {
1200 if ((NULL != rsc_mgr_funcs[i].destroy) &&
1201 (NULL != atc->rsc_mgrs[i]))
1202 rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1206 if (NULL != atc->hw)
1207 destroy_hw_obj((struct hw *)atc->hw);
1209 /* Destroy device virtual memory manager object */
1210 if (NULL != atc->vm) {
1211 ct_vm_destroy(atc->vm);
1220 static int atc_dev_free(struct snd_device *dev)
1222 struct ct_atc *atc = dev->device_data;
1223 return ct_atc_destroy(atc);
1226 static int __devinit atc_identify_card(struct ct_atc *atc)
1228 const struct snd_pci_quirk *p;
1229 const struct snd_pci_quirk *list;
1231 switch (atc->chip_type) {
1233 atc->chip_name = "20K1";
1234 list = subsys_20k1_list;
1237 atc->chip_name = "20K2";
1238 list = subsys_20k2_list;
1243 p = snd_pci_quirk_lookup(atc->pci, list);
1246 printk(KERN_ERR "ctxfi: "
1247 "Device %04x:%04x is black-listed\n",
1248 atc->pci->subsystem_vendor,
1249 atc->pci->subsystem_device);
1252 atc->model = p->value;
1254 if (atc->chip_type == ATC20K1)
1255 atc->model = CT20K1_UNKNOWN;
1257 atc->model = CT20K2_UNKNOWN;
1259 atc->model_name = ct_subsys_name[atc->model];
1260 snd_printd("ctxfi: chip %s model %s (%04x:%04x) is found\n",
1261 atc->chip_name, atc->model_name,
1262 atc->pci->subsystem_vendor,
1263 atc->pci->subsystem_device);
1267 int __devinit ct_atc_create_alsa_devs(struct ct_atc *atc)
1272 alsa_dev_funcs[MIXER].public_name = atc->chip_name;
1274 for (i = 0; i < NUM_CTALSADEVS; i++) {
1275 if (NULL == alsa_dev_funcs[i].create)
1278 err = alsa_dev_funcs[i].create(atc, i,
1279 alsa_dev_funcs[i].public_name);
1281 printk(KERN_ERR "ctxfi: "
1282 "Creating alsa device %d failed!\n", i);
1290 static int __devinit atc_create_hw_devs(struct ct_atc *atc)
1293 struct card_conf info = {0};
1296 err = create_hw_obj(atc->pci, atc->chip_type, atc->model, &hw);
1298 printk(KERN_ERR "Failed to create hw obj!!!\n");
1303 /* Initialize card hardware. */
1304 info.rsr = atc->rsr;
1305 info.msr = atc->msr;
1306 info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1307 err = hw->card_init(hw, &info);
1311 for (i = 0; i < NUM_RSCTYP; i++) {
1312 if (NULL == rsc_mgr_funcs[i].create)
1315 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1317 printk(KERN_ERR "ctxfi: "
1318 "Failed to create rsc_mgr %d!!!\n", i);
1326 static int __devinit atc_get_resources(struct ct_atc *atc)
1328 struct daio_desc da_desc = {0};
1329 struct daio_mgr *daio_mgr;
1330 struct src_desc src_dsc = {0};
1331 struct src_mgr *src_mgr;
1332 struct srcimp_desc srcimp_dsc = {0};
1333 struct srcimp_mgr *srcimp_mgr;
1334 struct sum_desc sum_dsc = {0};
1335 struct sum_mgr *sum_mgr;
1338 atc->daios = kzalloc(sizeof(void *)*(DAIONUM), GFP_KERNEL);
1339 if (NULL == atc->daios)
1342 atc->srcs = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1343 if (NULL == atc->srcs)
1346 atc->srcimps = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1347 if (NULL == atc->srcimps)
1350 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
1351 if (NULL == atc->pcm)
1354 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1355 da_desc.msr = atc->msr;
1356 for (i = 0, atc->n_daio = 0; i < DAIONUM-1; i++) {
1358 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1359 (struct daio **)&atc->daios[i]);
1361 printk(KERN_ERR "ctxfi: Failed to get DAIO "
1362 "resource %d!!!\n", i);
1367 if (atc->model == CTSB073X)
1368 da_desc.type = SPDIFI1;
1370 da_desc.type = SPDIFIO;
1371 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1372 (struct daio **)&atc->daios[i]);
1374 printk(KERN_ERR "ctxfi: Failed to get S/PDIF-in resource!!!\n");
1379 src_mgr = atc->rsc_mgrs[SRC];
1381 src_dsc.msr = atc->msr;
1382 src_dsc.mode = ARCRW;
1383 for (i = 0, atc->n_src = 0; i < (2*2); i++) {
1384 err = src_mgr->get_src(src_mgr, &src_dsc,
1385 (struct src **)&atc->srcs[i]);
1392 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1393 srcimp_dsc.msr = 8; /* SRCIMPs for S/PDIFIn SRT */
1394 for (i = 0, atc->n_srcimp = 0; i < (2*1); i++) {
1395 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1396 (struct srcimp **)&atc->srcimps[i]);
1402 srcimp_dsc.msr = 8; /* SRCIMPs for LINE/MICIn SRT */
1403 for (i = 0; i < (2*1); i++) {
1404 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1405 (struct srcimp **)&atc->srcimps[2*1+i]);
1412 sum_mgr = atc->rsc_mgrs[SUM];
1413 sum_dsc.msr = atc->msr;
1414 for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1415 err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1416 (struct sum **)&atc->pcm[i]);
1423 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1425 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
1432 static void __devinit
1433 atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1434 struct src **srcs, struct srcimp **srcimps)
1436 struct rsc *rscs[2] = {NULL};
1438 struct srcimp *srcimp;
1441 rscs[0] = &dai->daio.rscl;
1442 rscs[1] = &dai->daio.rscr;
1443 for (i = 0; i < 2; i++) {
1445 srcimp = srcimps[i];
1446 srcimp->ops->map(srcimp, src, rscs[i]);
1447 src_mgr->src_disable(src_mgr, src);
1450 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1453 src->ops->set_pm(src, 1);
1454 for (i = 0; i < 2; i++) {
1456 src->ops->set_state(src, SRC_STATE_RUN);
1457 src->ops->commit_write(src);
1458 src_mgr->src_enable_s(src_mgr, src);
1461 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1462 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1464 dai->ops->set_enb_src(dai, 1);
1465 dai->ops->set_enb_srt(dai, 1);
1466 dai->ops->commit_write(dai);
1468 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1471 static void __devinit atc_connect_resources(struct ct_atc *atc)
1477 struct ct_mixer *mixer;
1478 struct rsc *rscs[2] = {NULL};
1483 for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1484 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1485 dao = container_of(atc->daios[j], struct dao, daio);
1486 dao->ops->set_left_input(dao, rscs[0]);
1487 dao->ops->set_right_input(dao, rscs[1]);
1490 dai = container_of(atc->daios[LINEIM], struct dai, daio);
1491 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1492 (struct src **)&atc->srcs[2],
1493 (struct srcimp **)&atc->srcimps[2]);
1495 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1497 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1499 dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1500 atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1501 (struct src **)&atc->srcs[0],
1502 (struct srcimp **)&atc->srcimps[0]);
1505 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1507 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1509 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1511 mixer->set_input_left(mixer, i, &sum->rsc);
1512 sum = atc->pcm[j+1];
1513 mixer->set_input_right(mixer, i, &sum->rsc);
1517 static struct ct_atc atc_preset __devinitdata = {
1518 .map_audio_buffer = ct_map_audio_buffer,
1519 .unmap_audio_buffer = ct_unmap_audio_buffer,
1520 .pcm_playback_prepare = atc_pcm_playback_prepare,
1521 .pcm_release_resources = atc_pcm_release_resources,
1522 .pcm_playback_start = atc_pcm_playback_start,
1523 .pcm_playback_stop = atc_pcm_stop,
1524 .pcm_playback_position = atc_pcm_playback_position,
1525 .pcm_capture_prepare = atc_pcm_capture_prepare,
1526 .pcm_capture_start = atc_pcm_capture_start,
1527 .pcm_capture_stop = atc_pcm_stop,
1528 .pcm_capture_position = atc_pcm_capture_position,
1529 .spdif_passthru_playback_prepare = spdif_passthru_playback_prepare,
1530 .get_ptp_phys = atc_get_ptp_phys,
1531 .select_line_in = atc_select_line_in,
1532 .select_mic_in = atc_select_mic_in,
1533 .select_digit_io = atc_select_digit_io,
1534 .line_front_unmute = atc_line_front_unmute,
1535 .line_surround_unmute = atc_line_surround_unmute,
1536 .line_clfe_unmute = atc_line_clfe_unmute,
1537 .line_rear_unmute = atc_line_rear_unmute,
1538 .line_in_unmute = atc_line_in_unmute,
1539 .spdif_out_unmute = atc_spdif_out_unmute,
1540 .spdif_in_unmute = atc_spdif_in_unmute,
1541 .spdif_out_get_status = atc_spdif_out_get_status,
1542 .spdif_out_set_status = atc_spdif_out_set_status,
1543 .spdif_out_passthru = atc_spdif_out_passthru,
1544 .have_digit_io_switch = atc_have_digit_io_switch,
1548 * ct_atc_create - create and initialize a hardware manager
1549 * @card: corresponding alsa card object
1550 * @pci: corresponding kernel pci device object
1551 * @ratc: return created object address in it
1553 * Creates and initializes a hardware manager.
1555 * Creates kmallocated ct_atc structure. Initializes hardware.
1556 * Returns 0 if suceeds, or negative error code if fails.
1559 int __devinit ct_atc_create(struct snd_card *card, struct pci_dev *pci,
1560 unsigned int rsr, unsigned int msr,
1561 int chip_type, struct ct_atc **ratc)
1564 static struct snd_device_ops ops = {
1565 .dev_free = atc_dev_free,
1571 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
1575 /* Set operations */
1582 atc->chip_type = chip_type;
1584 mutex_init(&atc->atc_mutex);
1586 /* Find card model */
1587 err = atc_identify_card(atc);
1589 printk(KERN_ERR "ctatc: Card not recognised\n");
1593 /* Set up device virtual memory management object */
1594 err = ct_vm_create(&atc->vm);
1598 /* Create all atc hw devices */
1599 err = atc_create_hw_devs(atc);
1604 err = atc_get_resources(atc);
1608 /* Build topology */
1609 atc_connect_resources(atc);
1611 atc->timer = ct_timer_new(atc);
1615 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1619 snd_card_set_dev(card, &pci->dev);
1625 ct_atc_destroy(atc);
1626 printk(KERN_ERR "ctxfi: Something wrong!!!\n");