vx_mixer.c 29 KB

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