vx_uer.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Driver for Digigram VX soundcards
  3. *
  4. * IEC958 stuff
  5. *
  6. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/delay.h>
  23. #include <sound/core.h>
  24. #include <sound/vx_core.h>
  25. #include "vx_cmd.h"
  26. /*
  27. * vx_modify_board_clock - tell the board that its clock has been modified
  28. * @sync: DSP needs to resynchronize its FIFO
  29. */
  30. static int vx_modify_board_clock(struct vx_core *chip, int sync)
  31. {
  32. struct vx_rmh rmh;
  33. vx_init_rmh(&rmh, CMD_MODIFY_CLOCK);
  34. /* Ask the DSP to resynchronize its FIFO. */
  35. if (sync)
  36. rmh.Cmd[0] |= CMD_MODIFY_CLOCK_S_BIT;
  37. return vx_send_msg(chip, &rmh);
  38. }
  39. /*
  40. * vx_modify_board_inputs - resync audio inputs
  41. */
  42. static int vx_modify_board_inputs(struct vx_core *chip)
  43. {
  44. struct vx_rmh rmh;
  45. vx_init_rmh(&rmh, CMD_RESYNC_AUDIO_INPUTS);
  46. rmh.Cmd[0] |= 1 << 0; /* reference: AUDIO 0 */
  47. return vx_send_msg(chip, &rmh);
  48. }
  49. /*
  50. * vx_read_one_cbit - read one bit from UER config
  51. * @index: the bit index
  52. * returns 0 or 1.
  53. */
  54. static int vx_read_one_cbit(struct vx_core *chip, int index)
  55. {
  56. int val;
  57. mutex_lock(&chip->lock);
  58. if (chip->type >= VX_TYPE_VXPOCKET) {
  59. vx_outb(chip, CSUER, 1); /* read */
  60. vx_outb(chip, RUER, index & XX_UER_CBITS_OFFSET_MASK);
  61. val = (vx_inb(chip, RUER) >> 7) & 0x01;
  62. } else {
  63. vx_outl(chip, CSUER, 1); /* read */
  64. vx_outl(chip, RUER, index & XX_UER_CBITS_OFFSET_MASK);
  65. val = (vx_inl(chip, RUER) >> 7) & 0x01;
  66. }
  67. mutex_unlock(&chip->lock);
  68. return val;
  69. }
  70. /*
  71. * vx_write_one_cbit - write one bit to UER config
  72. * @index: the bit index
  73. * @val: bit value, 0 or 1
  74. */
  75. static void vx_write_one_cbit(struct vx_core *chip, int index, int val)
  76. {
  77. val = !!val; /* 0 or 1 */
  78. mutex_lock(&chip->lock);
  79. if (vx_is_pcmcia(chip)) {
  80. vx_outb(chip, CSUER, 0); /* write */
  81. vx_outb(chip, RUER, (val << 7) | (index & XX_UER_CBITS_OFFSET_MASK));
  82. } else {
  83. vx_outl(chip, CSUER, 0); /* write */
  84. vx_outl(chip, RUER, (val << 7) | (index & XX_UER_CBITS_OFFSET_MASK));
  85. }
  86. mutex_unlock(&chip->lock);
  87. }
  88. /*
  89. * vx_read_uer_status - read the current UER status
  90. * @mode: pointer to store the UER mode, VX_UER_MODE_XXX
  91. *
  92. * returns the frequency of UER, or 0 if not sync,
  93. * or a negative error code.
  94. */
  95. static int vx_read_uer_status(struct vx_core *chip, unsigned int *mode)
  96. {
  97. int val, freq;
  98. /* Default values */
  99. freq = 0;
  100. /* Read UER status */
  101. if (vx_is_pcmcia(chip))
  102. val = vx_inb(chip, CSUER);
  103. else
  104. val = vx_inl(chip, CSUER);
  105. if (val < 0)
  106. return val;
  107. /* If clock is present, read frequency */
  108. if (val & VX_SUER_CLOCK_PRESENT_MASK) {
  109. switch (val & VX_SUER_FREQ_MASK) {
  110. case VX_SUER_FREQ_32KHz_MASK:
  111. freq = 32000;
  112. break;
  113. case VX_SUER_FREQ_44KHz_MASK:
  114. freq = 44100;
  115. break;
  116. case VX_SUER_FREQ_48KHz_MASK:
  117. freq = 48000;
  118. break;
  119. }
  120. }
  121. if (val & VX_SUER_DATA_PRESENT_MASK)
  122. /* bit 0 corresponds to consumer/professional bit */
  123. *mode = vx_read_one_cbit(chip, 0) ?
  124. VX_UER_MODE_PROFESSIONAL : VX_UER_MODE_CONSUMER;
  125. else
  126. *mode = VX_UER_MODE_NOT_PRESENT;
  127. return freq;
  128. }
  129. /*
  130. * compute the sample clock value from frequency
  131. *
  132. * The formula is as follows:
  133. *
  134. * HexFreq = (dword) ((double) ((double) 28224000 / (double) Frequency))
  135. * switch ( HexFreq & 0x00000F00 )
  136. * case 0x00000100: ;
  137. * case 0x00000200:
  138. * case 0x00000300: HexFreq -= 0x00000201 ;
  139. * case 0x00000400:
  140. * case 0x00000500:
  141. * case 0x00000600:
  142. * case 0x00000700: HexFreq = (dword) (((double) 28224000 / (double) (Frequency*2)) - 1)
  143. * default : HexFreq = (dword) ((double) 28224000 / (double) (Frequency*4)) - 0x000001FF
  144. */
  145. static int vx_calc_clock_from_freq(struct vx_core *chip, int freq)
  146. {
  147. int hexfreq;
  148. if (snd_BUG_ON(freq <= 0))
  149. return 0;
  150. hexfreq = (28224000 * 10) / freq;
  151. hexfreq = (hexfreq + 5) / 10;
  152. /* max freq = 55125 Hz */
  153. if (snd_BUG_ON(hexfreq <= 0x00000200))
  154. return 0;
  155. if (hexfreq <= 0x03ff)
  156. return hexfreq - 0x00000201;
  157. if (hexfreq <= 0x07ff)
  158. return (hexfreq / 2) - 1;
  159. if (hexfreq <= 0x0fff)
  160. return (hexfreq / 4) + 0x000001ff;
  161. return 0x5fe; /* min freq = 6893 Hz */
  162. }
  163. /*
  164. * vx_change_clock_source - change the clock source
  165. * @source: the new source
  166. */
  167. static void vx_change_clock_source(struct vx_core *chip, int source)
  168. {
  169. /* we mute DAC to prevent clicks */
  170. vx_toggle_dac_mute(chip, 1);
  171. mutex_lock(&chip->lock);
  172. chip->ops->set_clock_source(chip, source);
  173. chip->clock_source = source;
  174. mutex_unlock(&chip->lock);
  175. /* unmute */
  176. vx_toggle_dac_mute(chip, 0);
  177. }
  178. /*
  179. * set the internal clock
  180. */
  181. void vx_set_internal_clock(struct vx_core *chip, unsigned int freq)
  182. {
  183. int clock;
  184. /* Get real clock value */
  185. clock = vx_calc_clock_from_freq(chip, freq);
  186. snd_printdd(KERN_DEBUG "set internal clock to 0x%x from freq %d\n", clock, freq);
  187. mutex_lock(&chip->lock);
  188. if (vx_is_pcmcia(chip)) {
  189. vx_outb(chip, HIFREQ, (clock >> 8) & 0x0f);
  190. vx_outb(chip, LOFREQ, clock & 0xff);
  191. } else {
  192. vx_outl(chip, HIFREQ, (clock >> 8) & 0x0f);
  193. vx_outl(chip, LOFREQ, clock & 0xff);
  194. }
  195. mutex_unlock(&chip->lock);
  196. }
  197. /*
  198. * set the iec958 status bits
  199. * @bits: 32-bit status bits
  200. */
  201. void vx_set_iec958_status(struct vx_core *chip, unsigned int bits)
  202. {
  203. int i;
  204. if (chip->chip_status & VX_STAT_IS_STALE)
  205. return;
  206. for (i = 0; i < 32; i++)
  207. vx_write_one_cbit(chip, i, bits & (1 << i));
  208. }
  209. /*
  210. * vx_set_clock - change the clock and audio source if necessary
  211. */
  212. int vx_set_clock(struct vx_core *chip, unsigned int freq)
  213. {
  214. int src_changed = 0;
  215. if (chip->chip_status & VX_STAT_IS_STALE)
  216. return 0;
  217. /* change the audio source if possible */
  218. vx_sync_audio_source(chip);
  219. if (chip->clock_mode == VX_CLOCK_MODE_EXTERNAL ||
  220. (chip->clock_mode == VX_CLOCK_MODE_AUTO &&
  221. chip->audio_source == VX_AUDIO_SRC_DIGITAL)) {
  222. if (chip->clock_source != UER_SYNC) {
  223. vx_change_clock_source(chip, UER_SYNC);
  224. mdelay(6);
  225. src_changed = 1;
  226. }
  227. } else if (chip->clock_mode == VX_CLOCK_MODE_INTERNAL ||
  228. (chip->clock_mode == VX_CLOCK_MODE_AUTO &&
  229. chip->audio_source != VX_AUDIO_SRC_DIGITAL)) {
  230. if (chip->clock_source != INTERNAL_QUARTZ) {
  231. vx_change_clock_source(chip, INTERNAL_QUARTZ);
  232. src_changed = 1;
  233. }
  234. if (chip->freq == freq)
  235. return 0;
  236. vx_set_internal_clock(chip, freq);
  237. if (src_changed)
  238. vx_modify_board_inputs(chip);
  239. }
  240. if (chip->freq == freq)
  241. return 0;
  242. chip->freq = freq;
  243. vx_modify_board_clock(chip, 1);
  244. return 0;
  245. }
  246. /*
  247. * vx_change_frequency - called from interrupt handler
  248. */
  249. int vx_change_frequency(struct vx_core *chip)
  250. {
  251. int freq;
  252. if (chip->chip_status & VX_STAT_IS_STALE)
  253. return 0;
  254. if (chip->clock_source == INTERNAL_QUARTZ)
  255. return 0;
  256. /*
  257. * Read the real UER board frequency
  258. */
  259. freq = vx_read_uer_status(chip, &chip->uer_detected);
  260. if (freq < 0)
  261. return freq;
  262. /*
  263. * The frequency computed by the DSP is good and
  264. * is different from the previous computed.
  265. */
  266. if (freq == 48000 || freq == 44100 || freq == 32000)
  267. chip->freq_detected = freq;
  268. return 0;
  269. }