26 #include <linux/slab.h>
31 #define MONO_SUM_SCALE 0x19a8
32 #define MAX_MULTI_CHN 8
34 #define IEC958_DEFAULT_CON ((IEC958_AES0_NONAUDIO \
35 | IEC958_AES0_CON_NOT_COPYRIGHT) \
36 | ((IEC958_AES1_CON_MIXER \
37 | IEC958_AES1_CON_ORIGINAL) << 8) \
39 | ((IEC958_AES3_CON_FS_48000) << 24))
41 static struct snd_pci_quirk
__devinitdata subsys_20k1_list[] = {
51 static struct snd_pci_quirk
__devinitdata subsys_20k2_list[] = {
90 .public_name =
"Front/WaveIn"},
93 .public_name =
"Surround"},
96 .public_name =
"Center/LFE"},
99 .public_name =
"Side"},
102 .public_name =
"IEC958 Non-audio"},
106 .public_name =
"Mixer"}
155 static void ct_unmap_audio_buffer(
struct ct_atc *atc,
struct ct_atc_pcm *apcm)
169 static unsigned long atc_get_ptp_phys(
struct ct_atc *atc,
int index)
171 return atc->
vm->get_ptp_phys(atc->
vm, index);
176 switch (snd_format) {
195 atc_get_pitch(
unsigned int input_rate,
unsigned int output_rate)
201 pitch = (input_rate / output_rate) << 24;
202 input_rate %= output_rate;
205 for (b = 31; ((b >= 0) && !(input_rate >> b)); )
209 input_rate <<= (31 -
b);
210 input_rate /= output_rate;
223 static int select_rom(
unsigned int pitch)
225 if (pitch > 0x00428f5c && pitch < 0x01b851ec) {
228 }
else if (pitch == 0x01d66666 || pitch == 0x01d66667) {
231 }
else if (pitch == 0x02000000) {
234 }
else if (pitch <= 0x08000000) {
242 static int atc_pcm_playback_prepare(
struct ct_atc *atc,
struct ct_atc_pcm *apcm)
251 int n_amixer = apcm->
substream->runtime->channels,
i = 0;
256 atc_pcm_release_resources(atc, apcm);
262 err = src_mgr->
get_src(src_mgr, &desc, (
struct src **)&apcm->
src);
266 pitch = atc_get_pitch(apcm->
substream->runtime->rate,
269 src->
ops->set_pitch(src, pitch);
270 src->
ops->set_rom(src, select_rom(pitch));
271 src->
ops->set_sf(src, convert_format(apcm->
substream->runtime->format));
272 src->
ops->set_pm(src, (src->
ops->next_interleave(src) !=
NULL));
275 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
282 for (
i = 0, apcm->
n_amixer = 0;
i < n_amixer;
i++) {
283 err = amixer_mgr->
get_amixer(amixer_mgr, &mix_dsc,
284 (
struct amixer **)&apcm->
amixers[
i]);
292 err = ct_map_audio_buffer(atc, apcm);
298 for (
i = 0;
i < n_amixer;
i++) {
301 amixer->
ops->setup(amixer, &src->
rsc,
304 src = src->
ops->next_interleave(src);
314 atc_pcm_release_resources(atc, apcm);
329 for (i = 0; i < apcm->
n_srcimp; i++) {
331 srcimp->
ops->unmap(srcimp);
340 for (i = 0; i < apcm->
n_srcc; i++) {
349 for (i = 0; i < apcm->
n_amixer; i++) {
369 ct_unmap_audio_buffer(atc, apcm);
376 static int atc_pcm_playback_start(
struct ct_atc *atc,
struct ct_atc_pcm *apcm)
378 unsigned int max_cisz;
379 struct src *src = apcm->
src;
385 max_cisz = src->
multi * src->
rsc.msr;
386 max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
390 src->
ops->set_ca(src, apcm->
vm_block->addr + max_cisz);
391 src->
ops->set_cisz(src, max_cisz);
393 src->
ops->set_bm(src, 1);
395 src->
ops->commit_write(src);
409 src->
ops->set_bm(src, 0);
411 src->
ops->commit_write(src);
414 for (i = 0; i < apcm->
n_srcc; i++) {
416 src->
ops->set_bm(src, 0);
418 src->
ops->commit_write(src);
430 struct src *src = apcm->
src;
436 position = src->
ops->get_ca(src);
439 max_cisz = src->
multi * src->
rsc.msr;
440 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
442 return (position + size - max_cisz - apcm->
vm_block->addr) %
size;
453 static void setup_src_node_conf(
struct ct_atc *atc,
struct ct_atc_pcm *apcm,
459 pitch = atc_get_pitch((atc->
rsr * atc->
msr),
464 *n_srcc = apcm->
substream->runtime->channels;
465 conf[0].
pitch = pitch;
468 }
else if (2 <= atc->
msr) {
469 if (0x8000000 < pitch) {
476 conf[1].
pitch = atc_get_pitch(atc->
rsr,
480 *n_srcc = apcm->
substream->runtime->channels * 2;
481 }
else if (0x1000000 < pitch) {
484 conf[0].
pitch = pitch;
488 *n_srcc = apcm->
substream->runtime->channels;
503 struct srcimp *srcimp;
508 int n_srcimp, n_amixer, n_srcc, n_sum;
512 atc_pcm_release_resources(atc, apcm);
517 multi = apcm->
substream->runtime->channels;
520 pitch = atc_get_pitch((atc->
rsr * atc->
msr),
523 setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
524 n_sum = (1 ==
multi) ? 1 : 0;
525 n_amixer = n_sum * 2 + n_srcc;
527 if ((multi > 1) && (0x8000000 >=
pitch)) {
530 n_amixer += multi * atc->
msr;
531 n_srcimp += multi * atc->
msr;
557 for (i = 0, apcm->
n_srcc = 0; i < n_srcc; i++) {
559 err = src_mgr->
get_src(src_mgr, &src_dsc,
560 (
struct src **)&apcm->
srccs[i]);
566 src->
ops->set_pitch(src, pitch);
567 src->
ops->set_rom(src, select_rom(pitch));
568 src->
ops->set_vo(src, src_node_conf[i/multi].
vo);
574 for (i = 0, apcm->
n_amixer = 0; i < n_amixer; i++) {
577 else if (i < (n_sum*2+n_srcc))
578 mix_dsc.
msr = src_node_conf[(i-n_sum*2)/multi].
mix_msr;
582 err = amixer_mgr->
get_amixer(amixer_mgr, &mix_dsc,
583 (
struct amixer **)&apcm->
amixers[i]);
592 err = sum_mgr->
get_sum(sum_mgr, &sum_dsc, (
struct sum **)&apcm->
mono);
596 pitch = atc_get_pitch((atc->
rsr * atc->
msr),
599 for (i = 0, apcm->
n_srcimp = 0; i < n_srcimp; i++) {
603 srcimp_dsc.
msr = (pitch <= 0x8000000) ? atc->
msr : 1;
607 err = srcimp_mgr->
get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
619 err = src_mgr->
get_src(src_mgr, &src_dsc, (
struct src **)&apcm->
src);
624 src->
ops->set_pitch(src, pitch);
627 err = ct_map_audio_buffer(atc, apcm);
634 atc_pcm_release_resources(atc, apcm);
638 static int atc_pcm_capture_prepare(
struct ct_atc *atc,
struct ct_atc_pcm *apcm)
641 struct amixer *amixer;
642 struct srcimp *srcimp;
645 struct rsc *out_ports[8] = {
NULL};
648 int mix_base = 0, imp_base = 0;
650 atc_pcm_release_resources(atc, apcm);
653 err = atc_pcm_capture_get_resources(atc, apcm);
659 &out_ports[0], &out_ports[1]);
661 multi = apcm->
substream->runtime->channels;
664 for (i = 0; i < 2; i++) {
666 amixer->
ops->setup(amixer, out_ports[i],
669 out_ports[0] = &mono->
rsc;
671 mix_base = n_sum * 2;
674 for (i = 0; i < apcm->
n_srcc; i++) {
678 srcimp->
ops->map(srcimp, src, out_ports[i%multi]);
683 pitch = atc_get_pitch((atc->
rsr * atc->
msr),
686 if ((multi > 1) && (pitch <= 0x8000000)) {
689 for (i = 0; i <
multi; i++) {
690 out_ports[
i]->
ops->master(out_ports[i]);
691 for (j = 0; j < atc->
msr; j++) {
693 amixer->
ops->set_input(amixer, out_ports[i]);
695 amixer->
ops->set_sum(amixer,
NULL);
696 amixer->
ops->commit_raw_write(amixer);
697 out_ports[
i]->
ops->next_conj(out_ports[i]);
700 srcimp->
ops->map(srcimp, apcm->
src,
705 for (i = 0; i <
multi; i++) {
707 srcimp->
ops->map(srcimp, apcm->
src, out_ports[i]);
716 static int atc_pcm_capture_start(
struct ct_atc *atc,
struct ct_atc_pcm *apcm)
726 multi = apcm->
substream->runtime->channels;
728 for (i = 0; i < apcm->
n_srcc; i++) {
730 src->
ops->set_pm(src, ((i%multi) != (multi-1)));
736 src->
ops->set_sf(src, convert_format(apcm->
substream->runtime->format));
746 for (i = 0; i < apcm->
n_srcc; i++) {
749 src->
ops->commit_write(src);
753 src->
ops->set_bm(src, 1);
755 src->
ops->commit_write(src);
768 struct src *src = apcm->
src;
772 return src->
ops->get_ca(src) - apcm->
vm_block->addr;
775 static int spdif_passthru_playback_get_resources(
struct ct_atc *atc,
784 int n_amixer = apcm->
substream->runtime->channels,
i;
785 unsigned int pitch, rsr = atc->
pll_rate;
788 atc_pcm_release_resources(atc, apcm);
793 while (apcm->
substream->runtime->rate > (rsr * desc.
msr))
797 err = src_mgr->
get_src(src_mgr, &desc, (
struct src **)&apcm->
src);
801 pitch = atc_get_pitch(apcm->
substream->runtime->rate, (rsr * desc.
msr));
803 src->
ops->set_pitch(src, pitch);
804 src->
ops->set_rom(src, select_rom(pitch));
805 src->
ops->set_sf(src, convert_format(apcm->
substream->runtime->format));
806 src->
ops->set_pm(src, (src->
ops->next_interleave(src) !=
NULL));
807 src->
ops->set_bp(src, 1);
810 n_amixer = (n_amixer < 2) ? 2 : n_amixer;
817 for (i = 0, apcm->
n_amixer = 0; i < n_amixer; i++) {
818 err = amixer_mgr->
get_amixer(amixer_mgr, &mix_dsc,
819 (
struct amixer **)&apcm->
amixers[i]);
827 err = ct_map_audio_buffer(atc, apcm);
834 atc_pcm_release_resources(atc, apcm);
838 static int atc_pll_init(
struct ct_atc *atc,
int rate)
851 unsigned int rate = apcm->
substream->runtime->rate;
854 unsigned char iec958_con_fs;
871 dao->
ops->get_spos(dao, &status);
874 status |= (iec958_con_fs << 24);
875 dao->
ops->set_spos(dao, status);
876 dao->
ops->commit_write(dao);
878 if ((rate != atc->
pll_rate) && (32000 != rate))
879 err = atc_pll_init(atc, rate);
886 spdif_passthru_playback_prepare(
struct ct_atc *atc,
struct ct_atc_pcm *apcm)
889 struct amixer *amixer;
894 atc_pcm_release_resources(atc, apcm);
898 err = spdif_passthru_playback_setup(atc, apcm);
903 err = spdif_passthru_playback_get_resources(atc, apcm);
909 for (i = 0; i < apcm->
n_amixer; i++) {
912 src = src->
ops->next_interleave(src);
920 dao->
ops->set_left_input(dao, &amixer->
rsc);
922 dao->
ops->set_right_input(dao, &amixer->
rsc);
930 static int atc_select_line_in(
struct ct_atc *atc)
932 struct hw *hw = atc->
hw;
952 static int atc_select_mic_in(
struct ct_atc *atc)
954 struct hw *hw = atc->
hw;
976 struct hw *hw = atc->
hw;
981 static int atc_output_switch_get(
struct ct_atc *atc)
983 struct hw *hw = atc->
hw;
988 static int atc_output_switch_put(
struct ct_atc *atc,
int position)
990 struct hw *hw = atc->
hw;
995 static int atc_mic_source_switch_get(
struct ct_atc *atc)
997 struct hw *hw = atc->
hw;
1002 static int atc_mic_source_switch_put(
struct ct_atc *atc,
int position)
1004 struct hw *hw = atc->
hw;
1009 static int atc_select_digit_io(
struct ct_atc *atc)
1011 struct hw *hw = atc->
hw;
1021 static int atc_daio_unmute(
struct ct_atc *atc,
unsigned char state,
int type)
1036 atc_dao_get_status(
struct ct_atc *atc,
unsigned int *status,
int type)
1039 return dao->
ops->get_spos(dao, status);
1043 atc_dao_set_status(
struct ct_atc *atc,
unsigned int status,
int type)
1047 dao->
ops->set_spos(dao, status);
1048 dao->
ops->commit_write(dao);
1052 static int atc_line_front_unmute(
struct ct_atc *atc,
unsigned char state)
1054 return atc_daio_unmute(atc, state,
LINEO1);
1057 static int atc_line_surround_unmute(
struct ct_atc *atc,
unsigned char state)
1059 return atc_daio_unmute(atc, state,
LINEO2);
1062 static int atc_line_clfe_unmute(
struct ct_atc *atc,
unsigned char state)
1064 return atc_daio_unmute(atc, state,
LINEO3);
1067 static int atc_line_rear_unmute(
struct ct_atc *atc,
unsigned char state)
1069 return atc_daio_unmute(atc, state,
LINEO4);
1072 static int atc_line_in_unmute(
struct ct_atc *atc,
unsigned char state)
1074 return atc_daio_unmute(atc, state,
LINEIM);
1077 static int atc_mic_unmute(
struct ct_atc *atc,
unsigned char state)
1079 return atc_daio_unmute(atc, state,
MIC);
1082 static int atc_spdif_out_unmute(
struct ct_atc *atc,
unsigned char state)
1084 return atc_daio_unmute(atc, state,
SPDIFOO);
1087 static int atc_spdif_in_unmute(
struct ct_atc *atc,
unsigned char state)
1089 return atc_daio_unmute(atc, state,
SPDIFIO);
1092 static int atc_spdif_out_get_status(
struct ct_atc *atc,
unsigned int *status)
1094 return atc_dao_get_status(atc, status,
SPDIFOO);
1097 static int atc_spdif_out_set_status(
struct ct_atc *atc,
unsigned int status)
1099 return atc_dao_set_status(atc, status,
SPDIFOO);
1102 static int atc_spdif_out_passthru(
struct ct_atc *atc,
unsigned char state)
1109 unsigned int spos = 0;
1113 da_dsc.
msr = state ? 1 : atc->
msr;
1115 err = dao->
ops->reinit(dao, &da_dsc);
1120 &rscs[0], &rscs[1]);
1121 dao->
ops->set_left_input(dao, rscs[0]);
1122 dao->
ops->set_right_input(dao, rscs[1]);
1125 err = atc_pll_init(atc, atc->
rsr);
1127 dao->
ops->set_spos(dao, spos);
1128 dao->
ops->commit_write(dao);
1134 static int atc_release_resources(
struct ct_atc *atc)
1137 struct daio_mgr *daio_mgr =
NULL;
1138 struct dao *dao =
NULL;
1141 struct sum_mgr *sum_mgr =
NULL;
1142 struct src_mgr *src_mgr =
NULL;
1143 struct srcimp_mgr *srcimp_mgr =
NULL;
1144 struct srcimp *srcimp =
NULL;
1164 dao->
ops->clear_left_input(dao);
1165 dao->
ops->clear_right_input(dao);
1170 daio_mgr->
put_daio(daio_mgr, daio);
1178 for (i = 0; i < atc->
n_pcm; i++)
1187 for (i = 0; i < atc->
n_src; i++)
1196 for (i = 0; i < atc->
n_srcimp; i++) {
1198 srcimp->
ops->unmap(srcimp);
1208 static int ct_atc_destroy(
struct ct_atc *atc)
1220 atc_release_resources(atc);
1228 rsc_mgr_funcs[
i].destroy(atc->
rsc_mgrs[i]);
1249 return ct_atc_destroy(atc);
1254 const struct snd_pci_quirk *
p;
1255 const struct snd_pci_quirk *
list;
1261 list = subsys_20k1_list;
1265 list = subsys_20k2_list;
1271 vendor_id = ssid >> 16;
1272 device_id = ssid & 0xffff;
1274 vendor_id = atc->
pci->subsystem_vendor;
1275 device_id = atc->
pci->subsystem_device;
1277 p = snd_pci_quirk_lookup_id(vendor_id, device_id, list);
1281 "Device %04x:%04x is black-listed\n",
1282 vendor_id, device_id);
1285 atc->
model = p->value;
1293 snd_printd(
"ctxfi: chip %s model %s (%04x:%04x) is found\n",
1295 vendor_id, device_id);
1307 if (!alsa_dev_funcs[i].
create)
1310 err = alsa_dev_funcs[
i].create(atc, i,
1314 "Creating alsa device %d failed!\n", i);
1344 if (!rsc_mgr_funcs[i].
create)
1347 err = rsc_mgr_funcs[
i].create(atc->
hw, &atc->
rsc_mgrs[i]);
1350 "Failed to create rsc_mgr %d!!!\n", i);
1358 static int atc_get_resources(
struct ct_atc *atc)
1361 struct daio_mgr *daio_mgr;
1363 struct src_mgr *src_mgr;
1365 struct srcimp_mgr *srcimp_mgr;
1367 struct sum_mgr *sum_mgr;
1368 int err,
i, num_srcs, num_daios;
1391 for (i = 0, atc->
n_daio = 0; i < num_daios; i++) {
1394 err = daio_mgr->
get_daio(daio_mgr, &da_desc,
1395 (
struct daio **)&atc->
daios[i]);
1398 "resource %d!!!\n", i);
1408 for (i = 0, atc->
n_src = 0; i < num_srcs; i++) {
1409 err = src_mgr->
get_src(src_mgr, &src_dsc,
1410 (
struct src **)&atc->
srcs[i]);
1419 for (i = 0, atc->
n_srcimp = 0; i < num_srcs; i++) {
1420 err = srcimp_mgr->
get_srcimp(srcimp_mgr, &srcimp_dsc,
1421 (
struct srcimp **)&atc->
srcimps[i]);
1430 for (i = 0, atc->
n_pcm = 0; i < (2*4); i++) {
1431 err = sum_mgr->
get_sum(sum_mgr, &sum_dsc,
1432 (
struct sum **)&atc->
pcm[i]);
1443 atc_connect_dai(
struct src_mgr *src_mgr,
struct dai *dai,
1444 struct src **srcs,
struct srcimp **srcimps)
1448 struct srcimp *srcimp;
1451 rscs[0] = &dai->
daio.rscl;
1452 rscs[1] = &dai->
daio.rscr;
1453 for (i = 0; i < 2; i++) {
1455 srcimp = srcimps[
i];
1456 srcimp->
ops->map(srcimp, src, rscs[i]);
1463 src->
ops->set_pm(src, 1);
1464 for (i = 0; i < 2; i++) {
1467 src->
ops->commit_write(src);
1471 dai->
ops->set_srt_srcl(dai, &(srcs[0]->
rsc));
1472 dai->
ops->set_srt_srcr(dai, &(srcs[1]->
rsc));
1474 dai->
ops->set_enb_src(dai, 1);
1475 dai->
ops->set_enb_srt(dai, 1);
1476 dai->
ops->commit_write(dai);
1481 static void atc_connect_resources(
struct ct_atc *atc)
1496 dao->
ops->set_left_input(dao, rscs[0]);
1497 dao->
ops->set_right_input(dao, rscs[1]);
1502 (
struct src **)&atc->
srcs[2],
1503 (
struct srcimp **)&atc->
srcimps[2]);
1513 (
struct src **)&atc->
srcs[4],
1514 (
struct srcimp **)&atc->
srcimps[4]);
1523 (
struct src **)&atc->
srcs[0],
1524 (
struct srcimp **)&atc->
srcimps[0]);
1534 sum = atc->
pcm[j+1];
1539 #ifdef CONFIG_PM_SLEEP
1540 static int atc_suspend(
struct ct_atc *atc)
1543 struct hw *hw = atc->
hw;
1547 for (i =
FRONT; i < NUM_PCMS; i++) {
1551 snd_pcm_suspend_all(atc->pcms[i]);
1554 atc_release_resources(atc);
1561 static int atc_hw_resume(
struct ct_atc *atc)
1563 struct hw *hw = atc->
hw;
1570 return hw->resume(hw, &info);
1573 static int atc_resources_resume(
struct ct_atc *atc)
1579 err = atc_get_resources(atc);
1581 atc_release_resources(atc);
1586 atc_connect_resources(atc);
1589 mixer->resume(mixer);
1594 static int atc_resume(
struct ct_atc *atc)
1599 err = atc_hw_resume(atc);
1602 "disabling device\n");
1607 err = atc_resources_resume(atc);
1618 .map_audio_buffer = ct_map_audio_buffer,
1619 .unmap_audio_buffer = ct_unmap_audio_buffer,
1620 .pcm_playback_prepare = atc_pcm_playback_prepare,
1621 .pcm_release_resources = atc_pcm_release_resources,
1622 .pcm_playback_start = atc_pcm_playback_start,
1623 .pcm_playback_stop = atc_pcm_stop,
1624 .pcm_playback_position = atc_pcm_playback_position,
1625 .pcm_capture_prepare = atc_pcm_capture_prepare,
1626 .pcm_capture_start = atc_pcm_capture_start,
1627 .pcm_capture_stop = atc_pcm_stop,
1628 .pcm_capture_position = atc_pcm_capture_position,
1630 .get_ptp_phys = atc_get_ptp_phys,
1631 .select_line_in = atc_select_line_in,
1632 .select_mic_in = atc_select_mic_in,
1633 .select_digit_io = atc_select_digit_io,
1634 .line_front_unmute = atc_line_front_unmute,
1635 .line_surround_unmute = atc_line_surround_unmute,
1636 .line_clfe_unmute = atc_line_clfe_unmute,
1637 .line_rear_unmute = atc_line_rear_unmute,
1638 .line_in_unmute = atc_line_in_unmute,
1639 .mic_unmute = atc_mic_unmute,
1640 .spdif_out_unmute = atc_spdif_out_unmute,
1641 .spdif_in_unmute = atc_spdif_in_unmute,
1642 .spdif_out_get_status = atc_spdif_out_get_status,
1643 .spdif_out_set_status = atc_spdif_out_set_status,
1644 .spdif_out_passthru = atc_spdif_out_passthru,
1645 .capabilities = atc_capabilities,
1646 .output_switch_get = atc_output_switch_get,
1647 .output_switch_put = atc_output_switch_put,
1648 .mic_source_switch_get = atc_mic_source_switch_get,
1649 .mic_source_switch_put = atc_mic_source_switch_put,
1650 #ifdef CONFIG_PM_SLEEP
1651 .suspend = atc_suspend,
1652 .resume = atc_resume,
1669 unsigned int rsr,
unsigned int msr,
1697 err = atc_identify_card(atc, ssid);
1709 err = atc_create_hw_devs(atc);
1720 err = atc_get_resources(atc);
1725 atc_connect_resources(atc);
1743 ct_atc_destroy(atc);