vx_mixer.c 28 KB

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