vx_mixer.c 28 KB

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