vx_mixer.c 29 KB

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