ctmixer.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /**
  2. * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
  3. *
  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.
  7. *
  8. * @File ctmixer.c
  9. *
  10. * @Brief
  11. * This file contains the implementation of alsa mixer device functions.
  12. *
  13. * @Author Liu Chun
  14. * @Date May 28 2008
  15. *
  16. */
  17. #include "ctmixer.h"
  18. #include "ctamixer.h"
  19. #include <sound/core.h>
  20. #include <sound/control.h>
  21. #include <sound/asoundef.h>
  22. #include <sound/pcm.h>
  23. #include <linux/slab.h>
  24. enum CT_SUM_CTL {
  25. SUM_IN_F,
  26. SUM_IN_R,
  27. SUM_IN_C,
  28. SUM_IN_S,
  29. SUM_IN_F_C,
  30. NUM_CT_SUMS
  31. };
  32. enum CT_AMIXER_CTL {
  33. /* volume control mixers */
  34. AMIXER_MASTER_F,
  35. AMIXER_MASTER_R,
  36. AMIXER_MASTER_C,
  37. AMIXER_MASTER_S,
  38. AMIXER_PCM_F,
  39. AMIXER_PCM_R,
  40. AMIXER_PCM_C,
  41. AMIXER_PCM_S,
  42. AMIXER_SPDIFI,
  43. AMIXER_LINEIN,
  44. AMIXER_MIC,
  45. AMIXER_SPDIFO,
  46. AMIXER_WAVE_F,
  47. AMIXER_WAVE_R,
  48. AMIXER_WAVE_C,
  49. AMIXER_WAVE_S,
  50. AMIXER_MASTER_F_C,
  51. AMIXER_PCM_F_C,
  52. AMIXER_SPDIFI_C,
  53. AMIXER_LINEIN_C,
  54. AMIXER_MIC_C,
  55. /* this should always be the last one */
  56. NUM_CT_AMIXERS
  57. };
  58. enum CTALSA_MIXER_CTL {
  59. /* volume control mixers */
  60. MIXER_MASTER_P,
  61. MIXER_PCM_P,
  62. MIXER_LINEIN_P,
  63. MIXER_MIC_P,
  64. MIXER_SPDIFI_P,
  65. MIXER_SPDIFO_P,
  66. MIXER_WAVEF_P,
  67. MIXER_WAVER_P,
  68. MIXER_WAVEC_P,
  69. MIXER_WAVES_P,
  70. MIXER_MASTER_C,
  71. MIXER_PCM_C,
  72. MIXER_LINEIN_C,
  73. MIXER_MIC_C,
  74. MIXER_SPDIFI_C,
  75. /* switch control mixers */
  76. MIXER_PCM_C_S,
  77. MIXER_LINEIN_C_S,
  78. MIXER_MIC_C_S,
  79. MIXER_SPDIFI_C_S,
  80. MIXER_LINEIN_P_S,
  81. MIXER_SPDIFO_P_S,
  82. MIXER_SPDIFI_P_S,
  83. MIXER_WAVEF_P_S,
  84. MIXER_WAVER_P_S,
  85. MIXER_WAVEC_P_S,
  86. MIXER_WAVES_P_S,
  87. MIXER_DIGITAL_IO_S,
  88. MIXER_IEC958_MASK,
  89. MIXER_IEC958_DEFAULT,
  90. MIXER_IEC958_STREAM,
  91. /* this should always be the last one */
  92. NUM_CTALSA_MIXERS
  93. };
  94. #define VOL_MIXER_START MIXER_MASTER_P
  95. #define VOL_MIXER_END MIXER_SPDIFI_C
  96. #define VOL_MIXER_NUM (VOL_MIXER_END - VOL_MIXER_START + 1)
  97. #define SWH_MIXER_START MIXER_PCM_C_S
  98. #define SWH_MIXER_END MIXER_DIGITAL_IO_S
  99. #define SWH_CAPTURE_START MIXER_PCM_C_S
  100. #define SWH_CAPTURE_END MIXER_SPDIFI_C_S
  101. #define CHN_NUM 2
  102. struct ct_kcontrol_init {
  103. unsigned char ctl;
  104. char *name;
  105. };
  106. static struct ct_kcontrol_init
  107. ct_kcontrol_init_table[NUM_CTALSA_MIXERS] = {
  108. [MIXER_MASTER_P] = {
  109. .ctl = 1,
  110. .name = "Master Playback Volume",
  111. },
  112. [MIXER_MASTER_C] = {
  113. .ctl = 1,
  114. .name = "Master Capture Volume",
  115. },
  116. [MIXER_PCM_P] = {
  117. .ctl = 1,
  118. .name = "PCM Playback Volume",
  119. },
  120. [MIXER_PCM_C] = {
  121. .ctl = 1,
  122. .name = "PCM Capture Volume",
  123. },
  124. [MIXER_LINEIN_P] = {
  125. .ctl = 1,
  126. .name = "Line-in Playback Volume",
  127. },
  128. [MIXER_LINEIN_C] = {
  129. .ctl = 1,
  130. .name = "Line-in Capture Volume",
  131. },
  132. [MIXER_MIC_P] = {
  133. .ctl = 1,
  134. .name = "Mic Playback Volume",
  135. },
  136. [MIXER_MIC_C] = {
  137. .ctl = 1,
  138. .name = "Mic Capture Volume",
  139. },
  140. [MIXER_SPDIFI_P] = {
  141. .ctl = 1,
  142. .name = "S/PDIF-in Playback Volume",
  143. },
  144. [MIXER_SPDIFI_C] = {
  145. .ctl = 1,
  146. .name = "S/PDIF-in Capture Volume",
  147. },
  148. [MIXER_SPDIFO_P] = {
  149. .ctl = 1,
  150. .name = "S/PDIF-out Playback Volume",
  151. },
  152. [MIXER_WAVEF_P] = {
  153. .ctl = 1,
  154. .name = "Front Playback Volume",
  155. },
  156. [MIXER_WAVES_P] = {
  157. .ctl = 1,
  158. .name = "Surround Playback Volume",
  159. },
  160. [MIXER_WAVEC_P] = {
  161. .ctl = 1,
  162. .name = "Center/LFE Playback Volume",
  163. },
  164. [MIXER_WAVER_P] = {
  165. .ctl = 1,
  166. .name = "Rear Playback Volume",
  167. },
  168. [MIXER_PCM_C_S] = {
  169. .ctl = 1,
  170. .name = "PCM Capture Switch",
  171. },
  172. [MIXER_LINEIN_C_S] = {
  173. .ctl = 1,
  174. .name = "Line-in Capture Switch",
  175. },
  176. [MIXER_MIC_C_S] = {
  177. .ctl = 1,
  178. .name = "Mic Capture Switch",
  179. },
  180. [MIXER_SPDIFI_C_S] = {
  181. .ctl = 1,
  182. .name = "S/PDIF-in Capture Switch",
  183. },
  184. [MIXER_LINEIN_P_S] = {
  185. .ctl = 1,
  186. .name = "Line-in Playback Switch",
  187. },
  188. [MIXER_SPDIFO_P_S] = {
  189. .ctl = 1,
  190. .name = "S/PDIF-out Playback Switch",
  191. },
  192. [MIXER_SPDIFI_P_S] = {
  193. .ctl = 1,
  194. .name = "S/PDIF-in Playback Switch",
  195. },
  196. [MIXER_WAVEF_P_S] = {
  197. .ctl = 1,
  198. .name = "Front Playback Switch",
  199. },
  200. [MIXER_WAVES_P_S] = {
  201. .ctl = 1,
  202. .name = "Surround Playback Switch",
  203. },
  204. [MIXER_WAVEC_P_S] = {
  205. .ctl = 1,
  206. .name = "Center/LFE Playback Switch",
  207. },
  208. [MIXER_WAVER_P_S] = {
  209. .ctl = 1,
  210. .name = "Rear Playback Switch",
  211. },
  212. [MIXER_DIGITAL_IO_S] = {
  213. .ctl = 0,
  214. .name = "Digit-IO Playback Switch",
  215. },
  216. };
  217. static void
  218. ct_mixer_recording_select(struct ct_mixer *mixer, enum CT_AMIXER_CTL type);
  219. static void
  220. ct_mixer_recording_unselect(struct ct_mixer *mixer, enum CT_AMIXER_CTL type);
  221. static struct snd_kcontrol *kctls[2] = {NULL};
  222. static enum CT_AMIXER_CTL get_amixer_index(enum CTALSA_MIXER_CTL alsa_index)
  223. {
  224. switch (alsa_index) {
  225. case MIXER_MASTER_P: return AMIXER_MASTER_F;
  226. case MIXER_MASTER_C: return AMIXER_MASTER_F_C;
  227. case MIXER_PCM_P: return AMIXER_PCM_F;
  228. case MIXER_PCM_C:
  229. case MIXER_PCM_C_S: return AMIXER_PCM_F_C;
  230. case MIXER_LINEIN_P: return AMIXER_LINEIN;
  231. case MIXER_LINEIN_C:
  232. case MIXER_LINEIN_C_S: return AMIXER_LINEIN_C;
  233. case MIXER_MIC_P: return AMIXER_MIC;
  234. case MIXER_MIC_C:
  235. case MIXER_MIC_C_S: return AMIXER_MIC_C;
  236. case MIXER_SPDIFI_P: return AMIXER_SPDIFI;
  237. case MIXER_SPDIFI_C:
  238. case MIXER_SPDIFI_C_S: return AMIXER_SPDIFI_C;
  239. case MIXER_SPDIFO_P: return AMIXER_SPDIFO;
  240. case MIXER_WAVEF_P: return AMIXER_WAVE_F;
  241. case MIXER_WAVES_P: return AMIXER_WAVE_S;
  242. case MIXER_WAVEC_P: return AMIXER_WAVE_C;
  243. case MIXER_WAVER_P: return AMIXER_WAVE_R;
  244. default: return NUM_CT_AMIXERS;
  245. }
  246. }
  247. static enum CT_AMIXER_CTL get_recording_amixer(enum CT_AMIXER_CTL index)
  248. {
  249. switch (index) {
  250. case AMIXER_MASTER_F: return AMIXER_MASTER_F_C;
  251. case AMIXER_PCM_F: return AMIXER_PCM_F_C;
  252. case AMIXER_SPDIFI: return AMIXER_SPDIFI_C;
  253. case AMIXER_LINEIN: return AMIXER_LINEIN_C;
  254. case AMIXER_MIC: return AMIXER_MIC_C;
  255. default: return NUM_CT_AMIXERS;
  256. }
  257. }
  258. static unsigned char
  259. get_switch_state(struct ct_mixer *mixer, enum CTALSA_MIXER_CTL type)
  260. {
  261. return (mixer->switch_state & (0x1 << (type - SWH_MIXER_START)))
  262. ? 1 : 0;
  263. }
  264. static void
  265. set_switch_state(struct ct_mixer *mixer,
  266. enum CTALSA_MIXER_CTL type, unsigned char state)
  267. {
  268. if (state)
  269. mixer->switch_state |= (0x1 << (type - SWH_MIXER_START));
  270. else
  271. mixer->switch_state &= ~(0x1 << (type - SWH_MIXER_START));
  272. }
  273. /* Map integer value ranging from 0 to 65535 to 14-bit float value ranging
  274. * from 2^-6 to (1+1023/1024) */
  275. static unsigned int uint16_to_float14(unsigned int x)
  276. {
  277. unsigned int i = 0;
  278. if (x < 17)
  279. return 0;
  280. x *= 2031;
  281. x /= 65535;
  282. x += 16;
  283. /* i <= 6 */
  284. for (i = 0; !(x & 0x400); i++)
  285. x <<= 1;
  286. x = (((7 - i) & 0x7) << 10) | (x & 0x3ff);
  287. return x;
  288. }
  289. static unsigned int float14_to_uint16(unsigned int x)
  290. {
  291. unsigned int e = 0;
  292. if (!x)
  293. return x;
  294. e = (x >> 10) & 0x7;
  295. x &= 0x3ff;
  296. x += 1024;
  297. x >>= (7 - e);
  298. x -= 16;
  299. x *= 65535;
  300. x /= 2031;
  301. return x;
  302. }
  303. static int ct_alsa_mix_volume_info(struct snd_kcontrol *kcontrol,
  304. struct snd_ctl_elem_info *uinfo)
  305. {
  306. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  307. uinfo->count = 2;
  308. uinfo->value.integer.min = 0;
  309. uinfo->value.integer.max = 43690;
  310. uinfo->value.integer.step = 128;
  311. return 0;
  312. }
  313. static int ct_alsa_mix_volume_get(struct snd_kcontrol *kcontrol,
  314. struct snd_ctl_elem_value *ucontrol)
  315. {
  316. struct ct_atc *atc = snd_kcontrol_chip(kcontrol);
  317. enum CT_AMIXER_CTL type = get_amixer_index(kcontrol->private_value);
  318. struct amixer *amixer = NULL;
  319. int i = 0;
  320. for (i = 0; i < 2; i++) {
  321. amixer = ((struct ct_mixer *)atc->mixer)->
  322. amixers[type*CHN_NUM+i];
  323. /* Convert 14-bit float-point scale to 16-bit integer volume */
  324. ucontrol->value.integer.value[i] =
  325. (float14_to_uint16(amixer->ops->get_scale(amixer)) & 0xffff);
  326. }
  327. return 0;
  328. }
  329. static int ct_alsa_mix_volume_put(struct snd_kcontrol *kcontrol,
  330. struct snd_ctl_elem_value *ucontrol)
  331. {
  332. struct ct_atc *atc = snd_kcontrol_chip(kcontrol);
  333. struct ct_mixer *mixer = atc->mixer;
  334. enum CT_AMIXER_CTL type = get_amixer_index(kcontrol->private_value);
  335. struct amixer *amixer = NULL;
  336. int i = 0, j = 0, change = 0, val = 0;
  337. for (i = 0; i < 2; i++) {
  338. /* Convert 16-bit integer volume to 14-bit float-point scale */
  339. val = (ucontrol->value.integer.value[i] & 0xffff);
  340. amixer = mixer->amixers[type*CHN_NUM+i];
  341. if ((float14_to_uint16(amixer->ops->get_scale(amixer)) & 0xff80)
  342. != (val & 0xff80)) {
  343. val = uint16_to_float14(val);
  344. amixer->ops->set_scale(amixer, val);
  345. amixer->ops->commit_write(amixer);
  346. change = 1;
  347. /* Synchronize Master/PCM playback AMIXERs. */
  348. if (AMIXER_MASTER_F == type || AMIXER_PCM_F == type) {
  349. for (j = 1; j < 4; j++) {
  350. amixer = mixer->
  351. amixers[(type+j)*CHN_NUM+i];
  352. amixer->ops->set_scale(amixer, val);
  353. amixer->ops->commit_write(amixer);
  354. }
  355. }
  356. }
  357. }
  358. return change;
  359. }
  360. static struct snd_kcontrol_new vol_ctl = {
  361. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  362. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  363. .info = ct_alsa_mix_volume_info,
  364. .get = ct_alsa_mix_volume_get,
  365. .put = ct_alsa_mix_volume_put
  366. };
  367. static void
  368. do_line_mic_switch(struct ct_atc *atc, enum CTALSA_MIXER_CTL type)
  369. {
  370. if (MIXER_LINEIN_C_S == type) {
  371. atc->select_line_in(atc);
  372. set_switch_state(atc->mixer, MIXER_MIC_C_S, 0);
  373. snd_ctl_notify(atc->card, SNDRV_CTL_EVENT_MASK_VALUE,
  374. &kctls[1]->id);
  375. } else if (MIXER_MIC_C_S == type) {
  376. atc->select_mic_in(atc);
  377. set_switch_state(atc->mixer, MIXER_LINEIN_C_S, 0);
  378. snd_ctl_notify(atc->card, SNDRV_CTL_EVENT_MASK_VALUE,
  379. &kctls[0]->id);
  380. }
  381. }
  382. static void
  383. do_digit_io_switch(struct ct_atc *atc, int state)
  384. {
  385. struct ct_mixer *mixer = atc->mixer;
  386. if (state) {
  387. atc->select_digit_io(atc);
  388. atc->spdif_out_unmute(atc,
  389. get_switch_state(mixer, MIXER_SPDIFO_P_S));
  390. atc->spdif_in_unmute(atc, 1);
  391. atc->line_in_unmute(atc, 0);
  392. return;
  393. }
  394. if (get_switch_state(mixer, MIXER_LINEIN_C_S))
  395. atc->select_line_in(atc);
  396. else if (get_switch_state(mixer, MIXER_MIC_C_S))
  397. atc->select_mic_in(atc);
  398. atc->spdif_out_unmute(atc, 0);
  399. atc->spdif_in_unmute(atc, 0);
  400. atc->line_in_unmute(atc, 1);
  401. return;
  402. }
  403. static int ct_alsa_mix_switch_info(struct snd_kcontrol *kcontrol,
  404. struct snd_ctl_elem_info *uinfo)
  405. {
  406. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  407. uinfo->count = 1;
  408. uinfo->value.integer.min = 0;
  409. uinfo->value.integer.max = 1;
  410. uinfo->value.integer.step = 1;
  411. return 0;
  412. }
  413. static int ct_alsa_mix_switch_get(struct snd_kcontrol *kcontrol,
  414. struct snd_ctl_elem_value *ucontrol)
  415. {
  416. struct ct_mixer *mixer =
  417. ((struct ct_atc *)snd_kcontrol_chip(kcontrol))->mixer;
  418. enum CTALSA_MIXER_CTL type = kcontrol->private_value;
  419. ucontrol->value.integer.value[0] = get_switch_state(mixer, type);
  420. return 0;
  421. }
  422. static int ct_alsa_mix_switch_put(struct snd_kcontrol *kcontrol,
  423. struct snd_ctl_elem_value *ucontrol)
  424. {
  425. struct ct_atc *atc = snd_kcontrol_chip(kcontrol);
  426. struct ct_mixer *mixer = atc->mixer;
  427. enum CTALSA_MIXER_CTL type = kcontrol->private_value;
  428. int state = 0;
  429. state = ucontrol->value.integer.value[0];
  430. if (get_switch_state(mixer, type) == state)
  431. return 0;
  432. set_switch_state(mixer, type, state);
  433. /* Do changes in mixer. */
  434. if ((SWH_CAPTURE_START <= type) && (SWH_CAPTURE_END >= type)) {
  435. if (state) {
  436. ct_mixer_recording_select(mixer,
  437. get_amixer_index(type));
  438. } else {
  439. ct_mixer_recording_unselect(mixer,
  440. get_amixer_index(type));
  441. }
  442. }
  443. /* Do changes out of mixer. */
  444. if (state && (MIXER_LINEIN_C_S == type || MIXER_MIC_C_S == type))
  445. do_line_mic_switch(atc, type);
  446. else if (MIXER_WAVEF_P_S == type)
  447. atc->line_front_unmute(atc, state);
  448. else if (MIXER_WAVES_P_S == type)
  449. atc->line_surround_unmute(atc, state);
  450. else if (MIXER_WAVEC_P_S == type)
  451. atc->line_clfe_unmute(atc, state);
  452. else if (MIXER_WAVER_P_S == type)
  453. atc->line_rear_unmute(atc, state);
  454. else if (MIXER_LINEIN_P_S == type)
  455. atc->line_in_unmute(atc, state);
  456. else if (MIXER_SPDIFO_P_S == type)
  457. atc->spdif_out_unmute(atc, state);
  458. else if (MIXER_SPDIFI_P_S == type)
  459. atc->spdif_in_unmute(atc, state);
  460. else if (MIXER_DIGITAL_IO_S == type)
  461. do_digit_io_switch(atc, state);
  462. return 1;
  463. }
  464. static struct snd_kcontrol_new swh_ctl = {
  465. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  466. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  467. .info = ct_alsa_mix_switch_info,
  468. .get = ct_alsa_mix_switch_get,
  469. .put = ct_alsa_mix_switch_put
  470. };
  471. static int ct_spdif_info(struct snd_kcontrol *kcontrol,
  472. struct snd_ctl_elem_info *uinfo)
  473. {
  474. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  475. uinfo->count = 1;
  476. return 0;
  477. }
  478. static int ct_spdif_get_mask(struct snd_kcontrol *kcontrol,
  479. struct snd_ctl_elem_value *ucontrol)
  480. {
  481. ucontrol->value.iec958.status[0] = 0xff;
  482. ucontrol->value.iec958.status[1] = 0xff;
  483. ucontrol->value.iec958.status[2] = 0xff;
  484. ucontrol->value.iec958.status[3] = 0xff;
  485. return 0;
  486. }
  487. static int ct_spdif_default_get(struct snd_kcontrol *kcontrol,
  488. struct snd_ctl_elem_value *ucontrol)
  489. {
  490. unsigned int status = SNDRV_PCM_DEFAULT_CON_SPDIF;
  491. ucontrol->value.iec958.status[0] = (status >> 0) & 0xff;
  492. ucontrol->value.iec958.status[1] = (status >> 8) & 0xff;
  493. ucontrol->value.iec958.status[2] = (status >> 16) & 0xff;
  494. ucontrol->value.iec958.status[3] = (status >> 24) & 0xff;
  495. return 0;
  496. }
  497. static int ct_spdif_get(struct snd_kcontrol *kcontrol,
  498. struct snd_ctl_elem_value *ucontrol)
  499. {
  500. struct ct_atc *atc = snd_kcontrol_chip(kcontrol);
  501. unsigned int status = 0;
  502. atc->spdif_out_get_status(atc, &status);
  503. ucontrol->value.iec958.status[0] = (status >> 0) & 0xff;
  504. ucontrol->value.iec958.status[1] = (status >> 8) & 0xff;
  505. ucontrol->value.iec958.status[2] = (status >> 16) & 0xff;
  506. ucontrol->value.iec958.status[3] = (status >> 24) & 0xff;
  507. return 0;
  508. }
  509. static int ct_spdif_put(struct snd_kcontrol *kcontrol,
  510. struct snd_ctl_elem_value *ucontrol)
  511. {
  512. struct ct_atc *atc = snd_kcontrol_chip(kcontrol);
  513. int change = 1;
  514. unsigned int status = 0, old_status = 0;
  515. status = (ucontrol->value.iec958.status[0] << 0) |
  516. (ucontrol->value.iec958.status[1] << 8) |
  517. (ucontrol->value.iec958.status[2] << 16) |
  518. (ucontrol->value.iec958.status[3] << 24);
  519. atc->spdif_out_get_status(atc, &old_status);
  520. change = (old_status != status);
  521. if (change)
  522. atc->spdif_out_set_status(atc, status);
  523. return change;
  524. }
  525. static struct snd_kcontrol_new iec958_mask_ctl = {
  526. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  527. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  528. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
  529. .count = 1,
  530. .info = ct_spdif_info,
  531. .get = ct_spdif_get_mask,
  532. .private_value = MIXER_IEC958_MASK
  533. };
  534. static struct snd_kcontrol_new iec958_default_ctl = {
  535. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  536. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  537. .count = 1,
  538. .info = ct_spdif_info,
  539. .get = ct_spdif_default_get,
  540. .put = ct_spdif_put,
  541. .private_value = MIXER_IEC958_DEFAULT
  542. };
  543. static struct snd_kcontrol_new iec958_ctl = {
  544. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  545. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  546. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
  547. .count = 1,
  548. .info = ct_spdif_info,
  549. .get = ct_spdif_get,
  550. .put = ct_spdif_put,
  551. .private_value = MIXER_IEC958_STREAM
  552. };
  553. #define NUM_IEC958_CTL 3
  554. static int
  555. ct_mixer_kcontrol_new(struct ct_mixer *mixer, struct snd_kcontrol_new *new)
  556. {
  557. struct snd_kcontrol *kctl = NULL;
  558. int err = 0;
  559. kctl = snd_ctl_new1(new, mixer->atc);
  560. if (NULL == kctl)
  561. return -ENOMEM;
  562. if (SNDRV_CTL_ELEM_IFACE_PCM == kctl->id.iface)
  563. kctl->id.device = IEC958;
  564. err = snd_ctl_add(mixer->atc->card, kctl);
  565. if (err)
  566. return err;
  567. switch (new->private_value) {
  568. case MIXER_LINEIN_C_S:
  569. kctls[0] = kctl; break;
  570. case MIXER_MIC_C_S:
  571. kctls[1] = kctl; break;
  572. default:
  573. break;
  574. }
  575. return 0;
  576. }
  577. static int ct_mixer_kcontrols_create(struct ct_mixer *mixer)
  578. {
  579. enum CTALSA_MIXER_CTL type = 0;
  580. struct ct_atc *atc = mixer->atc;
  581. int err = 0;
  582. /* Create snd kcontrol instances on demand */
  583. for (type = VOL_MIXER_START; type <= VOL_MIXER_END; type++) {
  584. if (ct_kcontrol_init_table[type].ctl) {
  585. vol_ctl.name = ct_kcontrol_init_table[type].name;
  586. vol_ctl.private_value = (unsigned long)type;
  587. err = ct_mixer_kcontrol_new(mixer, &vol_ctl);
  588. if (err)
  589. return err;
  590. }
  591. }
  592. ct_kcontrol_init_table[MIXER_DIGITAL_IO_S].ctl =
  593. atc->have_digit_io_switch(atc);
  594. for (type = SWH_MIXER_START; type <= SWH_MIXER_END; type++) {
  595. if (ct_kcontrol_init_table[type].ctl) {
  596. swh_ctl.name = ct_kcontrol_init_table[type].name;
  597. swh_ctl.private_value = (unsigned long)type;
  598. err = ct_mixer_kcontrol_new(mixer, &swh_ctl);
  599. if (err)
  600. return err;
  601. }
  602. }
  603. err = ct_mixer_kcontrol_new(mixer, &iec958_mask_ctl);
  604. if (err)
  605. return err;
  606. err = ct_mixer_kcontrol_new(mixer, &iec958_default_ctl);
  607. if (err)
  608. return err;
  609. err = ct_mixer_kcontrol_new(mixer, &iec958_ctl);
  610. if (err)
  611. return err;
  612. atc->line_front_unmute(atc, 1);
  613. set_switch_state(mixer, MIXER_WAVEF_P_S, 1);
  614. atc->line_surround_unmute(atc, 0);
  615. set_switch_state(mixer, MIXER_WAVES_P_S, 0);
  616. atc->line_clfe_unmute(atc, 0);
  617. set_switch_state(mixer, MIXER_WAVEC_P_S, 0);
  618. atc->line_rear_unmute(atc, 0);
  619. set_switch_state(mixer, MIXER_WAVER_P_S, 0);
  620. atc->spdif_out_unmute(atc, 0);
  621. set_switch_state(mixer, MIXER_SPDIFO_P_S, 0);
  622. atc->line_in_unmute(atc, 0);
  623. set_switch_state(mixer, MIXER_LINEIN_P_S, 0);
  624. atc->spdif_in_unmute(atc, 0);
  625. set_switch_state(mixer, MIXER_SPDIFI_P_S, 0);
  626. set_switch_state(mixer, MIXER_PCM_C_S, 1);
  627. set_switch_state(mixer, MIXER_LINEIN_C_S, 1);
  628. set_switch_state(mixer, MIXER_SPDIFI_C_S, 1);
  629. return 0;
  630. }
  631. static void
  632. ct_mixer_recording_select(struct ct_mixer *mixer, enum CT_AMIXER_CTL type)
  633. {
  634. struct amixer *amix_d = NULL;
  635. struct sum *sum_c = NULL;
  636. int i = 0;
  637. for (i = 0; i < 2; i++) {
  638. amix_d = mixer->amixers[type*CHN_NUM+i];
  639. sum_c = mixer->sums[SUM_IN_F_C*CHN_NUM+i];
  640. amix_d->ops->set_sum(amix_d, sum_c);
  641. amix_d->ops->commit_write(amix_d);
  642. }
  643. }
  644. static void
  645. ct_mixer_recording_unselect(struct ct_mixer *mixer, enum CT_AMIXER_CTL type)
  646. {
  647. struct amixer *amix_d = NULL;
  648. int i = 0;
  649. for (i = 0; i < 2; i++) {
  650. amix_d = mixer->amixers[type*CHN_NUM+i];
  651. amix_d->ops->set_sum(amix_d, NULL);
  652. amix_d->ops->commit_write(amix_d);
  653. }
  654. }
  655. static int ct_mixer_get_resources(struct ct_mixer *mixer)
  656. {
  657. struct sum_mgr *sum_mgr = NULL;
  658. struct sum *sum = NULL;
  659. struct sum_desc sum_desc = {0};
  660. struct amixer_mgr *amixer_mgr = NULL;
  661. struct amixer *amixer = NULL;
  662. struct amixer_desc am_desc = {0};
  663. int err = 0;
  664. int i = 0;
  665. /* Allocate sum resources for mixer obj */
  666. sum_mgr = (struct sum_mgr *)mixer->atc->rsc_mgrs[SUM];
  667. sum_desc.msr = mixer->atc->msr;
  668. for (i = 0; i < (NUM_CT_SUMS * CHN_NUM); i++) {
  669. err = sum_mgr->get_sum(sum_mgr, &sum_desc, &sum);
  670. if (err) {
  671. printk(KERN_ERR "ctxfi:Failed to get sum resources for "
  672. "front output!\n");
  673. break;
  674. }
  675. mixer->sums[i] = sum;
  676. }
  677. if (err)
  678. goto error1;
  679. /* Allocate amixer resources for mixer obj */
  680. amixer_mgr = (struct amixer_mgr *)mixer->atc->rsc_mgrs[AMIXER];
  681. am_desc.msr = mixer->atc->msr;
  682. for (i = 0; i < (NUM_CT_AMIXERS * CHN_NUM); i++) {
  683. err = amixer_mgr->get_amixer(amixer_mgr, &am_desc, &amixer);
  684. if (err) {
  685. printk(KERN_ERR "ctxfi:Failed to get amixer resources "
  686. "for mixer obj!\n");
  687. break;
  688. }
  689. mixer->amixers[i] = amixer;
  690. }
  691. if (err)
  692. goto error2;
  693. return 0;
  694. error2:
  695. for (i = 0; i < (NUM_CT_AMIXERS * CHN_NUM); i++) {
  696. if (NULL != mixer->amixers[i]) {
  697. amixer = mixer->amixers[i];
  698. amixer_mgr->put_amixer(amixer_mgr, amixer);
  699. mixer->amixers[i] = NULL;
  700. }
  701. }
  702. error1:
  703. for (i = 0; i < (NUM_CT_SUMS * CHN_NUM); i++) {
  704. if (NULL != mixer->sums[i]) {
  705. sum_mgr->put_sum(sum_mgr, (struct sum *)mixer->sums[i]);
  706. mixer->sums[i] = NULL;
  707. }
  708. }
  709. return err;
  710. }
  711. static int ct_mixer_get_mem(struct ct_mixer **rmixer)
  712. {
  713. struct ct_mixer *mixer = NULL;
  714. int err = 0;
  715. *rmixer = NULL;
  716. /* Allocate mem for mixer obj */
  717. mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
  718. if (NULL == mixer)
  719. return -ENOMEM;
  720. mixer->amixers = kzalloc(sizeof(void *)*(NUM_CT_AMIXERS*CHN_NUM),
  721. GFP_KERNEL);
  722. if (NULL == mixer->amixers) {
  723. err = -ENOMEM;
  724. goto error1;
  725. }
  726. mixer->sums = kzalloc(sizeof(void *)*(NUM_CT_SUMS*CHN_NUM), GFP_KERNEL);
  727. if (NULL == mixer->sums) {
  728. err = -ENOMEM;
  729. goto error2;
  730. }
  731. *rmixer = mixer;
  732. return 0;
  733. error2:
  734. kfree(mixer->amixers);
  735. error1:
  736. kfree(mixer);
  737. return err;
  738. }
  739. static int ct_mixer_topology_build(struct ct_mixer *mixer)
  740. {
  741. struct sum *sum = NULL;
  742. struct amixer *amix_d = NULL, *amix_s = NULL;
  743. enum CT_AMIXER_CTL i = 0, j = 0;
  744. /* Build topology from destination to source */
  745. /* Set up Master mixer */
  746. for (i = AMIXER_MASTER_F, j = SUM_IN_F;
  747. i <= AMIXER_MASTER_S; i++, j++) {
  748. amix_d = mixer->amixers[i*CHN_NUM];
  749. sum = mixer->sums[j*CHN_NUM];
  750. amix_d->ops->setup(amix_d, &sum->rsc, INIT_VOL, NULL);
  751. amix_d = mixer->amixers[i*CHN_NUM+1];
  752. sum = mixer->sums[j*CHN_NUM+1];
  753. amix_d->ops->setup(amix_d, &sum->rsc, INIT_VOL, NULL);
  754. }
  755. /* Set up Wave-out mixer */
  756. for (i = AMIXER_WAVE_F, j = AMIXER_MASTER_F;
  757. i <= AMIXER_WAVE_S; i++, j++) {
  758. amix_d = mixer->amixers[i*CHN_NUM];
  759. amix_s = mixer->amixers[j*CHN_NUM];
  760. amix_d->ops->setup(amix_d, &amix_s->rsc, INIT_VOL, NULL);
  761. amix_d = mixer->amixers[i*CHN_NUM+1];
  762. amix_s = mixer->amixers[j*CHN_NUM+1];
  763. amix_d->ops->setup(amix_d, &amix_s->rsc, INIT_VOL, NULL);
  764. }
  765. /* Set up S/PDIF-out mixer */
  766. amix_d = mixer->amixers[AMIXER_SPDIFO*CHN_NUM];
  767. amix_s = mixer->amixers[AMIXER_MASTER_F*CHN_NUM];
  768. amix_d->ops->setup(amix_d, &amix_s->rsc, INIT_VOL, NULL);
  769. amix_d = mixer->amixers[AMIXER_SPDIFO*CHN_NUM+1];
  770. amix_s = mixer->amixers[AMIXER_MASTER_F*CHN_NUM+1];
  771. amix_d->ops->setup(amix_d, &amix_s->rsc, INIT_VOL, NULL);
  772. /* Set up PCM-in mixer */
  773. for (i = AMIXER_PCM_F, j = SUM_IN_F; i <= AMIXER_PCM_S; i++, j++) {
  774. amix_d = mixer->amixers[i*CHN_NUM];
  775. sum = mixer->sums[j*CHN_NUM];
  776. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  777. amix_d = mixer->amixers[i*CHN_NUM+1];
  778. sum = mixer->sums[j*CHN_NUM+1];
  779. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  780. }
  781. /* Set up Line-in mixer */
  782. amix_d = mixer->amixers[AMIXER_LINEIN*CHN_NUM];
  783. sum = mixer->sums[SUM_IN_F*CHN_NUM];
  784. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  785. amix_d = mixer->amixers[AMIXER_LINEIN*CHN_NUM+1];
  786. sum = mixer->sums[SUM_IN_F*CHN_NUM+1];
  787. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  788. /* Set up Mic-in mixer */
  789. amix_d = mixer->amixers[AMIXER_MIC*CHN_NUM];
  790. sum = mixer->sums[SUM_IN_F*CHN_NUM];
  791. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  792. amix_d = mixer->amixers[AMIXER_MIC*CHN_NUM+1];
  793. sum = mixer->sums[SUM_IN_F*CHN_NUM+1];
  794. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  795. /* Set up S/PDIF-in mixer */
  796. amix_d = mixer->amixers[AMIXER_SPDIFI*CHN_NUM];
  797. sum = mixer->sums[SUM_IN_F*CHN_NUM];
  798. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  799. amix_d = mixer->amixers[AMIXER_SPDIFI*CHN_NUM+1];
  800. sum = mixer->sums[SUM_IN_F*CHN_NUM+1];
  801. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  802. /* Set up Master recording mixer */
  803. amix_d = mixer->amixers[AMIXER_MASTER_F_C*CHN_NUM];
  804. sum = mixer->sums[SUM_IN_F_C*CHN_NUM];
  805. amix_d->ops->setup(amix_d, &sum->rsc, INIT_VOL, NULL);
  806. amix_d = mixer->amixers[AMIXER_MASTER_F_C*CHN_NUM+1];
  807. sum = mixer->sums[SUM_IN_F_C*CHN_NUM+1];
  808. amix_d->ops->setup(amix_d, &sum->rsc, INIT_VOL, NULL);
  809. /* Set up PCM-in recording mixer */
  810. amix_d = mixer->amixers[AMIXER_PCM_F_C*CHN_NUM];
  811. sum = mixer->sums[SUM_IN_F_C*CHN_NUM];
  812. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  813. amix_d = mixer->amixers[AMIXER_PCM_F_C*CHN_NUM+1];
  814. sum = mixer->sums[SUM_IN_F_C*CHN_NUM+1];
  815. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  816. /* Set up Line-in recording mixer */
  817. amix_d = mixer->amixers[AMIXER_LINEIN_C*CHN_NUM];
  818. sum = mixer->sums[SUM_IN_F_C*CHN_NUM];
  819. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  820. amix_d = mixer->amixers[AMIXER_LINEIN_C*CHN_NUM+1];
  821. sum = mixer->sums[SUM_IN_F_C*CHN_NUM+1];
  822. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  823. /* Set up Mic-in recording mixer */
  824. amix_d = mixer->amixers[AMIXER_MIC_C*CHN_NUM];
  825. sum = mixer->sums[SUM_IN_F_C*CHN_NUM];
  826. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  827. amix_d = mixer->amixers[AMIXER_MIC_C*CHN_NUM+1];
  828. sum = mixer->sums[SUM_IN_F_C*CHN_NUM+1];
  829. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  830. /* Set up S/PDIF-in recording mixer */
  831. amix_d = mixer->amixers[AMIXER_SPDIFI_C*CHN_NUM];
  832. sum = mixer->sums[SUM_IN_F_C*CHN_NUM];
  833. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  834. amix_d = mixer->amixers[AMIXER_SPDIFI_C*CHN_NUM+1];
  835. sum = mixer->sums[SUM_IN_F_C*CHN_NUM+1];
  836. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  837. return 0;
  838. }
  839. static int mixer_set_input_port(struct amixer *amixer, struct rsc *rsc)
  840. {
  841. amixer->ops->set_input(amixer, rsc);
  842. amixer->ops->commit_write(amixer);
  843. return 0;
  844. }
  845. static enum CT_AMIXER_CTL port_to_amixer(enum MIXER_PORT_T type)
  846. {
  847. switch (type) {
  848. case MIX_WAVE_FRONT: return AMIXER_WAVE_F;
  849. case MIX_WAVE_SURROUND: return AMIXER_WAVE_S;
  850. case MIX_WAVE_CENTLFE: return AMIXER_WAVE_C;
  851. case MIX_WAVE_REAR: return AMIXER_WAVE_R;
  852. case MIX_PCMO_FRONT: return AMIXER_MASTER_F_C;
  853. case MIX_SPDIF_OUT: return AMIXER_SPDIFO;
  854. case MIX_LINE_IN: return AMIXER_LINEIN;
  855. case MIX_MIC_IN: return AMIXER_MIC;
  856. case MIX_SPDIF_IN: return AMIXER_SPDIFI;
  857. case MIX_PCMI_FRONT: return AMIXER_PCM_F;
  858. case MIX_PCMI_SURROUND: return AMIXER_PCM_S;
  859. case MIX_PCMI_CENTLFE: return AMIXER_PCM_C;
  860. case MIX_PCMI_REAR: return AMIXER_PCM_R;
  861. default: return 0;
  862. }
  863. }
  864. static int mixer_get_output_ports(struct ct_mixer *mixer,
  865. enum MIXER_PORT_T type,
  866. struct rsc **rleft, struct rsc **rright)
  867. {
  868. enum CT_AMIXER_CTL amix = port_to_amixer(type);
  869. if (NULL != rleft)
  870. *rleft = &((struct amixer *)mixer->amixers[amix*CHN_NUM])->rsc;
  871. if (NULL != rright)
  872. *rright =
  873. &((struct amixer *)mixer->amixers[amix*CHN_NUM+1])->rsc;
  874. return 0;
  875. }
  876. static int mixer_set_input_left(struct ct_mixer *mixer,
  877. enum MIXER_PORT_T type, struct rsc *rsc)
  878. {
  879. enum CT_AMIXER_CTL amix = port_to_amixer(type);
  880. mixer_set_input_port(mixer->amixers[amix*CHN_NUM], rsc);
  881. amix = get_recording_amixer(amix);
  882. if (amix < NUM_CT_AMIXERS)
  883. mixer_set_input_port(mixer->amixers[amix*CHN_NUM], rsc);
  884. return 0;
  885. }
  886. static int
  887. mixer_set_input_right(struct ct_mixer *mixer,
  888. enum MIXER_PORT_T type, struct rsc *rsc)
  889. {
  890. enum CT_AMIXER_CTL amix = port_to_amixer(type);
  891. mixer_set_input_port(mixer->amixers[amix*CHN_NUM+1], rsc);
  892. amix = get_recording_amixer(amix);
  893. if (amix < NUM_CT_AMIXERS)
  894. mixer_set_input_port(mixer->amixers[amix*CHN_NUM+1], rsc);
  895. return 0;
  896. }
  897. int ct_mixer_destroy(struct ct_mixer *mixer)
  898. {
  899. struct sum_mgr *sum_mgr = (struct sum_mgr *)mixer->atc->rsc_mgrs[SUM];
  900. struct amixer_mgr *amixer_mgr =
  901. (struct amixer_mgr *)mixer->atc->rsc_mgrs[AMIXER];
  902. struct amixer *amixer = NULL;
  903. int i = 0;
  904. /* Release amixer resources */
  905. for (i = 0; i < (NUM_CT_AMIXERS * CHN_NUM); i++) {
  906. if (NULL != mixer->amixers[i]) {
  907. amixer = mixer->amixers[i];
  908. amixer_mgr->put_amixer(amixer_mgr, amixer);
  909. }
  910. }
  911. /* Release sum resources */
  912. for (i = 0; i < (NUM_CT_SUMS * CHN_NUM); i++) {
  913. if (NULL != mixer->sums[i])
  914. sum_mgr->put_sum(sum_mgr, (struct sum *)mixer->sums[i]);
  915. }
  916. /* Release mem assigned to mixer object */
  917. kfree(mixer->sums);
  918. kfree(mixer->amixers);
  919. kfree(mixer);
  920. return 0;
  921. }
  922. int ct_mixer_create(struct ct_atc *atc, struct ct_mixer **rmixer)
  923. {
  924. struct ct_mixer *mixer = NULL;
  925. int err = 0;
  926. *rmixer = NULL;
  927. /* Allocate mem for mixer obj */
  928. err = ct_mixer_get_mem(&mixer);
  929. if (err)
  930. return err;
  931. mixer->switch_state = 0;
  932. mixer->atc = atc;
  933. /* Set operations */
  934. mixer->get_output_ports = mixer_get_output_ports;
  935. mixer->set_input_left = mixer_set_input_left;
  936. mixer->set_input_right = mixer_set_input_right;
  937. /* Allocate chip resources for mixer obj */
  938. err = ct_mixer_get_resources(mixer);
  939. if (err)
  940. goto error;
  941. /* Build internal mixer topology */
  942. ct_mixer_topology_build(mixer);
  943. *rmixer = mixer;
  944. return 0;
  945. error:
  946. ct_mixer_destroy(mixer);
  947. return err;
  948. }
  949. int ct_alsa_mix_create(struct ct_atc *atc,
  950. enum CTALSADEVS device,
  951. const char *device_name)
  952. {
  953. int err = 0;
  954. /* Create snd kcontrol instances on demand */
  955. vol_ctl.device = swh_ctl.device = device;
  956. err = ct_mixer_kcontrols_create((struct ct_mixer *)atc->mixer);
  957. if (err)
  958. return err;
  959. strcpy(atc->card->mixername, device_name);
  960. return 0;
  961. }