ctmixer.c 27 KB

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