msnd_pinnacle_mixer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 char *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. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  56. uinfo->count = 1;
  57. uinfo->value.enumerated.items = items;
  58. if (uinfo->value.enumerated.item >= items)
  59. uinfo->value.enumerated.item = items - 1;
  60. strcpy(uinfo->value.enumerated.name,
  61. texts[uinfo->value.enumerated.item]);
  62. return 0;
  63. }
  64. static int snd_msndmix_get_mux(struct snd_kcontrol *kcontrol,
  65. struct snd_ctl_elem_value *ucontrol)
  66. {
  67. struct snd_msnd *chip = snd_kcontrol_chip(kcontrol);
  68. /* MSND_MASK_IMIX is the default */
  69. ucontrol->value.enumerated.item[0] = 0;
  70. if (chip->recsrc & MSND_MASK_SYNTH) {
  71. ucontrol->value.enumerated.item[0] = 1;
  72. } else if ((chip->recsrc & MSND_MASK_DIGITAL) &&
  73. test_bit(F_HAVEDIGITAL, &chip->flags)) {
  74. ucontrol->value.enumerated.item[0] = 2;
  75. }
  76. return 0;
  77. }
  78. static int snd_msndmix_set_mux(struct snd_msnd *chip, int val)
  79. {
  80. unsigned newrecsrc;
  81. int change;
  82. unsigned char msndbyte;
  83. switch (val) {
  84. case 0:
  85. newrecsrc = MSND_MASK_IMIX;
  86. msndbyte = HDEXAR_SET_ANA_IN;
  87. break;
  88. case 1:
  89. newrecsrc = MSND_MASK_SYNTH;
  90. msndbyte = HDEXAR_SET_SYNTH_IN;
  91. break;
  92. case 2:
  93. newrecsrc = MSND_MASK_DIGITAL;
  94. msndbyte = HDEXAR_SET_DAT_IN;
  95. break;
  96. default:
  97. return -EINVAL;
  98. }
  99. change = newrecsrc != chip->recsrc;
  100. if (change) {
  101. change = 0;
  102. if (!snd_msnd_send_word(chip, 0, 0, msndbyte))
  103. if (!snd_msnd_send_dsp_cmd(chip, HDEX_AUX_REQ)) {
  104. chip->recsrc = newrecsrc;
  105. change = 1;
  106. }
  107. }
  108. return change;
  109. }
  110. static int snd_msndmix_put_mux(struct snd_kcontrol *kcontrol,
  111. struct snd_ctl_elem_value *ucontrol)
  112. {
  113. struct snd_msnd *msnd = snd_kcontrol_chip(kcontrol);
  114. return snd_msndmix_set_mux(msnd, ucontrol->value.enumerated.item[0]);
  115. }
  116. static int snd_msndmix_volume_info(struct snd_kcontrol *kcontrol,
  117. struct snd_ctl_elem_info *uinfo)
  118. {
  119. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  120. uinfo->count = 2;
  121. uinfo->value.integer.min = 0;
  122. uinfo->value.integer.max = 100;
  123. return 0;
  124. }
  125. static int snd_msndmix_volume_get(struct snd_kcontrol *kcontrol,
  126. struct snd_ctl_elem_value *ucontrol)
  127. {
  128. struct snd_msnd *msnd = snd_kcontrol_chip(kcontrol);
  129. int addr = kcontrol->private_value;
  130. unsigned long flags;
  131. spin_lock_irqsave(&msnd->mixer_lock, flags);
  132. ucontrol->value.integer.value[0] = msnd->left_levels[addr] * 100;
  133. ucontrol->value.integer.value[0] /= 0xFFFF;
  134. ucontrol->value.integer.value[1] = msnd->right_levels[addr] * 100;
  135. ucontrol->value.integer.value[1] /= 0xFFFF;
  136. spin_unlock_irqrestore(&msnd->mixer_lock, flags);
  137. return 0;
  138. }
  139. #define update_volm(a, b) \
  140. do { \
  141. writew((dev->left_levels[a] >> 1) * \
  142. readw(dev->SMA + SMA_wCurrMastVolLeft) / 0xffff, \
  143. dev->SMA + SMA_##b##Left); \
  144. writew((dev->right_levels[a] >> 1) * \
  145. readw(dev->SMA + SMA_wCurrMastVolRight) / 0xffff, \
  146. dev->SMA + SMA_##b##Right); \
  147. } while (0);
  148. #define update_potm(d, s, ar) \
  149. do { \
  150. writeb((dev->left_levels[d] >> 8) * \
  151. readw(dev->SMA + SMA_wCurrMastVolLeft) / 0xffff, \
  152. dev->SMA + SMA_##s##Left); \
  153. writeb((dev->right_levels[d] >> 8) * \
  154. readw(dev->SMA + SMA_wCurrMastVolRight) / 0xffff, \
  155. dev->SMA + SMA_##s##Right); \
  156. if (snd_msnd_send_word(dev, 0, 0, ar) == 0) \
  157. snd_msnd_send_dsp_cmd(dev, HDEX_AUX_REQ); \
  158. } while (0);
  159. #define update_pot(d, s, ar) \
  160. do { \
  161. writeb(dev->left_levels[d] >> 8, \
  162. dev->SMA + SMA_##s##Left); \
  163. writeb(dev->right_levels[d] >> 8, \
  164. dev->SMA + SMA_##s##Right); \
  165. if (snd_msnd_send_word(dev, 0, 0, ar) == 0) \
  166. snd_msnd_send_dsp_cmd(dev, HDEX_AUX_REQ); \
  167. } while (0);
  168. static int snd_msndmix_set(struct snd_msnd *dev, int d, int left, int right)
  169. {
  170. int bLeft, bRight;
  171. int wLeft, wRight;
  172. int updatemaster = 0;
  173. if (d >= LEVEL_ENTRIES)
  174. return -EINVAL;
  175. bLeft = left * 0xff / 100;
  176. wLeft = left * 0xffff / 100;
  177. bRight = right * 0xff / 100;
  178. wRight = right * 0xffff / 100;
  179. dev->left_levels[d] = wLeft;
  180. dev->right_levels[d] = wRight;
  181. switch (d) {
  182. /* master volume unscaled controls */
  183. case MSND_MIXER_LINE: /* line pot control */
  184. /* scaled by IMIX in digital mix */
  185. writeb(bLeft, dev->SMA + SMA_bInPotPosLeft);
  186. writeb(bRight, dev->SMA + SMA_bInPotPosRight);
  187. if (snd_msnd_send_word(dev, 0, 0, HDEXAR_IN_SET_POTS) == 0)
  188. snd_msnd_send_dsp_cmd(dev, HDEX_AUX_REQ);
  189. break;
  190. case MSND_MIXER_MIC: /* mic pot control */
  191. if (dev->type == msndClassic)
  192. return -EINVAL;
  193. /* scaled by IMIX in digital mix */
  194. writeb(bLeft, dev->SMA + SMA_bMicPotPosLeft);
  195. writeb(bRight, dev->SMA + SMA_bMicPotPosRight);
  196. if (snd_msnd_send_word(dev, 0, 0, HDEXAR_MIC_SET_POTS) == 0)
  197. snd_msnd_send_dsp_cmd(dev, HDEX_AUX_REQ);
  198. break;
  199. case MSND_MIXER_VOLUME: /* master volume */
  200. writew(wLeft, dev->SMA + SMA_wCurrMastVolLeft);
  201. writew(wRight, dev->SMA + SMA_wCurrMastVolRight);
  202. /* fall through */
  203. case MSND_MIXER_AUX: /* aux pot control */
  204. /* scaled by master volume */
  205. /* fall through */
  206. /* digital controls */
  207. case MSND_MIXER_SYNTH: /* synth vol (dsp mix) */
  208. case MSND_MIXER_PCM: /* pcm vol (dsp mix) */
  209. case MSND_MIXER_IMIX: /* input monitor (dsp mix) */
  210. /* scaled by master volume */
  211. updatemaster = 1;
  212. break;
  213. default:
  214. return -EINVAL;
  215. }
  216. if (updatemaster) {
  217. /* update master volume scaled controls */
  218. update_volm(MSND_MIXER_PCM, wCurrPlayVol);
  219. update_volm(MSND_MIXER_IMIX, wCurrInVol);
  220. if (dev->type == msndPinnacle)
  221. update_volm(MSND_MIXER_SYNTH, wCurrMHdrVol);
  222. update_potm(MSND_MIXER_AUX, bAuxPotPos, HDEXAR_AUX_SET_POTS);
  223. }
  224. return 0;
  225. }
  226. static int snd_msndmix_volume_put(struct snd_kcontrol *kcontrol,
  227. struct snd_ctl_elem_value *ucontrol)
  228. {
  229. struct snd_msnd *msnd = snd_kcontrol_chip(kcontrol);
  230. int change, addr = kcontrol->private_value;
  231. int left, right;
  232. unsigned long flags;
  233. left = ucontrol->value.integer.value[0] % 101;
  234. right = ucontrol->value.integer.value[1] % 101;
  235. spin_lock_irqsave(&msnd->mixer_lock, flags);
  236. change = msnd->left_levels[addr] != left
  237. || msnd->right_levels[addr] != right;
  238. snd_msndmix_set(msnd, addr, left, right);
  239. spin_unlock_irqrestore(&msnd->mixer_lock, flags);
  240. return change;
  241. }
  242. #define DUMMY_VOLUME(xname, xindex, addr) \
  243. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  244. .info = snd_msndmix_volume_info, \
  245. .get = snd_msndmix_volume_get, .put = snd_msndmix_volume_put, \
  246. .private_value = addr }
  247. static struct snd_kcontrol_new snd_msnd_controls[] = {
  248. DUMMY_VOLUME("Master Volume", 0, MSND_MIXER_VOLUME),
  249. DUMMY_VOLUME("PCM Volume", 0, MSND_MIXER_PCM),
  250. DUMMY_VOLUME("Aux Volume", 0, MSND_MIXER_AUX),
  251. DUMMY_VOLUME("Line Volume", 0, MSND_MIXER_LINE),
  252. DUMMY_VOLUME("Mic Volume", 0, MSND_MIXER_MIC),
  253. DUMMY_VOLUME("Monitor", 0, MSND_MIXER_IMIX),
  254. {
  255. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  256. .name = "Capture Source",
  257. .info = snd_msndmix_info_mux,
  258. .get = snd_msndmix_get_mux,
  259. .put = snd_msndmix_put_mux,
  260. }
  261. };
  262. int __devinit snd_msndmix_new(struct snd_card *card)
  263. {
  264. struct snd_msnd *chip = card->private_data;
  265. unsigned int idx;
  266. int err;
  267. if (snd_BUG_ON(!chip))
  268. return -EINVAL;
  269. spin_lock_init(&chip->mixer_lock);
  270. strcpy(card->mixername, "MSND Pinnacle Mixer");
  271. for (idx = 0; idx < ARRAY_SIZE(snd_msnd_controls); idx++)
  272. err = snd_ctl_add(card,
  273. snd_ctl_new1(snd_msnd_controls + idx, chip));
  274. if (err < 0)
  275. return err;
  276. return 0;
  277. }
  278. EXPORT_SYMBOL(snd_msndmix_new);
  279. void snd_msndmix_setup(struct snd_msnd *dev)
  280. {
  281. update_pot(MSND_MIXER_LINE, bInPotPos, HDEXAR_IN_SET_POTS);
  282. update_potm(MSND_MIXER_AUX, bAuxPotPos, HDEXAR_AUX_SET_POTS);
  283. update_volm(MSND_MIXER_PCM, wCurrPlayVol);
  284. update_volm(MSND_MIXER_IMIX, wCurrInVol);
  285. if (dev->type == msndPinnacle) {
  286. update_pot(MSND_MIXER_MIC, bMicPotPos, HDEXAR_MIC_SET_POTS);
  287. update_volm(MSND_MIXER_SYNTH, wCurrMHdrVol);
  288. }
  289. }
  290. EXPORT_SYMBOL(snd_msndmix_setup);
  291. int snd_msndmix_force_recsrc(struct snd_msnd *dev, int recsrc)
  292. {
  293. dev->recsrc = -1;
  294. return snd_msndmix_set_mux(dev, recsrc);
  295. }
  296. EXPORT_SYMBOL(snd_msndmix_force_recsrc);