vx_uer.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 <sound/driver.h>
  23. #include <linux/delay.h>
  24. #include <sound/core.h>
  25. #include <sound/vx_core.h>
  26. #include "vx_cmd.h"
  27. /*
  28. * vx_modify_board_clock - tell the board that its clock has been modified
  29. * @sync: DSP needs to resynchronize its FIFO
  30. */
  31. static int vx_modify_board_clock(vx_core_t *chip, int sync)
  32. {
  33. struct vx_rmh rmh;
  34. vx_init_rmh(&rmh, CMD_MODIFY_CLOCK);
  35. /* Ask the DSP to resynchronize its FIFO. */
  36. if (sync)
  37. rmh.Cmd[0] |= CMD_MODIFY_CLOCK_S_BIT;
  38. return vx_send_msg(chip, &rmh);
  39. }
  40. /*
  41. * vx_modify_board_inputs - resync audio inputs
  42. */
  43. static int vx_modify_board_inputs(vx_core_t *chip)
  44. {
  45. struct vx_rmh rmh;
  46. vx_init_rmh(&rmh, CMD_RESYNC_AUDIO_INPUTS);
  47. rmh.Cmd[0] |= 1 << 0; /* reference: AUDIO 0 */
  48. return vx_send_msg(chip, &rmh);
  49. }
  50. /*
  51. * vx_read_one_cbit - read one bit from UER config
  52. * @index: the bit index
  53. * returns 0 or 1.
  54. */
  55. static int vx_read_one_cbit(vx_core_t *chip, int index)
  56. {
  57. unsigned long flags;
  58. int val;
  59. spin_lock_irqsave(&chip->lock, flags);
  60. if (chip->type >= VX_TYPE_VXPOCKET) {
  61. vx_outb(chip, CSUER, 1); /* read */
  62. vx_outb(chip, RUER, index & XX_UER_CBITS_OFFSET_MASK);
  63. val = (vx_inb(chip, RUER) >> 7) & 0x01;
  64. } else {
  65. vx_outl(chip, CSUER, 1); /* read */
  66. vx_outl(chip, RUER, index & XX_UER_CBITS_OFFSET_MASK);
  67. val = (vx_inl(chip, RUER) >> 7) & 0x01;
  68. }
  69. spin_unlock_irqrestore(&chip->lock, flags);
  70. return val;
  71. }
  72. /*
  73. * vx_write_one_cbit - write one bit to UER config
  74. * @index: the bit index
  75. * @val: bit value, 0 or 1
  76. */
  77. static void vx_write_one_cbit(vx_core_t *chip, int index, int val)
  78. {
  79. unsigned long flags;
  80. val = !!val; /* 0 or 1 */
  81. spin_lock_irqsave(&chip->lock, flags);
  82. if (vx_is_pcmcia(chip)) {
  83. vx_outb(chip, CSUER, 0); /* write */
  84. vx_outb(chip, RUER, (val << 7) | (index & XX_UER_CBITS_OFFSET_MASK));
  85. } else {
  86. vx_outl(chip, CSUER, 0); /* write */
  87. vx_outl(chip, RUER, (val << 7) | (index & XX_UER_CBITS_OFFSET_MASK));
  88. }
  89. spin_unlock_irqrestore(&chip->lock, flags);
  90. }
  91. /*
  92. * vx_read_uer_status - read the current UER status
  93. * @mode: pointer to store the UER mode, VX_UER_MODE_XXX
  94. *
  95. * returns the frequency of UER, or 0 if not sync,
  96. * or a negative error code.
  97. */
  98. static int vx_read_uer_status(vx_core_t *chip, int *mode)
  99. {
  100. int val, freq;
  101. /* Default values */
  102. freq = 0;
  103. /* Read UER status */
  104. if (vx_is_pcmcia(chip))
  105. val = vx_inb(chip, CSUER);
  106. else
  107. val = vx_inl(chip, CSUER);
  108. if (val < 0)
  109. return val;
  110. /* If clock is present, read frequency */
  111. if (val & VX_SUER_CLOCK_PRESENT_MASK) {
  112. switch (val & VX_SUER_FREQ_MASK) {
  113. case VX_SUER_FREQ_32KHz_MASK:
  114. freq = 32000;
  115. break;
  116. case VX_SUER_FREQ_44KHz_MASK:
  117. freq = 44100;
  118. break;
  119. case VX_SUER_FREQ_48KHz_MASK:
  120. freq = 48000;
  121. break;
  122. }
  123. }
  124. if (val & VX_SUER_DATA_PRESENT_MASK)
  125. /* bit 0 corresponds to consumer/professional bit */
  126. *mode = vx_read_one_cbit(chip, 0) ?
  127. VX_UER_MODE_PROFESSIONAL : VX_UER_MODE_CONSUMER;
  128. else
  129. *mode = VX_UER_MODE_NOT_PRESENT;
  130. return freq;
  131. }
  132. /*
  133. * compute the sample clock value from frequency
  134. *
  135. * The formula is as follows:
  136. *
  137. * HexFreq = (dword) ((double) ((double) 28224000 / (double) Frequency))
  138. * switch ( HexFreq & 0x00000F00 )
  139. * case 0x00000100: ;
  140. * case 0x00000200:
  141. * case 0x00000300: HexFreq -= 0x00000201 ;
  142. * case 0x00000400:
  143. * case 0x00000500:
  144. * case 0x00000600:
  145. * case 0x00000700: HexFreq = (dword) (((double) 28224000 / (double) (Frequency*2)) - 1)
  146. * default : HexFreq = (dword) ((double) 28224000 / (double) (Frequency*4)) - 0x000001FF
  147. */
  148. static int vx_calc_clock_from_freq(vx_core_t *chip, int freq)
  149. {
  150. #define XX_FECH48000 0x0000004B
  151. #define XX_FECH32000 0x00000171
  152. #define XX_FECH24000 0x0000024B
  153. #define XX_FECH16000 0x00000371
  154. #define XX_FECH12000 0x0000044B
  155. #define XX_FECH8000 0x00000571
  156. #define XX_FECH44100 0x0000007F
  157. #define XX_FECH29400 0x0000016F
  158. #define XX_FECH22050 0x0000027F
  159. #define XX_FECH14000 0x000003EF
  160. #define XX_FECH11025 0x0000047F
  161. #define XX_FECH7350 0x000005BF
  162. switch (freq) {
  163. case 48000: return XX_FECH48000;
  164. case 44100: return XX_FECH44100;
  165. case 32000: return XX_FECH32000;
  166. case 29400: return XX_FECH29400;
  167. case 24000: return XX_FECH24000;
  168. case 22050: return XX_FECH22050;
  169. case 16000: return XX_FECH16000;
  170. case 14000: return XX_FECH14000;
  171. case 12000: return XX_FECH12000;
  172. case 11025: return XX_FECH11025;
  173. case 8000: return XX_FECH8000;
  174. case 7350: return XX_FECH7350;
  175. default: return freq; /* The value is already correct */
  176. }
  177. }
  178. /*
  179. * vx_change_clock_source - change the clock source
  180. * @source: the new source
  181. */
  182. static void vx_change_clock_source(vx_core_t *chip, int source)
  183. {
  184. unsigned long flags;
  185. /* we mute DAC to prevent clicks */
  186. vx_toggle_dac_mute(chip, 1);
  187. spin_lock_irqsave(&chip->lock, flags);
  188. chip->ops->set_clock_source(chip, source);
  189. chip->clock_source = source;
  190. spin_unlock_irqrestore(&chip->lock, flags);
  191. /* unmute */
  192. vx_toggle_dac_mute(chip, 0);
  193. }
  194. /*
  195. * set the internal clock
  196. */
  197. void vx_set_internal_clock(vx_core_t *chip, unsigned int freq)
  198. {
  199. int clock;
  200. unsigned long flags;
  201. /* Get real clock value */
  202. clock = vx_calc_clock_from_freq(chip, freq);
  203. snd_printdd(KERN_DEBUG "set internal clock to 0x%x from freq %d\n", clock, freq);
  204. spin_lock_irqsave(&chip->lock, flags);
  205. if (vx_is_pcmcia(chip)) {
  206. vx_outb(chip, HIFREQ, (clock >> 8) & 0x0f);
  207. vx_outb(chip, LOFREQ, clock & 0xff);
  208. } else {
  209. vx_outl(chip, HIFREQ, (clock >> 8) & 0x0f);
  210. vx_outl(chip, LOFREQ, clock & 0xff);
  211. }
  212. spin_unlock_irqrestore(&chip->lock, flags);
  213. }
  214. /*
  215. * set the iec958 status bits
  216. * @bits: 32-bit status bits
  217. */
  218. void vx_set_iec958_status(vx_core_t *chip, unsigned int bits)
  219. {
  220. int i;
  221. if (chip->chip_status & VX_STAT_IS_STALE)
  222. return;
  223. for (i = 0; i < 32; i++)
  224. vx_write_one_cbit(chip, i, bits & (1 << i));
  225. }
  226. /*
  227. * vx_set_clock - change the clock and audio source if necessary
  228. */
  229. int vx_set_clock(vx_core_t *chip, unsigned int freq)
  230. {
  231. int src_changed = 0;
  232. if (chip->chip_status & VX_STAT_IS_STALE)
  233. return 0;
  234. /* change the audio source if possible */
  235. vx_sync_audio_source(chip);
  236. if (chip->clock_mode == VX_CLOCK_MODE_EXTERNAL ||
  237. (chip->clock_mode == VX_CLOCK_MODE_AUTO &&
  238. chip->audio_source == VX_AUDIO_SRC_DIGITAL)) {
  239. if (chip->clock_source != UER_SYNC) {
  240. vx_change_clock_source(chip, UER_SYNC);
  241. mdelay(6);
  242. src_changed = 1;
  243. }
  244. } else if (chip->clock_mode == VX_CLOCK_MODE_INTERNAL ||
  245. (chip->clock_mode == VX_CLOCK_MODE_AUTO &&
  246. chip->audio_source != VX_AUDIO_SRC_DIGITAL)) {
  247. if (chip->clock_source != INTERNAL_QUARTZ) {
  248. vx_change_clock_source(chip, INTERNAL_QUARTZ);
  249. src_changed = 1;
  250. }
  251. if (chip->freq == freq)
  252. return 0;
  253. vx_set_internal_clock(chip, freq);
  254. if (src_changed)
  255. vx_modify_board_inputs(chip);
  256. }
  257. if (chip->freq == freq)
  258. return 0;
  259. chip->freq = freq;
  260. vx_modify_board_clock(chip, 1);
  261. return 0;
  262. }
  263. /*
  264. * vx_change_frequency - called from interrupt handler
  265. */
  266. int vx_change_frequency(vx_core_t *chip)
  267. {
  268. int freq;
  269. if (chip->chip_status & VX_STAT_IS_STALE)
  270. return 0;
  271. if (chip->clock_source == INTERNAL_QUARTZ)
  272. return 0;
  273. /*
  274. * Read the real UER board frequency
  275. */
  276. freq = vx_read_uer_status(chip, &chip->uer_detected);
  277. if (freq < 0)
  278. return freq;
  279. /*
  280. * The frequency computed by the DSP is good and
  281. * is different from the previous computed.
  282. */
  283. if (freq == 48000 || freq == 44100 || freq == 32000)
  284. chip->freq_detected = freq;
  285. return 0;
  286. }