cs8403.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef __SOUND_CS8403_H
  3. #define __SOUND_CS8403_H
  4. /*
  5. * Routines for Cirrus Logic CS8403/CS8404A IEC958 (S/PDIF) Transmitter
  6. * Copyright (c) by Jaroslav Kysela <[email protected]>,
  7. * Takashi Iwai <[email protected]>
  8. */
  9. #ifdef SND_CS8403
  10. #ifndef SND_CS8403_DECL
  11. #define SND_CS8403_DECL static
  12. #endif
  13. #ifndef SND_CS8403_DECODE
  14. #define SND_CS8403_DECODE snd_cs8403_decode_spdif_bits
  15. #endif
  16. #ifndef SND_CS8403_ENCODE
  17. #define SND_CS8403_ENCODE snd_cs8403_encode_spdif_bits
  18. #endif
  19. SND_CS8403_DECL void SND_CS8403_DECODE(struct snd_aes_iec958 *diga, unsigned char bits)
  20. {
  21. if (bits & 0x01) { /* consumer */
  22. if (!(bits & 0x02))
  23. diga->status[0] |= IEC958_AES0_NONAUDIO;
  24. if (!(bits & 0x08))
  25. diga->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
  26. switch (bits & 0x10) {
  27. case 0x10: diga->status[0] |= IEC958_AES0_CON_EMPHASIS_NONE; break;
  28. case 0x00: diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015; break;
  29. }
  30. if (!(bits & 0x80))
  31. diga->status[1] |= IEC958_AES1_CON_ORIGINAL;
  32. switch (bits & 0x60) {
  33. case 0x00: diga->status[1] |= IEC958_AES1_CON_MAGNETIC_ID; break;
  34. case 0x20: diga->status[1] |= IEC958_AES1_CON_DIGDIGCONV_ID; break;
  35. case 0x40: diga->status[1] |= IEC958_AES1_CON_LASEROPT_ID; break;
  36. case 0x60: diga->status[1] |= IEC958_AES1_CON_GENERAL; break;
  37. }
  38. switch (bits & 0x06) {
  39. case 0x00: diga->status[3] |= IEC958_AES3_CON_FS_44100; break;
  40. case 0x02: diga->status[3] |= IEC958_AES3_CON_FS_48000; break;
  41. case 0x04: diga->status[3] |= IEC958_AES3_CON_FS_32000; break;
  42. }
  43. } else {
  44. diga->status[0] = IEC958_AES0_PROFESSIONAL;
  45. switch (bits & 0x18) {
  46. case 0x00: diga->status[0] |= IEC958_AES0_PRO_FS_32000; break;
  47. case 0x10: diga->status[0] |= IEC958_AES0_PRO_FS_44100; break;
  48. case 0x08: diga->status[0] |= IEC958_AES0_PRO_FS_48000; break;
  49. case 0x18: diga->status[0] |= IEC958_AES0_PRO_FS_NOTID; break;
  50. }
  51. switch (bits & 0x60) {
  52. case 0x20: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_NONE; break;
  53. case 0x40: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015; break;
  54. case 0x00: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_CCITT; break;
  55. case 0x60: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_NOTID; break;
  56. }
  57. if (bits & 0x80)
  58. diga->status[1] |= IEC958_AES1_PRO_MODE_STEREOPHONIC;
  59. }
  60. }
  61. SND_CS8403_DECL unsigned char SND_CS8403_ENCODE(struct snd_aes_iec958 *diga)
  62. {
  63. unsigned char bits;
  64. if (!(diga->status[0] & IEC958_AES0_PROFESSIONAL)) {
  65. bits = 0x01; /* consumer mode */
  66. if (diga->status[0] & IEC958_AES0_NONAUDIO)
  67. bits &= ~0x02;
  68. else
  69. bits |= 0x02;
  70. if (diga->status[0] & IEC958_AES0_CON_NOT_COPYRIGHT)
  71. bits &= ~0x08;
  72. else
  73. bits |= 0x08;
  74. switch (diga->status[0] & IEC958_AES0_CON_EMPHASIS) {
  75. default:
  76. case IEC958_AES0_CON_EMPHASIS_NONE: bits |= 0x10; break;
  77. case IEC958_AES0_CON_EMPHASIS_5015: bits |= 0x00; break;
  78. }
  79. if (diga->status[1] & IEC958_AES1_CON_ORIGINAL)
  80. bits &= ~0x80;
  81. else
  82. bits |= 0x80;
  83. if ((diga->status[1] & IEC958_AES1_CON_CATEGORY) == IEC958_AES1_CON_GENERAL)
  84. bits |= 0x60;
  85. else {
  86. switch(diga->status[1] & IEC958_AES1_CON_MAGNETIC_MASK) {
  87. case IEC958_AES1_CON_MAGNETIC_ID:
  88. bits |= 0x00; break;
  89. case IEC958_AES1_CON_DIGDIGCONV_ID:
  90. bits |= 0x20; break;
  91. default:
  92. case IEC958_AES1_CON_LASEROPT_ID:
  93. bits |= 0x40; break;
  94. }
  95. }
  96. switch (diga->status[3] & IEC958_AES3_CON_FS) {
  97. default:
  98. case IEC958_AES3_CON_FS_44100: bits |= 0x00; break;
  99. case IEC958_AES3_CON_FS_48000: bits |= 0x02; break;
  100. case IEC958_AES3_CON_FS_32000: bits |= 0x04; break;
  101. }
  102. } else {
  103. bits = 0x00; /* professional mode */
  104. if (diga->status[0] & IEC958_AES0_NONAUDIO)
  105. bits &= ~0x02;
  106. else
  107. bits |= 0x02;
  108. /* CHECKME: I'm not sure about the bit order in val here */
  109. switch (diga->status[0] & IEC958_AES0_PRO_FS) {
  110. case IEC958_AES0_PRO_FS_32000: bits |= 0x00; break;
  111. case IEC958_AES0_PRO_FS_44100: bits |= 0x10; break; /* 44.1kHz */
  112. case IEC958_AES0_PRO_FS_48000: bits |= 0x08; break; /* 48kHz */
  113. default:
  114. case IEC958_AES0_PRO_FS_NOTID: bits |= 0x18; break;
  115. }
  116. switch (diga->status[0] & IEC958_AES0_PRO_EMPHASIS) {
  117. case IEC958_AES0_PRO_EMPHASIS_NONE: bits |= 0x20; break;
  118. case IEC958_AES0_PRO_EMPHASIS_5015: bits |= 0x40; break;
  119. case IEC958_AES0_PRO_EMPHASIS_CCITT: bits |= 0x00; break;
  120. default:
  121. case IEC958_AES0_PRO_EMPHASIS_NOTID: bits |= 0x60; break;
  122. }
  123. switch (diga->status[1] & IEC958_AES1_PRO_MODE) {
  124. case IEC958_AES1_PRO_MODE_TWO:
  125. case IEC958_AES1_PRO_MODE_STEREOPHONIC: bits |= 0x00; break;
  126. default: bits |= 0x80; break;
  127. }
  128. }
  129. return bits;
  130. }
  131. #endif /* SND_CS8403 */
  132. #ifdef SND_CS8404
  133. #ifndef SND_CS8404_DECL
  134. #define SND_CS8404_DECL static
  135. #endif
  136. #ifndef SND_CS8404_DECODE
  137. #define SND_CS8404_DECODE snd_cs8404_decode_spdif_bits
  138. #endif
  139. #ifndef SND_CS8404_ENCODE
  140. #define SND_CS8404_ENCODE snd_cs8404_encode_spdif_bits
  141. #endif
  142. SND_CS8404_DECL void SND_CS8404_DECODE(struct snd_aes_iec958 *diga, unsigned char bits)
  143. {
  144. if (bits & 0x10) { /* consumer */
  145. if (!(bits & 0x20))
  146. diga->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
  147. if (!(bits & 0x40))
  148. diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015;
  149. if (!(bits & 0x80))
  150. diga->status[1] |= IEC958_AES1_CON_ORIGINAL;
  151. switch (bits & 0x03) {
  152. case 0x00: diga->status[1] |= IEC958_AES1_CON_DAT; break;
  153. case 0x03: diga->status[1] |= IEC958_AES1_CON_GENERAL; break;
  154. }
  155. switch (bits & 0x06) {
  156. case 0x02: diga->status[3] |= IEC958_AES3_CON_FS_32000; break;
  157. case 0x04: diga->status[3] |= IEC958_AES3_CON_FS_48000; break;
  158. case 0x06: diga->status[3] |= IEC958_AES3_CON_FS_44100; break;
  159. }
  160. } else {
  161. diga->status[0] = IEC958_AES0_PROFESSIONAL;
  162. if (!(bits & 0x04))
  163. diga->status[0] |= IEC958_AES0_NONAUDIO;
  164. switch (bits & 0x60) {
  165. case 0x00: diga->status[0] |= IEC958_AES0_PRO_FS_32000; break;
  166. case 0x40: diga->status[0] |= IEC958_AES0_PRO_FS_44100; break;
  167. case 0x20: diga->status[0] |= IEC958_AES0_PRO_FS_48000; break;
  168. case 0x60: diga->status[0] |= IEC958_AES0_PRO_FS_NOTID; break;
  169. }
  170. switch (bits & 0x03) {
  171. case 0x02: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_NONE; break;
  172. case 0x01: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015; break;
  173. case 0x00: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_CCITT; break;
  174. case 0x03: diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_NOTID; break;
  175. }
  176. if (!(bits & 0x80))
  177. diga->status[1] |= IEC958_AES1_PRO_MODE_STEREOPHONIC;
  178. }
  179. }
  180. SND_CS8404_DECL unsigned char SND_CS8404_ENCODE(struct snd_aes_iec958 *diga)
  181. {
  182. unsigned char bits;
  183. if (!(diga->status[0] & IEC958_AES0_PROFESSIONAL)) {
  184. bits = 0x10; /* consumer mode */
  185. if (!(diga->status[0] & IEC958_AES0_CON_NOT_COPYRIGHT))
  186. bits |= 0x20;
  187. if ((diga->status[0] & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_NONE)
  188. bits |= 0x40;
  189. if (!(diga->status[1] & IEC958_AES1_CON_ORIGINAL))
  190. bits |= 0x80;
  191. if ((diga->status[1] & IEC958_AES1_CON_CATEGORY) == IEC958_AES1_CON_GENERAL)
  192. bits |= 0x03;
  193. switch (diga->status[3] & IEC958_AES3_CON_FS) {
  194. default:
  195. case IEC958_AES3_CON_FS_44100: bits |= 0x06; break;
  196. case IEC958_AES3_CON_FS_48000: bits |= 0x04; break;
  197. case IEC958_AES3_CON_FS_32000: bits |= 0x02; break;
  198. }
  199. } else {
  200. bits = 0x00; /* professional mode */
  201. if (!(diga->status[0] & IEC958_AES0_NONAUDIO))
  202. bits |= 0x04;
  203. switch (diga->status[0] & IEC958_AES0_PRO_FS) {
  204. case IEC958_AES0_PRO_FS_32000: bits |= 0x00; break;
  205. case IEC958_AES0_PRO_FS_44100: bits |= 0x40; break; /* 44.1kHz */
  206. case IEC958_AES0_PRO_FS_48000: bits |= 0x20; break; /* 48kHz */
  207. default:
  208. case IEC958_AES0_PRO_FS_NOTID: bits |= 0x00; break;
  209. }
  210. switch (diga->status[0] & IEC958_AES0_PRO_EMPHASIS) {
  211. case IEC958_AES0_PRO_EMPHASIS_NONE: bits |= 0x02; break;
  212. case IEC958_AES0_PRO_EMPHASIS_5015: bits |= 0x01; break;
  213. case IEC958_AES0_PRO_EMPHASIS_CCITT: bits |= 0x00; break;
  214. default:
  215. case IEC958_AES0_PRO_EMPHASIS_NOTID: bits |= 0x03; break;
  216. }
  217. switch (diga->status[1] & IEC958_AES1_PRO_MODE) {
  218. case IEC958_AES1_PRO_MODE_TWO:
  219. case IEC958_AES1_PRO_MODE_STEREOPHONIC: bits |= 0x00; break;
  220. default: bits |= 0x80; break;
  221. }
  222. }
  223. return bits;
  224. }
  225. #endif /* SND_CS8404 */
  226. #endif /* __SOUND_CS8403_H */