ctmixer.c 30 KB

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