vx_uer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. guard(mutex)(&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. return val;
  55. }
  56. /*
  57. * vx_write_one_cbit - write one bit to UER config
  58. * @index: the bit index
  59. * @val: bit value, 0 or 1
  60. */
  61. static void vx_write_one_cbit(struct vx_core *chip, int index, int val)
  62. {
  63. val = !!val; /* 0 or 1 */
  64. guard(mutex)(&chip->lock);
  65. if (vx_is_pcmcia(chip)) {
  66. vx_outb(chip, CSUER, 0); /* write */
  67. vx_outb(chip, RUER, (val << 7) | (index & XX_UER_CBITS_OFFSET_MASK));
  68. } else {
  69. vx_outl(chip, CSUER, 0); /* write */
  70. vx_outl(chip, RUER, (val << 7) | (index & XX_UER_CBITS_OFFSET_MASK));
  71. }
  72. }
  73. /*
  74. * vx_read_uer_status - read the current UER status
  75. * @mode: pointer to store the UER mode, VX_UER_MODE_XXX
  76. *
  77. * returns the frequency of UER, or 0 if not sync,
  78. * or a negative error code.
  79. */
  80. static int vx_read_uer_status(struct vx_core *chip, unsigned int *mode)
  81. {
  82. int val, freq;
  83. /* Default values */
  84. freq = 0;
  85. /* Read UER status */
  86. if (vx_is_pcmcia(chip))
  87. val = vx_inb(chip, CSUER);
  88. else
  89. val = vx_inl(chip, CSUER);
  90. if (val < 0)
  91. return val;
  92. /* If clock is present, read frequency */
  93. if (val & VX_SUER_CLOCK_PRESENT_MASK) {
  94. switch (val & VX_SUER_FREQ_MASK) {
  95. case VX_SUER_FREQ_32KHz_MASK:
  96. freq = 32000;
  97. break;
  98. case VX_SUER_FREQ_44KHz_MASK:
  99. freq = 44100;
  100. break;
  101. case VX_SUER_FREQ_48KHz_MASK:
  102. freq = 48000;
  103. break;
  104. }
  105. }
  106. if (val & VX_SUER_DATA_PRESENT_MASK)
  107. /* bit 0 corresponds to consumer/professional bit */
  108. *mode = vx_read_one_cbit(chip, 0) ?
  109. VX_UER_MODE_PROFESSIONAL : VX_UER_MODE_CONSUMER;
  110. else
  111. *mode = VX_UER_MODE_NOT_PRESENT;
  112. return freq;
  113. }
  114. /*
  115. * compute the sample clock value from frequency
  116. *
  117. * The formula is as follows:
  118. *
  119. * HexFreq = (dword) ((double) ((double) 28224000 / (double) Frequency))
  120. * switch ( HexFreq & 0x00000F00 )
  121. * case 0x00000100: ;
  122. * case 0x00000200:
  123. * case 0x00000300: HexFreq -= 0x00000201 ;
  124. * case 0x00000400:
  125. * case 0x00000500:
  126. * case 0x00000600:
  127. * case 0x00000700: HexFreq = (dword) (((double) 28224000 / (double) (Frequency*2)) - 1)
  128. * default : HexFreq = (dword) ((double) 28224000 / (double) (Frequency*4)) - 0x000001FF
  129. */
  130. static int vx_calc_clock_from_freq(struct vx_core *chip, int freq)
  131. {
  132. int hexfreq;
  133. if (snd_BUG_ON(freq <= 0))
  134. return 0;
  135. hexfreq = (28224000 * 10) / freq;
  136. hexfreq = (hexfreq + 5) / 10;
  137. /* max freq = 55125 Hz */
  138. if (snd_BUG_ON(hexfreq <= 0x00000200))
  139. return 0;
  140. if (hexfreq <= 0x03ff)
  141. return hexfreq - 0x00000201;
  142. if (hexfreq <= 0x07ff)
  143. return (hexfreq / 2) - 1;
  144. if (hexfreq <= 0x0fff)
  145. return (hexfreq / 4) + 0x000001ff;
  146. return 0x5fe; /* min freq = 6893 Hz */
  147. }
  148. /*
  149. * vx_change_clock_source - change the clock source
  150. * @source: the new source
  151. */
  152. static void vx_change_clock_source(struct vx_core *chip, int source)
  153. {
  154. /* we mute DAC to prevent clicks */
  155. vx_toggle_dac_mute(chip, 1);
  156. scoped_guard(mutex, &chip->lock) {
  157. chip->ops->set_clock_source(chip, source);
  158. chip->clock_source = source;
  159. }
  160. /* unmute */
  161. vx_toggle_dac_mute(chip, 0);
  162. }
  163. /*
  164. * set the internal clock
  165. */
  166. void vx_set_internal_clock(struct vx_core *chip, unsigned int freq)
  167. {
  168. int clock;
  169. /* Get real clock value */
  170. clock = vx_calc_clock_from_freq(chip, freq);
  171. dev_dbg(chip->card->dev,
  172. "set internal clock to 0x%x from freq %d\n", clock, freq);
  173. guard(mutex)(&chip->lock);
  174. if (vx_is_pcmcia(chip)) {
  175. vx_outb(chip, HIFREQ, (clock >> 8) & 0x0f);
  176. vx_outb(chip, LOFREQ, clock & 0xff);
  177. } else {
  178. vx_outl(chip, HIFREQ, (clock >> 8) & 0x0f);
  179. vx_outl(chip, LOFREQ, clock & 0xff);
  180. }
  181. }
  182. /*
  183. * set the iec958 status bits
  184. * @bits: 32-bit status bits
  185. */
  186. void vx_set_iec958_status(struct vx_core *chip, unsigned int bits)
  187. {
  188. int i;
  189. if (chip->chip_status & VX_STAT_IS_STALE)
  190. return;
  191. for (i = 0; i < 32; i++)
  192. vx_write_one_cbit(chip, i, bits & (1 << i));
  193. }
  194. /*
  195. * vx_set_clock - change the clock and audio source if necessary
  196. */
  197. int vx_set_clock(struct vx_core *chip, unsigned int freq)
  198. {
  199. int src_changed = 0;
  200. if (chip->chip_status & VX_STAT_IS_STALE)
  201. return 0;
  202. /* change the audio source if possible */
  203. vx_sync_audio_source(chip);
  204. if (chip->clock_mode == VX_CLOCK_MODE_EXTERNAL ||
  205. (chip->clock_mode == VX_CLOCK_MODE_AUTO &&
  206. chip->audio_source == VX_AUDIO_SRC_DIGITAL)) {
  207. if (chip->clock_source != UER_SYNC) {
  208. vx_change_clock_source(chip, UER_SYNC);
  209. mdelay(6);
  210. src_changed = 1;
  211. }
  212. } else if (chip->clock_mode == VX_CLOCK_MODE_INTERNAL ||
  213. (chip->clock_mode == VX_CLOCK_MODE_AUTO &&
  214. chip->audio_source != VX_AUDIO_SRC_DIGITAL)) {
  215. if (chip->clock_source != INTERNAL_QUARTZ) {
  216. vx_change_clock_source(chip, INTERNAL_QUARTZ);
  217. src_changed = 1;
  218. }
  219. if (chip->freq == freq)
  220. return 0;
  221. vx_set_internal_clock(chip, freq);
  222. if (src_changed)
  223. vx_modify_board_inputs(chip);
  224. }
  225. if (chip->freq == freq)
  226. return 0;
  227. chip->freq = freq;
  228. vx_modify_board_clock(chip, 1);
  229. return 0;
  230. }
  231. /*
  232. * vx_change_frequency - called from interrupt handler
  233. */
  234. int vx_change_frequency(struct vx_core *chip)
  235. {
  236. int freq;
  237. if (chip->chip_status & VX_STAT_IS_STALE)
  238. return 0;
  239. if (chip->clock_source == INTERNAL_QUARTZ)
  240. return 0;
  241. /*
  242. * Read the real UER board frequency
  243. */
  244. freq = vx_read_uer_status(chip, &chip->uer_detected);
  245. if (freq < 0)
  246. return freq;
  247. /*
  248. * The frequency computed by the DSP is good and
  249. * is different from the previous computed.
  250. */
  251. if (freq == 48000 || freq == 44100 || freq == 32000)
  252. chip->freq_detected = freq;
  253. return 0;
  254. }