ctmixer.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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 = kcalloc(NUM_CT_AMIXERS * CHN_NUM, sizeof(void *),
  801. GFP_KERNEL);
  802. if (!mixer->amixers) {
  803. err = -ENOMEM;
  804. goto error1;
  805. }
  806. mixer->sums = kcalloc(NUM_CT_SUMS * CHN_NUM, sizeof(void *),
  807. GFP_KERNEL);
  808. if (!mixer->sums) {
  809. err = -ENOMEM;
  810. goto error2;
  811. }
  812. *rmixer = mixer;
  813. return 0;
  814. error2:
  815. kfree(mixer->amixers);
  816. error1:
  817. kfree(mixer);
  818. return err;
  819. }
  820. static int ct_mixer_topology_build(struct ct_mixer *mixer)
  821. {
  822. struct sum *sum;
  823. struct amixer *amix_d, *amix_s;
  824. enum CT_AMIXER_CTL i, j;
  825. enum CT_SUM_CTL k;
  826. /* Build topology from destination to source */
  827. /* Set up Master mixer */
  828. for (i = AMIXER_MASTER_F, k = SUM_IN_F;
  829. i <= AMIXER_MASTER_S; i++, k++) {
  830. amix_d = mixer->amixers[i*CHN_NUM];
  831. sum = mixer->sums[k*CHN_NUM];
  832. amix_d->ops->setup(amix_d, &sum->rsc, INIT_VOL, NULL);
  833. amix_d = mixer->amixers[i*CHN_NUM+1];
  834. sum = mixer->sums[k*CHN_NUM+1];
  835. amix_d->ops->setup(amix_d, &sum->rsc, INIT_VOL, NULL);
  836. }
  837. /* Set up Wave-out mixer */
  838. for (i = AMIXER_WAVE_F, j = AMIXER_MASTER_F;
  839. i <= AMIXER_WAVE_S; i++, j++) {
  840. amix_d = mixer->amixers[i*CHN_NUM];
  841. amix_s = mixer->amixers[j*CHN_NUM];
  842. amix_d->ops->setup(amix_d, &amix_s->rsc, INIT_VOL, NULL);
  843. amix_d = mixer->amixers[i*CHN_NUM+1];
  844. amix_s = mixer->amixers[j*CHN_NUM+1];
  845. amix_d->ops->setup(amix_d, &amix_s->rsc, INIT_VOL, NULL);
  846. }
  847. /* Set up S/PDIF-out mixer */
  848. amix_d = mixer->amixers[AMIXER_SPDIFO*CHN_NUM];
  849. amix_s = mixer->amixers[AMIXER_MASTER_F*CHN_NUM];
  850. amix_d->ops->setup(amix_d, &amix_s->rsc, INIT_VOL, NULL);
  851. amix_d = mixer->amixers[AMIXER_SPDIFO*CHN_NUM+1];
  852. amix_s = mixer->amixers[AMIXER_MASTER_F*CHN_NUM+1];
  853. amix_d->ops->setup(amix_d, &amix_s->rsc, INIT_VOL, NULL);
  854. /* Set up PCM-in mixer */
  855. for (i = AMIXER_PCM_F, k = SUM_IN_F; i <= AMIXER_PCM_S; i++, k++) {
  856. amix_d = mixer->amixers[i*CHN_NUM];
  857. sum = mixer->sums[k*CHN_NUM];
  858. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  859. amix_d = mixer->amixers[i*CHN_NUM+1];
  860. sum = mixer->sums[k*CHN_NUM+1];
  861. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  862. }
  863. /* Set up Line-in mixer */
  864. amix_d = mixer->amixers[AMIXER_LINEIN*CHN_NUM];
  865. sum = mixer->sums[SUM_IN_F*CHN_NUM];
  866. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  867. amix_d = mixer->amixers[AMIXER_LINEIN*CHN_NUM+1];
  868. sum = mixer->sums[SUM_IN_F*CHN_NUM+1];
  869. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  870. /* Set up Mic-in mixer */
  871. amix_d = mixer->amixers[AMIXER_MIC*CHN_NUM];
  872. sum = mixer->sums[SUM_IN_F*CHN_NUM];
  873. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  874. amix_d = mixer->amixers[AMIXER_MIC*CHN_NUM+1];
  875. sum = mixer->sums[SUM_IN_F*CHN_NUM+1];
  876. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  877. /* Set up S/PDIF-in mixer */
  878. amix_d = mixer->amixers[AMIXER_SPDIFI*CHN_NUM];
  879. sum = mixer->sums[SUM_IN_F*CHN_NUM];
  880. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  881. amix_d = mixer->amixers[AMIXER_SPDIFI*CHN_NUM+1];
  882. sum = mixer->sums[SUM_IN_F*CHN_NUM+1];
  883. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  884. /* Set up Master recording mixer */
  885. amix_d = mixer->amixers[AMIXER_MASTER_F_C*CHN_NUM];
  886. sum = mixer->sums[SUM_IN_F_C*CHN_NUM];
  887. amix_d->ops->setup(amix_d, &sum->rsc, INIT_VOL, NULL);
  888. amix_d = mixer->amixers[AMIXER_MASTER_F_C*CHN_NUM+1];
  889. sum = mixer->sums[SUM_IN_F_C*CHN_NUM+1];
  890. amix_d->ops->setup(amix_d, &sum->rsc, INIT_VOL, NULL);
  891. /* Set up PCM-in recording mixer */
  892. amix_d = mixer->amixers[AMIXER_PCM_F_C*CHN_NUM];
  893. sum = mixer->sums[SUM_IN_F_C*CHN_NUM];
  894. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  895. amix_d = mixer->amixers[AMIXER_PCM_F_C*CHN_NUM+1];
  896. sum = mixer->sums[SUM_IN_F_C*CHN_NUM+1];
  897. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  898. /* Set up Line-in recording mixer */
  899. amix_d = mixer->amixers[AMIXER_LINEIN_C*CHN_NUM];
  900. sum = mixer->sums[SUM_IN_F_C*CHN_NUM];
  901. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  902. amix_d = mixer->amixers[AMIXER_LINEIN_C*CHN_NUM+1];
  903. sum = mixer->sums[SUM_IN_F_C*CHN_NUM+1];
  904. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  905. /* Set up Mic-in recording mixer */
  906. amix_d = mixer->amixers[AMIXER_MIC_C*CHN_NUM];
  907. sum = mixer->sums[SUM_IN_F_C*CHN_NUM];
  908. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  909. amix_d = mixer->amixers[AMIXER_MIC_C*CHN_NUM+1];
  910. sum = mixer->sums[SUM_IN_F_C*CHN_NUM+1];
  911. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  912. /* Set up S/PDIF-in recording mixer */
  913. amix_d = mixer->amixers[AMIXER_SPDIFI_C*CHN_NUM];
  914. sum = mixer->sums[SUM_IN_F_C*CHN_NUM];
  915. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  916. amix_d = mixer->amixers[AMIXER_SPDIFI_C*CHN_NUM+1];
  917. sum = mixer->sums[SUM_IN_F_C*CHN_NUM+1];
  918. amix_d->ops->setup(amix_d, NULL, INIT_VOL, sum);
  919. return 0;
  920. }
  921. static int mixer_set_input_port(struct amixer *amixer, struct rsc *rsc)
  922. {
  923. amixer->ops->set_input(amixer, rsc);
  924. amixer->ops->commit_write(amixer);
  925. return 0;
  926. }
  927. static enum CT_AMIXER_CTL port_to_amixer(enum MIXER_PORT_T type)
  928. {
  929. switch (type) {
  930. case MIX_WAVE_FRONT: return AMIXER_WAVE_F;
  931. case MIX_WAVE_SURROUND: return AMIXER_WAVE_S;
  932. case MIX_WAVE_CENTLFE: return AMIXER_WAVE_C;
  933. case MIX_WAVE_REAR: return AMIXER_WAVE_R;
  934. case MIX_PCMO_FRONT: return AMIXER_MASTER_F_C;
  935. case MIX_SPDIF_OUT: return AMIXER_SPDIFO;
  936. case MIX_LINE_IN: return AMIXER_LINEIN;
  937. case MIX_MIC_IN: return AMIXER_MIC;
  938. case MIX_SPDIF_IN: return AMIXER_SPDIFI;
  939. case MIX_PCMI_FRONT: return AMIXER_PCM_F;
  940. case MIX_PCMI_SURROUND: return AMIXER_PCM_S;
  941. case MIX_PCMI_CENTLFE: return AMIXER_PCM_C;
  942. case MIX_PCMI_REAR: return AMIXER_PCM_R;
  943. default: return 0;
  944. }
  945. }
  946. static int mixer_get_output_ports(struct ct_mixer *mixer,
  947. enum MIXER_PORT_T type,
  948. struct rsc **rleft, struct rsc **rright)
  949. {
  950. enum CT_AMIXER_CTL amix = port_to_amixer(type);
  951. if (NULL != rleft)
  952. *rleft = &((struct amixer *)mixer->amixers[amix*CHN_NUM])->rsc;
  953. if (NULL != rright)
  954. *rright =
  955. &((struct amixer *)mixer->amixers[amix*CHN_NUM+1])->rsc;
  956. return 0;
  957. }
  958. static int mixer_set_input_left(struct ct_mixer *mixer,
  959. enum MIXER_PORT_T type, struct rsc *rsc)
  960. {
  961. enum CT_AMIXER_CTL amix = port_to_amixer(type);
  962. mixer_set_input_port(mixer->amixers[amix*CHN_NUM], rsc);
  963. amix = get_recording_amixer(amix);
  964. if (amix < NUM_CT_AMIXERS)
  965. mixer_set_input_port(mixer->amixers[amix*CHN_NUM], rsc);
  966. return 0;
  967. }
  968. static int
  969. mixer_set_input_right(struct ct_mixer *mixer,
  970. enum MIXER_PORT_T type, struct rsc *rsc)
  971. {
  972. enum CT_AMIXER_CTL amix = port_to_amixer(type);
  973. mixer_set_input_port(mixer->amixers[amix*CHN_NUM+1], rsc);
  974. amix = get_recording_amixer(amix);
  975. if (amix < NUM_CT_AMIXERS)
  976. mixer_set_input_port(mixer->amixers[amix*CHN_NUM+1], rsc);
  977. return 0;
  978. }
  979. #ifdef CONFIG_PM_SLEEP
  980. static int mixer_resume(struct ct_mixer *mixer)
  981. {
  982. int i, state;
  983. struct amixer *amixer;
  984. /* resume topology and volume gain. */
  985. for (i = 0; i < NUM_CT_AMIXERS*CHN_NUM; i++) {
  986. amixer = mixer->amixers[i];
  987. amixer->ops->commit_write(amixer);
  988. }
  989. /* resume switch state. */
  990. for (i = SWH_MIXER_START; i <= SWH_MIXER_END; i++) {
  991. state = get_switch_state(mixer, i);
  992. do_switch(mixer->atc, i, state);
  993. }
  994. return 0;
  995. }
  996. #endif
  997. int ct_mixer_destroy(struct ct_mixer *mixer)
  998. {
  999. struct sum_mgr *sum_mgr = (struct sum_mgr *)mixer->atc->rsc_mgrs[SUM];
  1000. struct amixer_mgr *amixer_mgr =
  1001. (struct amixer_mgr *)mixer->atc->rsc_mgrs[AMIXER];
  1002. struct amixer *amixer;
  1003. int i = 0;
  1004. /* Release amixer resources */
  1005. for (i = 0; i < (NUM_CT_AMIXERS * CHN_NUM); i++) {
  1006. if (NULL != mixer->amixers[i]) {
  1007. amixer = mixer->amixers[i];
  1008. amixer_mgr->put_amixer(amixer_mgr, amixer);
  1009. }
  1010. }
  1011. /* Release sum resources */
  1012. for (i = 0; i < (NUM_CT_SUMS * CHN_NUM); i++) {
  1013. if (NULL != mixer->sums[i])
  1014. sum_mgr->put_sum(sum_mgr, (struct sum *)mixer->sums[i]);
  1015. }
  1016. /* Release mem assigned to mixer object */
  1017. kfree(mixer->sums);
  1018. kfree(mixer->amixers);
  1019. kfree(mixer);
  1020. return 0;
  1021. }
  1022. int ct_mixer_create(struct ct_atc *atc, struct ct_mixer **rmixer)
  1023. {
  1024. struct ct_mixer *mixer;
  1025. int err;
  1026. *rmixer = NULL;
  1027. /* Allocate mem for mixer obj */
  1028. err = ct_mixer_get_mem(&mixer);
  1029. if (err)
  1030. return err;
  1031. mixer->switch_state = 0;
  1032. mixer->atc = atc;
  1033. /* Set operations */
  1034. mixer->get_output_ports = mixer_get_output_ports;
  1035. mixer->set_input_left = mixer_set_input_left;
  1036. mixer->set_input_right = mixer_set_input_right;
  1037. #ifdef CONFIG_PM_SLEEP
  1038. mixer->resume = mixer_resume;
  1039. #endif
  1040. /* Allocate chip resources for mixer obj */
  1041. err = ct_mixer_get_resources(mixer);
  1042. if (err)
  1043. goto error;
  1044. /* Build internal mixer topology */
  1045. ct_mixer_topology_build(mixer);
  1046. *rmixer = mixer;
  1047. return 0;
  1048. error:
  1049. ct_mixer_destroy(mixer);
  1050. return err;
  1051. }
  1052. int ct_alsa_mix_create(struct ct_atc *atc,
  1053. enum CTALSADEVS device,
  1054. const char *device_name)
  1055. {
  1056. int err;
  1057. /* Create snd kcontrol instances on demand */
  1058. /* vol_ctl.device = swh_ctl.device = device; */ /* better w/ device 0 */
  1059. err = ct_mixer_kcontrols_create((struct ct_mixer *)atc->mixer);
  1060. if (err)
  1061. return err;
  1062. strcpy(atc->card->mixername, device_name);
  1063. return 0;
  1064. }