vx_uer.c 6.8 KB

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