vx_mixer.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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/core.h>
  23. #include <sound/control.h>
  24. #include <sound/tlv.h>
  25. #include <sound/vx_core.h>
  26. #include "vx_cmd.h"
  27. /*
  28. * write a codec data (24bit)
  29. */
  30. static void vx_write_codec_reg(struct vx_core *chip, int codec, unsigned int data)
  31. {
  32. unsigned long flags;
  33. if (snd_BUG_ON(!chip->ops->write_codec))
  34. 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. unsigned int val[2], vmax;
  386. vmax = chip->hw->output_level_max;
  387. val[0] = ucontrol->value.integer.value[0];
  388. val[1] = ucontrol->value.integer.value[1];
  389. if (val[0] > vmax || val[1] > vmax)
  390. return -EINVAL;
  391. mutex_lock(&chip->mixer_mutex);
  392. if (val[0] != chip->output_level[codec][0] ||
  393. val[1] != chip->output_level[codec][1]) {
  394. vx_set_analog_output_level(chip, codec, val[0], val[1]);
  395. chip->output_level[codec][0] = val[0];
  396. chip->output_level[codec][1] = val[1];
  397. mutex_unlock(&chip->mixer_mutex);
  398. return 1;
  399. }
  400. mutex_unlock(&chip->mixer_mutex);
  401. return 0;
  402. }
  403. static struct snd_kcontrol_new vx_control_output_level = {
  404. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  405. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  406. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  407. .name = "Master Playback Volume",
  408. .info = vx_output_level_info,
  409. .get = vx_output_level_get,
  410. .put = vx_output_level_put,
  411. /* tlv will be filled later */
  412. };
  413. /*
  414. * audio source select
  415. */
  416. static int vx_audio_src_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  417. {
  418. static char *texts_mic[3] = {
  419. "Digital", "Line", "Mic"
  420. };
  421. static char *texts_vx2[2] = {
  422. "Digital", "Analog"
  423. };
  424. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  425. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  426. uinfo->count = 1;
  427. if (chip->type >= VX_TYPE_VXPOCKET) {
  428. uinfo->value.enumerated.items = 3;
  429. if (uinfo->value.enumerated.item > 2)
  430. uinfo->value.enumerated.item = 2;
  431. strcpy(uinfo->value.enumerated.name,
  432. texts_mic[uinfo->value.enumerated.item]);
  433. } else {
  434. uinfo->value.enumerated.items = 2;
  435. if (uinfo->value.enumerated.item > 1)
  436. uinfo->value.enumerated.item = 1;
  437. strcpy(uinfo->value.enumerated.name,
  438. texts_vx2[uinfo->value.enumerated.item]);
  439. }
  440. return 0;
  441. }
  442. static int vx_audio_src_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  443. {
  444. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  445. ucontrol->value.enumerated.item[0] = chip->audio_source_target;
  446. return 0;
  447. }
  448. static int vx_audio_src_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  449. {
  450. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  451. if (chip->type >= VX_TYPE_VXPOCKET) {
  452. if (ucontrol->value.enumerated.item[0] > 2)
  453. return -EINVAL;
  454. } else {
  455. if (ucontrol->value.enumerated.item[0] > 1)
  456. return -EINVAL;
  457. }
  458. mutex_lock(&chip->mixer_mutex);
  459. if (chip->audio_source_target != ucontrol->value.enumerated.item[0]) {
  460. chip->audio_source_target = ucontrol->value.enumerated.item[0];
  461. vx_sync_audio_source(chip);
  462. mutex_unlock(&chip->mixer_mutex);
  463. return 1;
  464. }
  465. mutex_unlock(&chip->mixer_mutex);
  466. return 0;
  467. }
  468. static struct snd_kcontrol_new vx_control_audio_src = {
  469. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  470. .name = "Capture Source",
  471. .info = vx_audio_src_info,
  472. .get = vx_audio_src_get,
  473. .put = vx_audio_src_put,
  474. };
  475. /*
  476. * clock mode selection
  477. */
  478. static int vx_clock_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  479. {
  480. static char *texts[3] = {
  481. "Auto", "Internal", "External"
  482. };
  483. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  484. uinfo->count = 1;
  485. uinfo->value.enumerated.items = 3;
  486. if (uinfo->value.enumerated.item > 2)
  487. uinfo->value.enumerated.item = 2;
  488. strcpy(uinfo->value.enumerated.name,
  489. texts[uinfo->value.enumerated.item]);
  490. return 0;
  491. }
  492. static int vx_clock_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  493. {
  494. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  495. ucontrol->value.enumerated.item[0] = chip->clock_mode;
  496. return 0;
  497. }
  498. static int vx_clock_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  499. {
  500. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  501. if (ucontrol->value.enumerated.item[0] > 2)
  502. return -EINVAL;
  503. mutex_lock(&chip->mixer_mutex);
  504. if (chip->clock_mode != ucontrol->value.enumerated.item[0]) {
  505. chip->clock_mode = ucontrol->value.enumerated.item[0];
  506. vx_set_clock(chip, chip->freq);
  507. mutex_unlock(&chip->mixer_mutex);
  508. return 1;
  509. }
  510. mutex_unlock(&chip->mixer_mutex);
  511. return 0;
  512. }
  513. static struct snd_kcontrol_new vx_control_clock_mode = {
  514. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  515. .name = "Clock Mode",
  516. .info = vx_clock_mode_info,
  517. .get = vx_clock_mode_get,
  518. .put = vx_clock_mode_put,
  519. };
  520. /*
  521. * Audio Gain
  522. */
  523. static int vx_audio_gain_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  524. {
  525. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  526. uinfo->count = 2;
  527. uinfo->value.integer.min = 0;
  528. uinfo->value.integer.max = CVAL_MAX;
  529. return 0;
  530. }
  531. static int vx_audio_gain_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  532. {
  533. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  534. int audio = kcontrol->private_value & 0xff;
  535. int capture = (kcontrol->private_value >> 8) & 1;
  536. mutex_lock(&chip->mixer_mutex);
  537. ucontrol->value.integer.value[0] = chip->audio_gain[capture][audio];
  538. ucontrol->value.integer.value[1] = chip->audio_gain[capture][audio+1];
  539. mutex_unlock(&chip->mixer_mutex);
  540. return 0;
  541. }
  542. static int vx_audio_gain_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  543. {
  544. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  545. int audio = kcontrol->private_value & 0xff;
  546. int capture = (kcontrol->private_value >> 8) & 1;
  547. unsigned int val[2];
  548. val[0] = ucontrol->value.integer.value[0];
  549. val[1] = ucontrol->value.integer.value[1];
  550. if (val[0] > CVAL_MAX || val[1] > CVAL_MAX)
  551. return -EINVAL;
  552. mutex_lock(&chip->mixer_mutex);
  553. if (val[0] != chip->audio_gain[capture][audio] ||
  554. val[1] != chip->audio_gain[capture][audio+1]) {
  555. vx_set_audio_gain(chip, audio, capture, val[0]);
  556. vx_set_audio_gain(chip, audio+1, capture, val[1]);
  557. mutex_unlock(&chip->mixer_mutex);
  558. return 1;
  559. }
  560. mutex_unlock(&chip->mixer_mutex);
  561. return 0;
  562. }
  563. static int vx_audio_monitor_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  564. {
  565. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  566. int audio = kcontrol->private_value & 0xff;
  567. mutex_lock(&chip->mixer_mutex);
  568. ucontrol->value.integer.value[0] = chip->audio_monitor[audio];
  569. ucontrol->value.integer.value[1] = chip->audio_monitor[audio+1];
  570. mutex_unlock(&chip->mixer_mutex);
  571. return 0;
  572. }
  573. static int vx_audio_monitor_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  574. {
  575. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  576. int audio = kcontrol->private_value & 0xff;
  577. unsigned int val[2];
  578. val[0] = ucontrol->value.integer.value[0];
  579. val[1] = ucontrol->value.integer.value[1];
  580. if (val[0] > CVAL_MAX || val[1] > CVAL_MAX)
  581. return -EINVAL;
  582. mutex_lock(&chip->mixer_mutex);
  583. if (val[0] != chip->audio_monitor[audio] ||
  584. val[1] != chip->audio_monitor[audio+1]) {
  585. vx_set_monitor_level(chip, audio, val[0],
  586. chip->audio_monitor_active[audio]);
  587. vx_set_monitor_level(chip, audio+1, val[1],
  588. chip->audio_monitor_active[audio+1]);
  589. mutex_unlock(&chip->mixer_mutex);
  590. return 1;
  591. }
  592. mutex_unlock(&chip->mixer_mutex);
  593. return 0;
  594. }
  595. #define vx_audio_sw_info snd_ctl_boolean_stereo_info
  596. static int vx_audio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  597. {
  598. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  599. int audio = kcontrol->private_value & 0xff;
  600. mutex_lock(&chip->mixer_mutex);
  601. ucontrol->value.integer.value[0] = chip->audio_active[audio];
  602. ucontrol->value.integer.value[1] = chip->audio_active[audio+1];
  603. mutex_unlock(&chip->mixer_mutex);
  604. return 0;
  605. }
  606. static int vx_audio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  607. {
  608. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  609. int audio = kcontrol->private_value & 0xff;
  610. mutex_lock(&chip->mixer_mutex);
  611. if (ucontrol->value.integer.value[0] != chip->audio_active[audio] ||
  612. ucontrol->value.integer.value[1] != chip->audio_active[audio+1]) {
  613. vx_set_audio_switch(chip, audio,
  614. !!ucontrol->value.integer.value[0]);
  615. vx_set_audio_switch(chip, audio+1,
  616. !!ucontrol->value.integer.value[1]);
  617. mutex_unlock(&chip->mixer_mutex);
  618. return 1;
  619. }
  620. mutex_unlock(&chip->mixer_mutex);
  621. return 0;
  622. }
  623. static int vx_monitor_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  624. {
  625. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  626. int audio = kcontrol->private_value & 0xff;
  627. mutex_lock(&chip->mixer_mutex);
  628. ucontrol->value.integer.value[0] = chip->audio_monitor_active[audio];
  629. ucontrol->value.integer.value[1] = chip->audio_monitor_active[audio+1];
  630. mutex_unlock(&chip->mixer_mutex);
  631. return 0;
  632. }
  633. static int vx_monitor_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  634. {
  635. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  636. int audio = kcontrol->private_value & 0xff;
  637. mutex_lock(&chip->mixer_mutex);
  638. if (ucontrol->value.integer.value[0] != chip->audio_monitor_active[audio] ||
  639. ucontrol->value.integer.value[1] != chip->audio_monitor_active[audio+1]) {
  640. vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
  641. !!ucontrol->value.integer.value[0]);
  642. vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
  643. !!ucontrol->value.integer.value[1]);
  644. mutex_unlock(&chip->mixer_mutex);
  645. return 1;
  646. }
  647. mutex_unlock(&chip->mixer_mutex);
  648. return 0;
  649. }
  650. static const DECLARE_TLV_DB_SCALE(db_scale_audio_gain, -10975, 25, 0);
  651. static struct snd_kcontrol_new vx_control_audio_gain = {
  652. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  653. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  654. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  655. /* name will be filled later */
  656. .info = vx_audio_gain_info,
  657. .get = vx_audio_gain_get,
  658. .put = vx_audio_gain_put,
  659. .tlv = { .p = db_scale_audio_gain },
  660. };
  661. static struct snd_kcontrol_new vx_control_output_switch = {
  662. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  663. .name = "PCM Playback Switch",
  664. .info = vx_audio_sw_info,
  665. .get = vx_audio_sw_get,
  666. .put = vx_audio_sw_put
  667. };
  668. static struct snd_kcontrol_new vx_control_monitor_gain = {
  669. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  670. .name = "Monitoring Volume",
  671. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  672. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  673. .info = vx_audio_gain_info, /* shared */
  674. .get = vx_audio_monitor_get,
  675. .put = vx_audio_monitor_put,
  676. .tlv = { .p = db_scale_audio_gain },
  677. };
  678. static struct snd_kcontrol_new vx_control_monitor_switch = {
  679. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  680. .name = "Monitoring Switch",
  681. .info = vx_audio_sw_info, /* shared */
  682. .get = vx_monitor_sw_get,
  683. .put = vx_monitor_sw_put
  684. };
  685. /*
  686. * IEC958 status bits
  687. */
  688. static int vx_iec958_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  689. {
  690. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  691. uinfo->count = 1;
  692. return 0;
  693. }
  694. static int vx_iec958_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  695. {
  696. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  697. mutex_lock(&chip->mixer_mutex);
  698. ucontrol->value.iec958.status[0] = (chip->uer_bits >> 0) & 0xff;
  699. ucontrol->value.iec958.status[1] = (chip->uer_bits >> 8) & 0xff;
  700. ucontrol->value.iec958.status[2] = (chip->uer_bits >> 16) & 0xff;
  701. ucontrol->value.iec958.status[3] = (chip->uer_bits >> 24) & 0xff;
  702. mutex_unlock(&chip->mixer_mutex);
  703. return 0;
  704. }
  705. static int vx_iec958_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  706. {
  707. ucontrol->value.iec958.status[0] = 0xff;
  708. ucontrol->value.iec958.status[1] = 0xff;
  709. ucontrol->value.iec958.status[2] = 0xff;
  710. ucontrol->value.iec958.status[3] = 0xff;
  711. return 0;
  712. }
  713. static int vx_iec958_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  714. {
  715. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  716. unsigned int val;
  717. val = (ucontrol->value.iec958.status[0] << 0) |
  718. (ucontrol->value.iec958.status[1] << 8) |
  719. (ucontrol->value.iec958.status[2] << 16) |
  720. (ucontrol->value.iec958.status[3] << 24);
  721. mutex_lock(&chip->mixer_mutex);
  722. if (chip->uer_bits != val) {
  723. chip->uer_bits = val;
  724. vx_set_iec958_status(chip, val);
  725. mutex_unlock(&chip->mixer_mutex);
  726. return 1;
  727. }
  728. mutex_unlock(&chip->mixer_mutex);
  729. return 0;
  730. }
  731. static struct snd_kcontrol_new vx_control_iec958_mask = {
  732. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  733. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  734. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  735. .info = vx_iec958_info, /* shared */
  736. .get = vx_iec958_mask_get,
  737. };
  738. static struct snd_kcontrol_new vx_control_iec958 = {
  739. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  740. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  741. .info = vx_iec958_info,
  742. .get = vx_iec958_get,
  743. .put = vx_iec958_put
  744. };
  745. /*
  746. * VU meter
  747. */
  748. #define METER_MAX 0xff
  749. #define METER_SHIFT 16
  750. static int vx_vu_meter_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  751. {
  752. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  753. uinfo->count = 2;
  754. uinfo->value.integer.min = 0;
  755. uinfo->value.integer.max = METER_MAX;
  756. return 0;
  757. }
  758. static int vx_vu_meter_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  759. {
  760. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  761. struct vx_vu_meter meter[2];
  762. int audio = kcontrol->private_value & 0xff;
  763. int capture = (kcontrol->private_value >> 8) & 1;
  764. vx_get_audio_vu_meter(chip, audio, capture, meter);
  765. ucontrol->value.integer.value[0] = meter[0].vu_level >> METER_SHIFT;
  766. ucontrol->value.integer.value[1] = meter[1].vu_level >> METER_SHIFT;
  767. return 0;
  768. }
  769. static int vx_peak_meter_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  770. {
  771. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  772. struct vx_vu_meter meter[2];
  773. int audio = kcontrol->private_value & 0xff;
  774. int capture = (kcontrol->private_value >> 8) & 1;
  775. vx_get_audio_vu_meter(chip, audio, capture, meter);
  776. ucontrol->value.integer.value[0] = meter[0].peak_level >> METER_SHIFT;
  777. ucontrol->value.integer.value[1] = meter[1].peak_level >> METER_SHIFT;
  778. return 0;
  779. }
  780. #define vx_saturation_info snd_ctl_boolean_stereo_info
  781. static int vx_saturation_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  782. {
  783. struct vx_core *chip = snd_kcontrol_chip(kcontrol);
  784. struct vx_vu_meter meter[2];
  785. int audio = kcontrol->private_value & 0xff;
  786. vx_get_audio_vu_meter(chip, audio, 1, meter); /* capture only */
  787. ucontrol->value.integer.value[0] = meter[0].saturated;
  788. ucontrol->value.integer.value[1] = meter[1].saturated;
  789. return 0;
  790. }
  791. static struct snd_kcontrol_new vx_control_vu_meter = {
  792. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  793. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  794. /* name will be filled later */
  795. .info = vx_vu_meter_info,
  796. .get = vx_vu_meter_get,
  797. };
  798. static struct snd_kcontrol_new vx_control_peak_meter = {
  799. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  800. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  801. /* name will be filled later */
  802. .info = vx_vu_meter_info, /* shared */
  803. .get = vx_peak_meter_get,
  804. };
  805. static struct snd_kcontrol_new vx_control_saturation = {
  806. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  807. .name = "Input Saturation",
  808. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  809. .info = vx_saturation_info,
  810. .get = vx_saturation_get,
  811. };
  812. /*
  813. *
  814. */
  815. int snd_vx_mixer_new(struct vx_core *chip)
  816. {
  817. unsigned int i, c;
  818. int err;
  819. struct snd_kcontrol_new temp;
  820. struct snd_card *card = chip->card;
  821. char name[32];
  822. strcpy(card->mixername, card->driver);
  823. /* output level controls */
  824. for (i = 0; i < chip->hw->num_outs; i++) {
  825. temp = vx_control_output_level;
  826. temp.index = i;
  827. temp.tlv.p = chip->hw->output_level_db_scale;
  828. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  829. return err;
  830. }
  831. /* PCM volumes, switches, monitoring */
  832. for (i = 0; i < chip->hw->num_outs; i++) {
  833. int val = i * 2;
  834. temp = vx_control_audio_gain;
  835. temp.index = i;
  836. temp.name = "PCM Playback Volume";
  837. temp.private_value = val;
  838. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  839. return err;
  840. temp = vx_control_output_switch;
  841. temp.index = i;
  842. temp.private_value = val;
  843. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  844. return err;
  845. temp = vx_control_monitor_gain;
  846. temp.index = i;
  847. temp.private_value = val;
  848. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  849. return err;
  850. temp = vx_control_monitor_switch;
  851. temp.index = i;
  852. temp.private_value = val;
  853. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  854. return err;
  855. }
  856. for (i = 0; i < chip->hw->num_outs; i++) {
  857. temp = vx_control_audio_gain;
  858. temp.index = i;
  859. temp.name = "PCM Capture Volume";
  860. temp.private_value = (i * 2) | (1 << 8);
  861. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  862. return err;
  863. }
  864. /* Audio source */
  865. if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_audio_src, chip))) < 0)
  866. return err;
  867. /* clock mode */
  868. if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_clock_mode, chip))) < 0)
  869. return err;
  870. /* IEC958 controls */
  871. if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_iec958_mask, chip))) < 0)
  872. return err;
  873. if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_iec958, chip))) < 0)
  874. return err;
  875. /* VU, peak, saturation meters */
  876. for (c = 0; c < 2; c++) {
  877. static char *dir[2] = { "Output", "Input" };
  878. for (i = 0; i < chip->hw->num_ins; i++) {
  879. int val = (i * 2) | (c << 8);
  880. if (c == 1) {
  881. temp = vx_control_saturation;
  882. temp.index = i;
  883. temp.private_value = val;
  884. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  885. return err;
  886. }
  887. sprintf(name, "%s VU Meter", dir[c]);
  888. temp = vx_control_vu_meter;
  889. temp.index = i;
  890. temp.name = name;
  891. temp.private_value = val;
  892. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  893. return err;
  894. sprintf(name, "%s Peak Meter", dir[c]);
  895. temp = vx_control_peak_meter;
  896. temp.index = i;
  897. temp.name = name;
  898. temp.private_value = val;
  899. if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
  900. return err;
  901. }
  902. }
  903. vx_reset_audio_levels(chip);
  904. return 0;
  905. }