vx_mixer.c 27 KB

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