vx_mixer.c 28 KB

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