vx_mixer.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /*
  2. * Driver for Digigram VX soundcards
  3. *
  4. * Common mixer part
  5. *
  6. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <sound/driver.h>
  23. #include <sound/core.h>
  24. #include <sound/control.h>
  25. #include <sound/tlv.h>
  26. #include <sound/vx_core.h>
  27. #include "vx_cmd.h"
  28. /*
  29. * write a codec data (24bit)
  30. */
  31. static void vx_write_codec_reg(struct vx_core *chip, int codec, unsigned int data)
  32. {
  33. unsigned long flags;
  34. snd_assert(chip->ops->write_codec, return);
  35. if (chip->chip_status & VX_STAT_IS_STALE)
  36. return;
  37. spin_lock_irqsave(&chip->lock, flags);
  38. chip->ops->write_codec(chip, codec, data);
  39. spin_unlock_irqrestore(&chip->lock, flags);
  40. }
  41. /*
  42. * Data type used to access the Codec
  43. */
  44. union vx_codec_data {
  45. u32 l;
  46. #ifdef SNDRV_BIG_ENDIAN
  47. struct w {
  48. u16 h;
  49. u16 l;
  50. } w;
  51. struct b {
  52. u8 hh;
  53. u8 mh;
  54. u8 ml;
  55. u8 ll;
  56. } b;
  57. #else /* LITTLE_ENDIAN */
  58. struct w {
  59. u16 l;
  60. u16 h;
  61. } w;
  62. struct b {
  63. u8 ll;
  64. u8 ml;
  65. u8 mh;
  66. u8 hh;
  67. } b;
  68. #endif
  69. };
  70. #define SET_CDC_DATA_SEL(di,s) ((di).b.mh = (u8) (s))
  71. #define SET_CDC_DATA_REG(di,r) ((di).b.ml = (u8) (r))
  72. #define SET_CDC_DATA_VAL(di,d) ((di).b.ll = (u8) (d))
  73. #define SET_CDC_DATA_INIT(di) ((di).l = 0L, SET_CDC_DATA_SEL(di,XX_CODEC_SELECTOR))
  74. /*
  75. * set up codec register and write the value
  76. * @codec: the codec id, 0 or 1
  77. * @reg: register index
  78. * @val: data value
  79. */
  80. static void vx_set_codec_reg(struct vx_core *chip, int codec, int reg, int val)
  81. {
  82. union vx_codec_data data;
  83. /* DAC control register */
  84. SET_CDC_DATA_INIT(data);
  85. SET_CDC_DATA_REG(data, reg);
  86. SET_CDC_DATA_VAL(data, val);
  87. vx_write_codec_reg(chip, codec, data.l);
  88. }
  89. /*
  90. * vx_set_analog_output_level - set the output attenuation level
  91. * @codec: the output codec, 0 or 1. (1 for VXP440 only)
  92. * @left: left output level, 0 = mute
  93. * @right: right output level
  94. */
  95. static void vx_set_analog_output_level(struct vx_core *chip, int codec, int left, int right)
  96. {
  97. left = chip->hw->output_level_max - left;
  98. right = chip->hw->output_level_max - right;
  99. if (chip->ops->akm_write) {
  100. chip->ops->akm_write(chip, XX_CODEC_LEVEL_LEFT_REGISTER, left);
  101. chip->ops->akm_write(chip, XX_CODEC_LEVEL_RIGHT_REGISTER, right);
  102. } else {
  103. /* convert to attenuation level: 0 = 0dB (max), 0xe3 = -113.5 dB (min) */
  104. vx_set_codec_reg(chip, codec, XX_CODEC_LEVEL_LEFT_REGISTER, left);
  105. vx_set_codec_reg(chip, codec, XX_CODEC_LEVEL_RIGHT_REGISTER, right);
  106. }
  107. }
  108. /*
  109. * vx_toggle_dac_mute - mute/unmute DAC
  110. * @mute: 0 = unmute, 1 = mute
  111. */
  112. #define DAC_ATTEN_MIN 0x08
  113. #define DAC_ATTEN_MAX 0x38
  114. void vx_toggle_dac_mute(struct vx_core *chip, int mute)
  115. {
  116. unsigned int i;
  117. for (i = 0; i < chip->hw->num_codecs; i++) {
  118. if (chip->ops->akm_write)
  119. chip->ops->akm_write(chip, XX_CODEC_DAC_CONTROL_REGISTER, mute); /* XXX */
  120. else
  121. vx_set_codec_reg(chip, i, XX_CODEC_DAC_CONTROL_REGISTER,
  122. mute ? DAC_ATTEN_MAX : DAC_ATTEN_MIN);
  123. }
  124. }
  125. /*
  126. * vx_reset_codec - reset and initialize the codecs
  127. */
  128. void vx_reset_codec(struct vx_core *chip, int cold_reset)
  129. {
  130. unsigned int i;
  131. int port = chip->type >= VX_TYPE_VXPOCKET ? 0x75 : 0x65;
  132. chip->ops->reset_codec(chip);
  133. /* AKM codecs should be initialized in reset_codec callback */
  134. if (! chip->ops->akm_write) {
  135. /* initialize old codecs */
  136. for (i = 0; i < chip->hw->num_codecs; i++) {
  137. /* DAC control register (change level when zero crossing + mute) */
  138. vx_set_codec_reg(chip, i, XX_CODEC_DAC_CONTROL_REGISTER, DAC_ATTEN_MAX);
  139. /* ADC control register */
  140. vx_set_codec_reg(chip, i, XX_CODEC_ADC_CONTROL_REGISTER, 0x00);
  141. /* Port mode register */
  142. vx_set_codec_reg(chip, i, XX_CODEC_PORT_MODE_REGISTER, port);
  143. /* Clock control register */
  144. vx_set_codec_reg(chip, i, XX_CODEC_CLOCK_CONTROL_REGISTER, 0x00);
  145. }
  146. }
  147. /* mute analog output */
  148. for (i = 0; i < chip->hw->num_codecs; i++) {
  149. chip->output_level[i][0] = 0;
  150. chip->output_level[i][1] = 0;
  151. vx_set_analog_output_level(chip, i, 0, 0);
  152. }
  153. }
  154. /*
  155. * change the audio input source
  156. * @src: the target source (VX_AUDIO_SRC_XXX)
  157. */
  158. static void vx_change_audio_source(struct vx_core *chip, int src)
  159. {
  160. unsigned long flags;
  161. if (chip->chip_status & VX_STAT_IS_STALE)
  162. return;
  163. spin_lock_irqsave(&chip->lock, flags);
  164. chip->ops->change_audio_source(chip, src);
  165. spin_unlock_irqrestore(&chip->lock, flags);
  166. }
  167. /*
  168. * change the audio source if necessary and possible
  169. * returns 1 if the source is actually changed.
  170. */
  171. int vx_sync_audio_source(struct vx_core *chip)
  172. {
  173. if (chip->audio_source_target == chip->audio_source ||
  174. chip->pcm_running)
  175. return 0;
  176. vx_change_audio_source(chip, chip->audio_source_target);
  177. chip->audio_source = chip->audio_source_target;
  178. return 1;
  179. }
  180. /*
  181. * audio level, mute, monitoring
  182. */
  183. struct vx_audio_level {
  184. unsigned int has_level: 1;
  185. unsigned int has_monitor_level: 1;
  186. unsigned int has_mute: 1;
  187. unsigned int has_monitor_mute: 1;
  188. unsigned int mute;
  189. unsigned int monitor_mute;
  190. short level;
  191. short monitor_level;
  192. };
  193. static int vx_adjust_audio_level(struct vx_core *chip, int audio, int capture,
  194. struct vx_audio_level *info)
  195. {
  196. struct vx_rmh rmh;
  197. if (chip->chip_status & VX_STAT_IS_STALE)
  198. return -EBUSY;
  199. vx_init_rmh(&rmh, CMD_AUDIO_LEVEL_ADJUST);
  200. if (capture)
  201. rmh.Cmd[0] |= COMMAND_RECORD_MASK;
  202. /* Add Audio IO mask */
  203. rmh.Cmd[1] = 1 << audio;
  204. rmh.Cmd[2] = 0;
  205. if (info->has_level) {
  206. rmh.Cmd[0] |= VALID_AUDIO_IO_DIGITAL_LEVEL;
  207. rmh.Cmd[2] |= info->level;
  208. }
  209. if (info->has_monitor_level) {
  210. rmh.Cmd[0] |= VALID_AUDIO_IO_MONITORING_LEVEL;
  211. rmh.Cmd[2] |= ((unsigned int)info->monitor_level << 10);
  212. }
  213. if (info->has_mute) {
  214. rmh.Cmd[0] |= VALID_AUDIO_IO_MUTE_LEVEL;
  215. if (info->mute)
  216. rmh.Cmd[2] |= AUDIO_IO_HAS_MUTE_LEVEL;
  217. }
  218. if (info->has_monitor_mute) {
  219. /* validate flag for M2 at least to unmute it */
  220. rmh.Cmd[0] |= VALID_AUDIO_IO_MUTE_MONITORING_1 | VALID_AUDIO_IO_MUTE_MONITORING_2;
  221. if (info->monitor_mute)
  222. rmh.Cmd[2] |= AUDIO_IO_HAS_MUTE_MONITORING_1;
  223. }
  224. return vx_send_msg(chip, &rmh);
  225. }
  226. #if 0 // not used
  227. static int vx_read_audio_level(struct vx_core *chip, int audio, int capture,
  228. struct vx_audio_level *info)
  229. {
  230. int err;
  231. struct vx_rmh rmh;
  232. memset(info, 0, sizeof(*info));
  233. vx_init_rmh(&rmh, CMD_GET_AUDIO_LEVELS);
  234. if (capture)
  235. rmh.Cmd[0] |= COMMAND_RECORD_MASK;
  236. /* Add Audio IO mask */
  237. rmh.Cmd[1] = 1 << audio;
  238. err = vx_send_msg(chip, &rmh);
  239. if (err < 0)
  240. return err;
  241. info.level = rmh.Stat[0] & MASK_DSP_WORD_LEVEL;
  242. info.monitor_level = (rmh.Stat[0] >> 10) & MASK_DSP_WORD_LEVEL;
  243. info.mute = (rmh.Stat[i] & AUDIO_IO_HAS_MUTE_LEVEL) ? 1 : 0;
  244. info.monitor_mute = (rmh.Stat[i] & AUDIO_IO_HAS_MUTE_MONITORING_1) ? 1 : 0;
  245. return 0;
  246. }
  247. #endif // not used
  248. /*
  249. * set the monitoring level and mute state of the given audio
  250. * no more static, because must be called from vx_pcm to demute monitoring
  251. */
  252. int vx_set_monitor_level(struct vx_core *chip, int audio, int level, int active)
  253. {
  254. struct vx_audio_level info;
  255. memset(&info, 0, sizeof(info));
  256. info.has_monitor_level = 1;
  257. info.monitor_level = level;
  258. info.has_monitor_mute = 1;
  259. info.monitor_mute = !active;
  260. chip->audio_monitor[audio] = level;
  261. chip->audio_monitor_active[audio] = active;
  262. return vx_adjust_audio_level(chip, audio, 0, &info); /* playback only */
  263. }
  264. /*
  265. * set the mute status of the given audio
  266. */
  267. static int vx_set_audio_switch(struct vx_core *chip, int audio, int active)
  268. {
  269. struct vx_audio_level info;
  270. memset(&info, 0, sizeof(info));
  271. info.has_mute = 1;
  272. info.mute = !active;
  273. chip->audio_active[audio] = active;
  274. return vx_adjust_audio_level(chip, audio, 0, &info); /* playback only */
  275. }
  276. /*
  277. * set the mute status of the given audio
  278. */
  279. static int vx_set_audio_gain(struct vx_core *chip, int audio, int capture, int level)
  280. {
  281. struct vx_audio_level info;
  282. memset(&info, 0, sizeof(info));
  283. info.has_level = 1;
  284. info.level = level;
  285. chip->audio_gain[capture][audio] = level;
  286. return vx_adjust_audio_level(chip, audio, capture, &info);
  287. }
  288. /*
  289. * reset all audio levels
  290. */
  291. static void vx_reset_audio_levels(struct vx_core *chip)
  292. {
  293. unsigned int i, c;
  294. struct vx_audio_level info;
  295. memset(chip->audio_gain, 0, sizeof(chip->audio_gain));
  296. memset(chip->audio_active, 0, sizeof(chip->audio_active));
  297. memset(chip->audio_monitor, 0, sizeof(chip->audio_monitor));
  298. memset(chip->audio_monitor_active, 0, sizeof(chip->audio_monitor_active));
  299. for (c = 0; c < 2; c++) {
  300. for (i = 0; i < chip->hw->num_ins * 2; i++) {
  301. memset(&info, 0, sizeof(info));
  302. if (c == 0) {
  303. info.has_monitor_level = 1;
  304. info.has_mute = 1;
  305. info.has_monitor_mute = 1;
  306. }
  307. info.has_level = 1;
  308. info.level = CVAL_0DB; /* default: 0dB */
  309. vx_adjust_audio_level(chip, i, c, &info);
  310. chip->audio_gain[c][i] = CVAL_0DB;
  311. chip->audio_monitor[i] = CVAL_0DB;
  312. }
  313. }
  314. }
  315. /*
  316. * VU, peak meter record
  317. */
  318. #define VU_METER_CHANNELS 2
  319. struct vx_vu_meter {
  320. int saturated;
  321. int vu_level;
  322. int peak_level;
  323. };
  324. /*
  325. * get the VU and peak meter values
  326. * @audio: the audio index
  327. * @capture: 0 = playback, 1 = capture operation
  328. * @info: the array of vx_vu_meter records (size = 2).
  329. */
  330. static int vx_get_audio_vu_meter(struct vx_core *chip, int audio, int capture, struct vx_vu_meter *info)
  331. {
  332. struct vx_rmh rmh;
  333. int i, err;
  334. if (chip->chip_status & VX_STAT_IS_STALE)
  335. return -EBUSY;
  336. vx_init_rmh(&rmh, CMD_AUDIO_VU_PIC_METER);
  337. rmh.LgStat += 2 * VU_METER_CHANNELS;
  338. if (capture)
  339. rmh.Cmd[0] |= COMMAND_RECORD_MASK;
  340. /* Add Audio IO mask */
  341. rmh.Cmd[1] = 0;
  342. for (i = 0; i < VU_METER_CHANNELS; i++)
  343. rmh.Cmd[1] |= 1 << (audio + i);
  344. err = vx_send_msg(chip, &rmh);
  345. if (err < 0)
  346. return err;
  347. /* Read response */
  348. for (i = 0; i < 2 * VU_METER_CHANNELS; i +=2) {
  349. info->saturated = (rmh.Stat[0] & (1 << (audio + i))) ? 1 : 0;
  350. info->vu_level = rmh.Stat[i + 1];
  351. info->peak_level = rmh.Stat[i + 2];
  352. info++;
  353. }
  354. return 0;
  355. }
  356. /*
  357. * control API entries
  358. */
  359. /*
  360. * output level control
  361. */
  362. static int vx_output_level_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  363. {
  364. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  365. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  366. uinfo->count = 2;
  367. uinfo->value.integer.min = 0;
  368. uinfo->value.integer.max = chip->hw->output_level_max;
  369. return 0;
  370. }
  371. static int vx_output_level_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  372. {
  373. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  374. int codec = kcontrol->id.index;
  375. mutex_lock(&chip->mixer_mutex);
  376. ucontrol->value.integer.value[0] = chip->output_level[codec][0];
  377. ucontrol->value.integer.value[1] = chip->output_level[codec][1];
  378. mutex_unlock(&chip->mixer_mutex);
  379. return 0;
  380. }
  381. static int vx_output_level_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  382. {
  383. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  384. int codec = kcontrol->id.index;
  385. mutex_lock(&chip->mixer_mutex);
  386. if (ucontrol->value.integer.value[0] != chip->output_level[codec][0] ||
  387. ucontrol->value.integer.value[1] != chip->output_level[codec][1]) {
  388. vx_set_analog_output_level(chip, codec,
  389. ucontrol->value.integer.value[0],
  390. ucontrol->value.integer.value[1]);
  391. chip->output_level[codec][0] = ucontrol->value.integer.value[0];
  392. chip->output_level[codec][1] = ucontrol->value.integer.value[1];
  393. mutex_unlock(&chip->mixer_mutex);
  394. return 1;
  395. }
  396. mutex_unlock(&chip->mixer_mutex);
  397. return 0;
  398. }
  399. static struct snd_kcontrol_new vx_control_output_level = {
  400. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  401. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  402. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  403. .name = "Master Playback Volume",
  404. .info = vx_output_level_info,
  405. .get = vx_output_level_get,
  406. .put = vx_output_level_put,
  407. /* tlv will be filled later */
  408. };
  409. /*
  410. * audio source select
  411. */
  412. static int vx_audio_src_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  413. {
  414. static char *texts_mic[3] = {
  415. "Digital", "Line", "Mic"
  416. };
  417. static char *texts_vx2[2] = {
  418. "Digital", "Analog"
  419. };
  420. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  421. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  422. uinfo->count = 1;
  423. if (chip->type >= VX_TYPE_VXPOCKET) {
  424. uinfo->value.enumerated.items = 3;
  425. if (uinfo->value.enumerated.item > 2)
  426. uinfo->value.enumerated.item = 2;
  427. strcpy(uinfo->value.enumerated.name,
  428. texts_mic[uinfo->value.enumerated.item]);
  429. } else {
  430. uinfo->value.enumerated.items = 2;
  431. if (uinfo->value.enumerated.item > 1)
  432. uinfo->value.enumerated.item = 1;
  433. strcpy(uinfo->value.enumerated.name,
  434. texts_vx2[uinfo->value.enumerated.item]);
  435. }
  436. return 0;
  437. }
  438. static int vx_audio_src_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  439. {
  440. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  441. ucontrol->value.enumerated.item[0] = chip->audio_source_target;
  442. return 0;
  443. }
  444. static int vx_audio_src_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  445. {
  446. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  447. mutex_lock(&chip->mixer_mutex);
  448. if (chip->audio_source_target != ucontrol->value.enumerated.item[0]) {
  449. chip->audio_source_target = ucontrol->value.enumerated.item[0];
  450. vx_sync_audio_source(chip);
  451. mutex_unlock(&chip->mixer_mutex);
  452. return 1;
  453. }
  454. mutex_unlock(&chip->mixer_mutex);
  455. return 0;
  456. }
  457. static struct snd_kcontrol_new vx_control_audio_src = {
  458. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  459. .name = "Capture Source",
  460. .info = vx_audio_src_info,
  461. .get = vx_audio_src_get,
  462. .put = vx_audio_src_put,
  463. };
  464. /*
  465. * clock mode selection
  466. */
  467. static int vx_clock_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  468. {
  469. static char *texts[3] = {
  470. "Auto", "Internal", "External"
  471. };
  472. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  473. uinfo->count = 1;
  474. uinfo->value.enumerated.items = 3;
  475. if (uinfo->value.enumerated.item > 2)
  476. uinfo->value.enumerated.item = 2;
  477. strcpy(uinfo->value.enumerated.name,
  478. texts[uinfo->value.enumerated.item]);
  479. return 0;
  480. }
  481. static int vx_clock_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  482. {
  483. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  484. ucontrol->value.enumerated.item[0] = chip->clock_mode;
  485. return 0;
  486. }
  487. static int vx_clock_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  488. {
  489. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  490. mutex_lock(&chip->mixer_mutex);
  491. if (chip->clock_mode != ucontrol->value.enumerated.item[0]) {
  492. chip->clock_mode = ucontrol->value.enumerated.item[0];
  493. vx_set_clock(chip, chip->freq);
  494. mutex_unlock(&chip->mixer_mutex);
  495. return 1;
  496. }
  497. mutex_unlock(&chip->mixer_mutex);
  498. return 0;
  499. }
  500. static struct snd_kcontrol_new vx_control_clock_mode = {
  501. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  502. .name = "Clock Mode",
  503. .info = vx_clock_mode_info,
  504. .get = vx_clock_mode_get,
  505. .put = vx_clock_mode_put,
  506. };
  507. /*
  508. * Audio Gain
  509. */
  510. static int vx_audio_gain_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  511. {
  512. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  513. uinfo->count = 2;
  514. uinfo->value.integer.min = 0;
  515. uinfo->value.integer.max = CVAL_MAX;
  516. return 0;
  517. }
  518. static int vx_audio_gain_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  519. {
  520. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  521. int audio = kcontrol->private_value & 0xff;
  522. int capture = (kcontrol->private_value >> 8) & 1;
  523. mutex_lock(&chip->mixer_mutex);
  524. ucontrol->value.integer.value[0] = chip->audio_gain[capture][audio];
  525. ucontrol->value.integer.value[1] = chip->audio_gain[capture][audio+1];
  526. mutex_unlock(&chip->mixer_mutex);
  527. return 0;
  528. }
  529. static int vx_audio_gain_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  530. {
  531. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  532. int audio = kcontrol->private_value & 0xff;
  533. int capture = (kcontrol->private_value >> 8) & 1;
  534. mutex_lock(&chip->mixer_mutex);
  535. if (ucontrol->value.integer.value[0] != chip->audio_gain[capture][audio] ||
  536. ucontrol->value.integer.value[1] != chip->audio_gain[capture][audio+1]) {
  537. vx_set_audio_gain(chip, audio, capture, ucontrol->value.integer.value[0]);
  538. vx_set_audio_gain(chip, audio+1, capture, ucontrol->value.integer.value[1]);
  539. mutex_unlock(&chip->mixer_mutex);
  540. return 1;
  541. }
  542. mutex_unlock(&chip->mixer_mutex);
  543. return 0;
  544. }
  545. static int vx_audio_monitor_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  546. {
  547. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  548. int audio = kcontrol->private_value & 0xff;
  549. mutex_lock(&chip->mixer_mutex);
  550. ucontrol->value.integer.value[0] = chip->audio_monitor[audio];
  551. ucontrol->value.integer.value[1] = chip->audio_monitor[audio+1];
  552. mutex_unlock(&chip->mixer_mutex);
  553. return 0;
  554. }
  555. static int vx_audio_monitor_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  556. {
  557. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  558. int audio = kcontrol->private_value & 0xff;
  559. mutex_lock(&chip->mixer_mutex);
  560. if (ucontrol->value.integer.value[0] != chip->audio_monitor[audio] ||
  561. ucontrol->value.integer.value[1] != chip->audio_monitor[audio+1]) {
  562. vx_set_monitor_level(chip, audio, ucontrol->value.integer.value[0],
  563. chip->audio_monitor_active[audio]);
  564. vx_set_monitor_level(chip, audio+1, ucontrol->value.integer.value[1],
  565. chip->audio_monitor_active[audio+1]);
  566. mutex_unlock(&chip->mixer_mutex);
  567. return 1;
  568. }
  569. mutex_unlock(&chip->mixer_mutex);
  570. return 0;
  571. }
  572. static int vx_audio_sw_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  573. {
  574. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  575. uinfo->count = 2;
  576. uinfo->value.integer.min = 0;
  577. uinfo->value.integer.max = 1;
  578. return 0;
  579. }
  580. static int vx_audio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  581. {
  582. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  583. int audio = kcontrol->private_value & 0xff;
  584. mutex_lock(&chip->mixer_mutex);
  585. ucontrol->value.integer.value[0] = chip->audio_active[audio];
  586. ucontrol->value.integer.value[1] = chip->audio_active[audio+1];
  587. mutex_unlock(&chip->mixer_mutex);
  588. return 0;
  589. }
  590. static int vx_audio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  591. {
  592. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  593. int audio = kcontrol->private_value & 0xff;
  594. mutex_lock(&chip->mixer_mutex);
  595. if (ucontrol->value.integer.value[0] != chip->audio_active[audio] ||
  596. ucontrol->value.integer.value[1] != chip->audio_active[audio+1]) {
  597. vx_set_audio_switch(chip, audio, ucontrol->value.integer.value[0]);
  598. vx_set_audio_switch(chip, audio+1, ucontrol->value.integer.value[1]);
  599. mutex_unlock(&chip->mixer_mutex);
  600. return 1;
  601. }
  602. mutex_unlock(&chip->mixer_mutex);
  603. return 0;
  604. }
  605. static int vx_monitor_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  606. {
  607. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  608. int audio = kcontrol->private_value & 0xff;
  609. mutex_lock(&chip->mixer_mutex);
  610. ucontrol->value.integer.value[0] = chip->audio_monitor_active[audio];
  611. ucontrol->value.integer.value[1] = chip->audio_monitor_active[audio+1];
  612. mutex_unlock(&chip->mixer_mutex);
  613. return 0;
  614. }
  615. static int vx_monitor_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  616. {
  617. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  618. int audio = kcontrol->private_value & 0xff;
  619. mutex_lock(&chip->mixer_mutex);
  620. if (ucontrol->value.integer.value[0] != chip->audio_monitor_active[audio] ||
  621. ucontrol->value.integer.value[1] != chip->audio_monitor_active[audio+1]) {
  622. vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
  623. ucontrol->value.integer.value[0]);
  624. vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
  625. ucontrol->value.integer.value[1]);
  626. mutex_unlock(&chip->mixer_mutex);
  627. return 1;
  628. }
  629. mutex_unlock(&chip->mixer_mutex);
  630. return 0;
  631. }
  632. static const DECLARE_TLV_DB_SCALE(db_scale_audio_gain, -10975, 25, 0);
  633. static struct snd_kcontrol_new vx_control_audio_gain = {
  634. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  635. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  636. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  637. /* name will be filled later */
  638. .info = vx_audio_gain_info,
  639. .get = vx_audio_gain_get,
  640. .put = vx_audio_gain_put,
  641. .tlv = { .p = db_scale_audio_gain },
  642. };
  643. static struct snd_kcontrol_new vx_control_output_switch = {
  644. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  645. .name = "PCM Playback Switch",
  646. .info = vx_audio_sw_info,
  647. .get = vx_audio_sw_get,
  648. .put = vx_audio_sw_put
  649. };
  650. static struct snd_kcontrol_new vx_control_monitor_gain = {
  651. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  652. .name = "Monitoring Volume",
  653. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  654. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  655. .info = vx_audio_gain_info, /* shared */
  656. .get = vx_audio_monitor_get,
  657. .put = vx_audio_monitor_put,
  658. .tlv = { .p = db_scale_audio_gain },
  659. };
  660. static struct snd_kcontrol_new vx_control_monitor_switch = {
  661. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  662. .name = "Monitoring Switch",
  663. .info = vx_audio_sw_info, /* shared */
  664. .get = vx_monitor_sw_get,
  665. .put = vx_monitor_sw_put
  666. };
  667. /*
  668. * IEC958 status bits
  669. */
  670. static int vx_iec958_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  671. {
  672. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  673. uinfo->count = 1;
  674. return 0;
  675. }
  676. static int vx_iec958_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  677. {
  678. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  679. mutex_lock(&chip->mixer_mutex);
  680. ucontrol->value.iec958.status[0] = (chip->uer_bits >> 0) & 0xff;
  681. ucontrol->value.iec958.status[1] = (chip->uer_bits >> 8) & 0xff;
  682. ucontrol->value.iec958.status[2] = (chip->uer_bits >> 16) & 0xff;
  683. ucontrol->value.iec958.status[3] = (chip->uer_bits >> 24) & 0xff;
  684. mutex_unlock(&chip->mixer_mutex);
  685. return 0;
  686. }
  687. static int vx_iec958_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  688. {
  689. ucontrol->value.iec958.status[0] = 0xff;
  690. ucontrol->value.iec958.status[1] = 0xff;
  691. ucontrol->value.iec958.status[2] = 0xff;
  692. ucontrol->value.iec958.status[3] = 0xff;
  693. return 0;
  694. }
  695. static int vx_iec958_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  696. {
  697. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  698. unsigned int val;
  699. val = (ucontrol->value.iec958.status[0] << 0) |
  700. (ucontrol->value.iec958.status[1] << 8) |
  701. (ucontrol->value.iec958.status[2] << 16) |
  702. (ucontrol->value.iec958.status[3] << 24);
  703. mutex_lock(&chip->mixer_mutex);
  704. if (chip->uer_bits != val) {
  705. chip->uer_bits = val;
  706. vx_set_iec958_status(chip, val);
  707. mutex_unlock(&chip->mixer_mutex);
  708. return 1;
  709. }
  710. mutex_unlock(&chip->mixer_mutex);
  711. return 0;
  712. }
  713. static struct snd_kcontrol_new vx_control_iec958_mask = {
  714. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  715. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  716. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  717. .info = vx_iec958_info, /* shared */
  718. .get = vx_iec958_mask_get,
  719. };
  720. static struct snd_kcontrol_new vx_control_iec958 = {
  721. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  722. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  723. .info = vx_iec958_info,
  724. .get = vx_iec958_get,
  725. .put = vx_iec958_put
  726. };
  727. /*
  728. * VU meter
  729. */
  730. #define METER_MAX 0xff
  731. #define METER_SHIFT 16
  732. static int vx_vu_meter_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  733. {
  734. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  735. uinfo->count = 2;
  736. uinfo->value.integer.min = 0;
  737. uinfo->value.integer.max = METER_MAX;
  738. return 0;
  739. }
  740. static int vx_vu_meter_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  741. {
  742. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  743. struct vx_vu_meter meter[2];
  744. int audio = kcontrol->private_value & 0xff;
  745. int capture = (kcontrol->private_value >> 8) & 1;
  746. vx_get_audio_vu_meter(chip, audio, capture, meter);
  747. ucontrol->value.integer.value[0] = meter[0].vu_level >> METER_SHIFT;
  748. ucontrol->value.integer.value[1] = meter[1].vu_level >> METER_SHIFT;
  749. return 0;
  750. }
  751. static int vx_peak_meter_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  752. {
  753. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  754. struct vx_vu_meter meter[2];
  755. int audio = kcontrol->private_value & 0xff;
  756. int capture = (kcontrol->private_value >> 8) & 1;
  757. vx_get_audio_vu_meter(chip, audio, capture, meter);
  758. ucontrol->value.integer.value[0] = meter[0].peak_level >> METER_SHIFT;
  759. ucontrol->value.integer.value[1] = meter[1].peak_level >> METER_SHIFT;
  760. return 0;
  761. }
  762. static int vx_saturation_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  763. {
  764. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  765. uinfo->count = 2;
  766. uinfo->value.integer.min = 0;
  767. uinfo->value.integer.max = 1;
  768. return 0;
  769. }
  770. static int vx_saturation_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  771. {
  772. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  773. struct vx_vu_meter meter[2];
  774. int audio = kcontrol->private_value & 0xff;
  775. vx_get_audio_vu_meter(chip, audio, 1, meter); /* capture only */
  776. ucontrol->value.integer.value[0] = meter[0].saturated;
  777. ucontrol->value.integer.value[1] = meter[1].saturated;
  778. return 0;
  779. }
  780. static struct snd_kcontrol_new vx_control_vu_meter = {
  781. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  782. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  783. /* name will be filled later */
  784. .info = vx_vu_meter_info,
  785. .get = vx_vu_meter_get,
  786. };
  787. static struct snd_kcontrol_new vx_control_peak_meter = {
  788. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  789. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  790. /* name will be filled later */
  791. .info = vx_vu_meter_info, /* shared */
  792. .get = vx_peak_meter_get,
  793. };
  794. static struct snd_kcontrol_new vx_control_saturation = {
  795. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  796. .name = "Input Saturation",
  797. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  798. .info = vx_saturation_info,
  799. .get = vx_saturation_get,
  800. };
  801. /*
  802. *
  803. */
  804. int snd_vx_mixer_new(struct vx_core *chip)
  805. {
  806. unsigned int i, c;
  807. int err;
  808. struct snd_kcontrol_new temp;
  809. struct snd_card *card = chip->card;
  810. char name[32];
  811. strcpy(card->mixername, card->driver);
  812. /* output level controls */
  813. for (i = 0; i < chip->hw->num_outs; i++) {
  814. temp = vx_control_output_level;
  815. temp.index = i;
  816. temp.tlv.p = chip->hw->output_level_db_scale;
  817. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  818. return err;
  819. }
  820. /* PCM volumes, switches, monitoring */
  821. for (i = 0; i < chip->hw->num_outs; i++) {
  822. int val = i * 2;
  823. temp = vx_control_audio_gain;
  824. temp.index = i;
  825. temp.name = "PCM Playback Volume";
  826. temp.private_value = val;
  827. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  828. return err;
  829. temp = vx_control_output_switch;
  830. temp.index = i;
  831. temp.private_value = val;
  832. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  833. return err;
  834. temp = vx_control_monitor_gain;
  835. temp.index = i;
  836. temp.private_value = val;
  837. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  838. return err;
  839. temp = vx_control_monitor_switch;
  840. temp.index = i;
  841. temp.private_value = val;
  842. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  843. return err;
  844. }
  845. for (i = 0; i < chip->hw->num_outs; i++) {
  846. temp = vx_control_audio_gain;
  847. temp.index = i;
  848. temp.name = "PCM Capture Volume";
  849. temp.private_value = (i * 2) | (1 << 8);
  850. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  851. return err;
  852. }
  853. /* Audio source */
  854. if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_audio_src, chip))) < 0)
  855. return err;
  856. /* clock mode */
  857. if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_clock_mode, chip))) < 0)
  858. return err;
  859. /* IEC958 controls */
  860. if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_iec958_mask, chip))) < 0)
  861. return err;
  862. if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_iec958, chip))) < 0)
  863. return err;
  864. /* VU, peak, saturation meters */
  865. for (c = 0; c < 2; c++) {
  866. static char *dir[2] = { "Output", "Input" };
  867. for (i = 0; i < chip->hw->num_ins; i++) {
  868. int val = (i * 2) | (c << 8);
  869. if (c == 1) {
  870. temp = vx_control_saturation;
  871. temp.index = i;
  872. temp.private_value = val;
  873. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  874. return err;
  875. }
  876. sprintf(name, "%s VU Meter", dir[c]);
  877. temp = vx_control_vu_meter;
  878. temp.index = i;
  879. temp.name = name;
  880. temp.private_value = val;
  881. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  882. return err;
  883. sprintf(name, "%s Peak Meter", dir[c]);
  884. temp = vx_control_peak_meter;
  885. temp.index = i;
  886. temp.name = name;
  887. temp.private_value = val;
  888. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  889. return err;
  890. }
  891. }
  892. vx_reset_audio_levels(chip);
  893. return 0;
  894. }