cs8403.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #ifndef __SOUND_CS8403_H
  2. #define __SOUND_CS8403_H
  3. /*
  4. * Routines for Cirrus Logic CS8403/CS8404A IEC958 (S/PDIF) Transmitter
  5. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
  6. * Takashi Iwai <tiwai@suse.de>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #ifdef SND_CS8403
  25. #ifndef SND_CS8403_DECL
  26. #define SND_CS8403_DECL static
  27. #endif
  28. #ifndef SND_CS8403_DECODE
  29. #define SND_CS8403_DECODE snd_cs8403_decode_spdif_bits
  30. #endif
  31. #ifndef SND_CS8403_ENCODE
  32. #define SND_CS8403_ENCODE snd_cs8403_encode_spdif_bits
  33. #endif
  34. SND_CS8403_DECL void SND_CS8403_DECODE(struct snd_aes_iec958 *diga, unsigned char bits)
  35. {
  36. if (bits & 0x01) { /* consumer */
  37. if (!(bits & 0x02))
  38. diga->status[0] |= IEC958_AES0_NONAUDIO;
  39. if (!(bits & 0x08))
  40. diga->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
  41. switch (bits & 0x10) {
  42. case 0x10: diga->status[0] |= IEC958_AES0_CON_EMPHASIS_NONE; break;
  43. case 0x00: diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015; break;
  44. }
  45. if (!(bits & 0x80))
  46. diga->status[1] |= IEC958_AES1_CON_ORIGINAL;
  47. switch (bits & 0x60) {
  48. case 0x00: diga->status[1] |= IEC958_AES1_CON_MAGNETIC_ID; break;
  49. case 0x20: diga->status[1] |= IEC958_AES1_CON_DIGDIGCONV_ID; break;
  50. case 0x40: diga->status[1] |= IEC958_AES1_CON_LASEROPT_ID; break;
  51. case 0x60: diga->status[1] |= IEC958_AES1_CON_GENERAL; break;
  52. }
  53. switch (bits & 0x06) {
  54. case 0x00: diga->status[3] |= IEC958_AES3_CON_FS_44100; break;
  55. case 0x02: diga->status[3] |= IEC958_AES3_CON_FS_48000; break;
  56. case 0x04: diga->status[3] |= IEC958_AES3_CON_FS_32000; break;
  57. }
  58. } else {
  59. diga->status[0] = IEC958_AES0_PROFESSIONAL;
  60. switch (bits & 0x18) {
  61. case 0x00: diga->status[0] |= IEC958_AES0_PRO_FS_32000; break;
  62. case 0x10: diga->status[0] |= IEC958_AES0_PRO_FS_44100; break;
  63. case 0x08: diga->status[0] |= IEC958_AES0_PRO_FS_48000; break;
  64. case 0x18: diga->status[0] |= IEC958_AES0_PRO_FS_NOTID; break;
  65. }
  66. switch (bits & 0x60) {
  67. case 0x20: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_NONE; break;
  68. case 0x40: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015; break;
  69. case 0x00: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_CCITT; break;
  70. case 0x60: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_NOTID; break;
  71. }
  72. if (bits & 0x80)
  73. diga->status[1] |= IEC958_AES1_PRO_MODE_STEREOPHONIC;
  74. }
  75. }
  76. SND_CS8403_DECL unsigned char SND_CS8403_ENCODE(struct snd_aes_iec958 *diga)
  77. {
  78. unsigned char bits;
  79. if (!(diga->status[0] & IEC958_AES0_PROFESSIONAL)) {
  80. bits = 0x01; /* consumer mode */
  81. if (diga->status[0] & IEC958_AES0_NONAUDIO)
  82. bits &= ~0x02;
  83. else
  84. bits |= 0x02;
  85. if (diga->status[0] & IEC958_AES0_CON_NOT_COPYRIGHT)
  86. bits &= ~0x08;
  87. else
  88. bits |= 0x08;
  89. switch (diga->status[0] & IEC958_AES0_CON_EMPHASIS) {
  90. default:
  91. case IEC958_AES0_CON_EMPHASIS_NONE: bits |= 0x10; break;
  92. case IEC958_AES0_CON_EMPHASIS_5015: bits |= 0x00; break;
  93. }
  94. if (diga->status[1] & IEC958_AES1_CON_ORIGINAL)
  95. bits &= ~0x80;
  96. else
  97. bits |= 0x80;
  98. if ((diga->status[1] & IEC958_AES1_CON_CATEGORY) == IEC958_AES1_CON_GENERAL)
  99. bits |= 0x60;
  100. else {
  101. switch(diga->status[1] & IEC958_AES1_CON_MAGNETIC_MASK) {
  102. case IEC958_AES1_CON_MAGNETIC_ID:
  103. bits |= 0x00; break;
  104. case IEC958_AES1_CON_DIGDIGCONV_ID:
  105. bits |= 0x20; break;
  106. default:
  107. case IEC958_AES1_CON_LASEROPT_ID:
  108. bits |= 0x40; break;
  109. }
  110. }
  111. switch (diga->status[3] & IEC958_AES3_CON_FS) {
  112. default:
  113. case IEC958_AES3_CON_FS_44100: bits |= 0x00; break;
  114. case IEC958_AES3_CON_FS_48000: bits |= 0x02; break;
  115. case IEC958_AES3_CON_FS_32000: bits |= 0x04; break;
  116. }
  117. } else {
  118. bits = 0x00; /* professional mode */
  119. if (diga->status[0] & IEC958_AES0_NONAUDIO)
  120. bits &= ~0x02;
  121. else
  122. bits |= 0x02;
  123. /* CHECKME: I'm not sure about the bit order in val here */
  124. switch (diga->status[0] & IEC958_AES0_PRO_FS) {
  125. case IEC958_AES0_PRO_FS_32000: bits |= 0x00; break;
  126. case IEC958_AES0_PRO_FS_44100: bits |= 0x10; break; /* 44.1kHz */
  127. case IEC958_AES0_PRO_FS_48000: bits |= 0x08; break; /* 48kHz */
  128. default:
  129. case IEC958_AES0_PRO_FS_NOTID: bits |= 0x18; break;
  130. }
  131. switch (diga->status[0] & IEC958_AES0_PRO_EMPHASIS) {
  132. case IEC958_AES0_PRO_EMPHASIS_NONE: bits |= 0x20; break;
  133. case IEC958_AES0_PRO_EMPHASIS_5015: bits |= 0x40; break;
  134. case IEC958_AES0_PRO_EMPHASIS_CCITT: bits |= 0x00; break;
  135. default:
  136. case IEC958_AES0_PRO_EMPHASIS_NOTID: bits |= 0x60; break;
  137. }
  138. switch (diga->status[1] & IEC958_AES1_PRO_MODE) {
  139. case IEC958_AES1_PRO_MODE_TWO:
  140. case IEC958_AES1_PRO_MODE_STEREOPHONIC: bits |= 0x00; break;
  141. default: bits |= 0x80; break;
  142. }
  143. }
  144. return bits;
  145. }
  146. #endif /* SND_CS8403 */
  147. #ifdef SND_CS8404
  148. #ifndef SND_CS8404_DECL
  149. #define SND_CS8404_DECL static
  150. #endif
  151. #ifndef SND_CS8404_DECODE
  152. #define SND_CS8404_DECODE snd_cs8404_decode_spdif_bits
  153. #endif
  154. #ifndef SND_CS8404_ENCODE
  155. #define SND_CS8404_ENCODE snd_cs8404_encode_spdif_bits
  156. #endif
  157. SND_CS8404_DECL void SND_CS8404_DECODE(struct snd_aes_iec958 *diga, unsigned char bits)
  158. {
  159. if (bits & 0x10) { /* consumer */
  160. if (!(bits & 0x20))
  161. diga->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
  162. if (!(bits & 0x40))
  163. diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015;
  164. if (!(bits & 0x80))
  165. diga->status[1] |= IEC958_AES1_CON_ORIGINAL;
  166. switch (bits & 0x03) {
  167. case 0x00: diga->status[1] |= IEC958_AES1_CON_DAT; break;
  168. case 0x03: diga->status[1] |= IEC958_AES1_CON_GENERAL; break;
  169. }
  170. switch (bits & 0x06) {
  171. case 0x02: diga->status[3] |= IEC958_AES3_CON_FS_32000; break;
  172. case 0x04: diga->status[3] |= IEC958_AES3_CON_FS_48000; break;
  173. case 0x06: diga->status[3] |= IEC958_AES3_CON_FS_44100; break;
  174. }
  175. } else {
  176. diga->status[0] = IEC958_AES0_PROFESSIONAL;
  177. if (!(bits & 0x04))
  178. diga->status[0] |= IEC958_AES0_NONAUDIO;
  179. switch (bits & 0x60) {
  180. case 0x00: diga->status[0] |= IEC958_AES0_PRO_FS_32000; break;
  181. case 0x40: diga->status[0] |= IEC958_AES0_PRO_FS_44100; break;
  182. case 0x20: diga->status[0] |= IEC958_AES0_PRO_FS_48000; break;
  183. case 0x60: diga->status[0] |= IEC958_AES0_PRO_FS_NOTID; break;
  184. }
  185. switch (bits & 0x03) {
  186. case 0x02: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_NONE; break;
  187. case 0x01: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015; break;
  188. case 0x00: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_CCITT; break;
  189. case 0x03: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_NOTID; break;
  190. }
  191. if (!(bits & 0x80))
  192. diga->status[1] |= IEC958_AES1_PRO_MODE_STEREOPHONIC;
  193. }
  194. }
  195. SND_CS8404_DECL unsigned char SND_CS8404_ENCODE(struct snd_aes_iec958 *diga)
  196. {
  197. unsigned char bits;
  198. if (!(diga->status[0] & IEC958_AES0_PROFESSIONAL)) {
  199. bits = 0x10; /* consumer mode */
  200. if (!(diga->status[0] & IEC958_AES0_CON_NOT_COPYRIGHT))
  201. bits |= 0x20;
  202. if ((diga->status[0] & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_NONE)
  203. bits |= 0x40;
  204. if (!(diga->status[1] & IEC958_AES1_CON_ORIGINAL))
  205. bits |= 0x80;
  206. if ((diga->status[1] & IEC958_AES1_CON_CATEGORY) == IEC958_AES1_CON_GENERAL)
  207. bits |= 0x03;
  208. switch (diga->status[3] & IEC958_AES3_CON_FS) {
  209. default:
  210. case IEC958_AES3_CON_FS_44100: bits |= 0x06; break;
  211. case IEC958_AES3_CON_FS_48000: bits |= 0x04; break;
  212. case IEC958_AES3_CON_FS_32000: bits |= 0x02; break;
  213. }
  214. } else {
  215. bits = 0x00; /* professional mode */
  216. if (!(diga->status[0] & IEC958_AES0_NONAUDIO))
  217. bits |= 0x04;
  218. switch (diga->status[0] & IEC958_AES0_PRO_FS) {
  219. case IEC958_AES0_PRO_FS_32000: bits |= 0x00; break;
  220. case IEC958_AES0_PRO_FS_44100: bits |= 0x40; break; /* 44.1kHz */
  221. case IEC958_AES0_PRO_FS_48000: bits |= 0x20; break; /* 48kHz */
  222. default:
  223. case IEC958_AES0_PRO_FS_NOTID: bits |= 0x00; break;
  224. }
  225. switch (diga->status[0] & IEC958_AES0_PRO_EMPHASIS) {
  226. case IEC958_AES0_PRO_EMPHASIS_NONE: bits |= 0x02; break;
  227. case IEC958_AES0_PRO_EMPHASIS_5015: bits |= 0x01; break;
  228. case IEC958_AES0_PRO_EMPHASIS_CCITT: bits |= 0x00; break;
  229. default:
  230. case IEC958_AES0_PRO_EMPHASIS_NOTID: bits |= 0x03; break;
  231. }
  232. switch (diga->status[1] & IEC958_AES1_PRO_MODE) {
  233. case IEC958_AES1_PRO_MODE_TWO:
  234. case IEC958_AES1_PRO_MODE_STEREOPHONIC: bits |= 0x00; break;
  235. default: bits |= 0x80; break;
  236. }
  237. }
  238. return bits;
  239. }
  240. #endif /* SND_CS8404 */
  241. #endif /* __SOUND_CS8403_H */