msnd_pinnacle_mixer.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /***************************************************************************
  2. msnd_pinnacle_mixer.c - description
  3. -------------------
  4. begin : Fre Jun 7 2002
  5. copyright : (C) 2002 by karsten wiese
  6. email : annabellesgarden@yahoo.de
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include <linux/io.h>
  17. #include <linux/export.h>
  18. #include <sound/core.h>
  19. #include <sound/control.h>
  20. #include "msnd.h"
  21. #include "msnd_pinnacle.h"
  22. #define MSND_MIXER_VOLUME 0
  23. #define MSND_MIXER_PCM 1
  24. #define MSND_MIXER_AUX 2 /* Input source 1 (aux1) */
  25. #define MSND_MIXER_IMIX 3 /* Recording monitor */
  26. #define MSND_MIXER_SYNTH 4
  27. #define MSND_MIXER_SPEAKER 5
  28. #define MSND_MIXER_LINE 6
  29. #define MSND_MIXER_MIC 7
  30. #define MSND_MIXER_RECLEV 11 /* Recording level */
  31. #define MSND_MIXER_IGAIN 12 /* Input gain */
  32. #define MSND_MIXER_OGAIN 13 /* Output gain */
  33. #define MSND_MIXER_DIGITAL 17 /* Digital (input) 1 */
  34. /* Device mask bits */
  35. #define MSND_MASK_VOLUME (1 << MSND_MIXER_VOLUME)
  36. #define MSND_MASK_SYNTH (1 << MSND_MIXER_SYNTH)
  37. #define MSND_MASK_PCM (1 << MSND_MIXER_PCM)
  38. #define MSND_MASK_SPEAKER (1 << MSND_MIXER_SPEAKER)
  39. #define MSND_MASK_LINE (1 << MSND_MIXER_LINE)
  40. #define MSND_MASK_MIC (1 << MSND_MIXER_MIC)
  41. #define MSND_MASK_IMIX (1 << MSND_MIXER_IMIX)
  42. #define MSND_MASK_RECLEV (1 << MSND_MIXER_RECLEV)
  43. #define MSND_MASK_IGAIN (1 << MSND_MIXER_IGAIN)
  44. #define MSND_MASK_OGAIN (1 << MSND_MIXER_OGAIN)
  45. #define MSND_MASK_AUX (1 << MSND_MIXER_AUX)
  46. #define MSND_MASK_DIGITAL (1 << MSND_MIXER_DIGITAL)
  47. static int snd_msndmix_info_mux(struct snd_kcontrol *kcontrol,
  48. struct snd_ctl_elem_info *uinfo)
  49. {
  50. static const char * const texts[3] = {
  51. "Analog", "MASS", "SPDIF",
  52. };
  53. struct snd_msnd *chip = snd_kcontrol_chip(kcontrol);
  54. unsigned items = test_bit(F_HAVEDIGITAL, &chip->flags) ? 3 : 2;
  55. return snd_ctl_enum_info(uinfo, 1, items, texts);
  56. }
  57. static int snd_msndmix_get_mux(struct snd_kcontrol *kcontrol,
  58. struct snd_ctl_elem_value *ucontrol)
  59. {
  60. struct snd_msnd *chip = snd_kcontrol_chip(kcontrol);
  61. /* MSND_MASK_IMIX is the default */
  62. ucontrol->value.enumerated.item[0] = 0;
  63. if (chip->recsrc & MSND_MASK_SYNTH) {
  64. ucontrol->value.enumerated.item[0] = 1;
  65. } else if ((chip->recsrc & MSND_MASK_DIGITAL) &&
  66. test_bit(F_HAVEDIGITAL, &chip->flags)) {
  67. ucontrol->value.enumerated.item[0] = 2;
  68. }
  69. return 0;
  70. }
  71. static int snd_msndmix_set_mux(struct snd_msnd *chip, int val)
  72. {
  73. unsigned newrecsrc;
  74. int change;
  75. unsigned char msndbyte;
  76. switch (val) {
  77. case 0:
  78. newrecsrc = MSND_MASK_IMIX;
  79. msndbyte = HDEXAR_SET_ANA_IN;
  80. break;
  81. case 1:
  82. newrecsrc = MSND_MASK_SYNTH;
  83. msndbyte = HDEXAR_SET_SYNTH_IN;
  84. break;
  85. case 2:
  86. newrecsrc = MSND_MASK_DIGITAL;
  87. msndbyte = HDEXAR_SET_DAT_IN;
  88. break;
  89. default:
  90. return -EINVAL;
  91. }
  92. change = newrecsrc != chip->recsrc;
  93. if (change) {
  94. change = 0;
  95. if (!snd_msnd_send_word(chip, 0, 0, msndbyte))
  96. if (!snd_msnd_send_dsp_cmd(chip, HDEX_AUX_REQ)) {
  97. chip->recsrc = newrecsrc;
  98. change = 1;
  99. }
  100. }
  101. return change;
  102. }
  103. static int snd_msndmix_put_mux(struct snd_kcontrol *kcontrol,
  104. struct snd_ctl_elem_value *ucontrol)
  105. {
  106. struct snd_msnd *msnd = snd_kcontrol_chip(kcontrol);
  107. return snd_msndmix_set_mux(msnd, ucontrol->value.enumerated.item[0]);
  108. }
  109. static int snd_msndmix_volume_info(struct snd_kcontrol *kcontrol,
  110. struct snd_ctl_elem_info *uinfo)
  111. {
  112. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  113. uinfo->count = 2;
  114. uinfo->value.integer.min = 0;
  115. uinfo->value.integer.max = 100;
  116. return 0;
  117. }
  118. static int snd_msndmix_volume_get(struct snd_kcontrol *kcontrol,
  119. struct snd_ctl_elem_value *ucontrol)
  120. {
  121. struct snd_msnd *msnd = snd_kcontrol_chip(kcontrol);
  122. int addr = kcontrol->private_value;
  123. unsigned long flags;
  124. spin_lock_irqsave(&msnd->mixer_lock, flags);
  125. ucontrol->value.integer.value[0] = msnd->left_levels[addr] * 100;
  126. ucontrol->value.integer.value[0] /= 0xFFFF;
  127. ucontrol->value.integer.value[1] = msnd->right_levels[addr] * 100;
  128. ucontrol->value.integer.value[1] /= 0xFFFF;
  129. spin_unlock_irqrestore(&msnd->mixer_lock, flags);
  130. return 0;
  131. }
  132. #define update_volm(a, b) \
  133. do { \
  134. writew((dev->left_levels[a] >> 1) * \
  135. readw(dev->SMA + SMA_wCurrMastVolLeft) / 0xffff, \
  136. dev->SMA + SMA_##b##Left); \
  137. writew((dev->right_levels[a] >> 1) * \
  138. readw(dev->SMA + SMA_wCurrMastVolRight) / 0xffff, \
  139. dev->SMA + SMA_##b##Right); \
  140. } while (0);
  141. #define update_potm(d, s, ar) \
  142. do { \
  143. writeb((dev->left_levels[d] >> 8) * \
  144. readw(dev->SMA + SMA_wCurrMastVolLeft) / 0xffff, \
  145. dev->SMA + SMA_##s##Left); \
  146. writeb((dev->right_levels[d] >> 8) * \
  147. readw(dev->SMA + SMA_wCurrMastVolRight) / 0xffff, \
  148. dev->SMA + SMA_##s##Right); \
  149. if (snd_msnd_send_word(dev, 0, 0, ar) == 0) \
  150. snd_msnd_send_dsp_cmd(dev, HDEX_AUX_REQ); \
  151. } while (0);
  152. #define update_pot(d, s, ar) \
  153. do { \
  154. writeb(dev->left_levels[d] >> 8, \
  155. dev->SMA + SMA_##s##Left); \
  156. writeb(dev->right_levels[d] >> 8, \
  157. dev->SMA + SMA_##s##Right); \
  158. if (snd_msnd_send_word(dev, 0, 0, ar) == 0) \
  159. snd_msnd_send_dsp_cmd(dev, HDEX_AUX_REQ); \
  160. } while (0);
  161. static int snd_msndmix_set(struct snd_msnd *dev, int d, int left, int right)
  162. {
  163. int bLeft, bRight;
  164. int wLeft, wRight;
  165. int updatemaster = 0;
  166. if (d >= LEVEL_ENTRIES)
  167. return -EINVAL;
  168. bLeft = left * 0xff / 100;
  169. wLeft = left * 0xffff / 100;
  170. bRight = right * 0xff / 100;
  171. wRight = right * 0xffff / 100;
  172. dev->left_levels[d] = wLeft;
  173. dev->right_levels[d] = wRight;
  174. switch (d) {
  175. /* master volume unscaled controls */
  176. case MSND_MIXER_LINE: /* line pot control */
  177. /* scaled by IMIX in digital mix */
  178. writeb(bLeft, dev->SMA + SMA_bInPotPosLeft);
  179. writeb(bRight, dev->SMA + SMA_bInPotPosRight);
  180. if (snd_msnd_send_word(dev, 0, 0, HDEXAR_IN_SET_POTS) == 0)
  181. snd_msnd_send_dsp_cmd(dev, HDEX_AUX_REQ);
  182. break;
  183. case MSND_MIXER_MIC: /* mic pot control */
  184. if (dev->type == msndClassic)
  185. return -EINVAL;
  186. /* scaled by IMIX in digital mix */
  187. writeb(bLeft, dev->SMA + SMA_bMicPotPosLeft);
  188. writeb(bRight, dev->SMA + SMA_bMicPotPosRight);
  189. if (snd_msnd_send_word(dev, 0, 0, HDEXAR_MIC_SET_POTS) == 0)
  190. snd_msnd_send_dsp_cmd(dev, HDEX_AUX_REQ);
  191. break;
  192. case MSND_MIXER_VOLUME: /* master volume */
  193. writew(wLeft, dev->SMA + SMA_wCurrMastVolLeft);
  194. writew(wRight, dev->SMA + SMA_wCurrMastVolRight);
  195. /* fall through */
  196. case MSND_MIXER_AUX: /* aux pot control */
  197. /* scaled by master volume */
  198. /* fall through */
  199. /* digital controls */
  200. case MSND_MIXER_SYNTH: /* synth vol (dsp mix) */
  201. case MSND_MIXER_PCM: /* pcm vol (dsp mix) */
  202. case MSND_MIXER_IMIX: /* input monitor (dsp mix) */
  203. /* scaled by master volume */
  204. updatemaster = 1;
  205. break;
  206. default:
  207. return -EINVAL;
  208. }
  209. if (updatemaster) {
  210. /* update master volume scaled controls */
  211. update_volm(MSND_MIXER_PCM, wCurrPlayVol);
  212. update_volm(MSND_MIXER_IMIX, wCurrInVol);
  213. if (dev->type == msndPinnacle)
  214. update_volm(MSND_MIXER_SYNTH, wCurrMHdrVol);
  215. update_potm(MSND_MIXER_AUX, bAuxPotPos, HDEXAR_AUX_SET_POTS);
  216. }
  217. return 0;
  218. }
  219. static int snd_msndmix_volume_put(struct snd_kcontrol *kcontrol,
  220. struct snd_ctl_elem_value *ucontrol)
  221. {
  222. struct snd_msnd *msnd = snd_kcontrol_chip(kcontrol);
  223. int change, addr = kcontrol->private_value;
  224. int left, right;
  225. unsigned long flags;
  226. left = ucontrol->value.integer.value[0] % 101;
  227. right = ucontrol->value.integer.value[1] % 101;
  228. spin_lock_irqsave(&msnd->mixer_lock, flags);
  229. change = msnd->left_levels[addr] != left
  230. || msnd->right_levels[addr] != right;
  231. snd_msndmix_set(msnd, addr, left, right);
  232. spin_unlock_irqrestore(&msnd->mixer_lock, flags);
  233. return change;
  234. }
  235. #define DUMMY_VOLUME(xname, xindex, addr) \
  236. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  237. .info = snd_msndmix_volume_info, \
  238. .get = snd_msndmix_volume_get, .put = snd_msndmix_volume_put, \
  239. .private_value = addr }
  240. static struct snd_kcontrol_new snd_msnd_controls[] = {
  241. DUMMY_VOLUME("Master Volume", 0, MSND_MIXER_VOLUME),
  242. DUMMY_VOLUME("PCM Volume", 0, MSND_MIXER_PCM),
  243. DUMMY_VOLUME("Aux Volume", 0, MSND_MIXER_AUX),
  244. DUMMY_VOLUME("Line Volume", 0, MSND_MIXER_LINE),
  245. DUMMY_VOLUME("Mic Volume", 0, MSND_MIXER_MIC),
  246. DUMMY_VOLUME("Monitor", 0, MSND_MIXER_IMIX),
  247. {
  248. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  249. .name = "Capture Source",
  250. .info = snd_msndmix_info_mux,
  251. .get = snd_msndmix_get_mux,
  252. .put = snd_msndmix_put_mux,
  253. }
  254. };
  255. int snd_msndmix_new(struct snd_card *card)
  256. {
  257. struct snd_msnd *chip = card->private_data;
  258. unsigned int idx;
  259. int err;
  260. if (snd_BUG_ON(!chip))
  261. return -EINVAL;
  262. spin_lock_init(&chip->mixer_lock);
  263. strcpy(card->mixername, "MSND Pinnacle Mixer");
  264. for (idx = 0; idx < ARRAY_SIZE(snd_msnd_controls); idx++) {
  265. err = snd_ctl_add(card,
  266. snd_ctl_new1(snd_msnd_controls + idx, chip));
  267. if (err < 0)
  268. return err;
  269. }
  270. return 0;
  271. }
  272. EXPORT_SYMBOL(snd_msndmix_new);
  273. void snd_msndmix_setup(struct snd_msnd *dev)
  274. {
  275. update_pot(MSND_MIXER_LINE, bInPotPos, HDEXAR_IN_SET_POTS);
  276. update_potm(MSND_MIXER_AUX, bAuxPotPos, HDEXAR_AUX_SET_POTS);
  277. update_volm(MSND_MIXER_PCM, wCurrPlayVol);
  278. update_volm(MSND_MIXER_IMIX, wCurrInVol);
  279. if (dev->type == msndPinnacle) {
  280. update_pot(MSND_MIXER_MIC, bMicPotPos, HDEXAR_MIC_SET_POTS);
  281. update_volm(MSND_MIXER_SYNTH, wCurrMHdrVol);
  282. }
  283. }
  284. EXPORT_SYMBOL(snd_msndmix_setup);
  285. int snd_msndmix_force_recsrc(struct snd_msnd *dev, int recsrc)
  286. {
  287. dev->recsrc = -1;
  288. return snd_msndmix_set_mux(dev, recsrc);
  289. }
  290. EXPORT_SYMBOL(snd_msndmix_force_recsrc);