ctmixer.c 30 KB

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