motu-protocol-v3.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * motu-protocol-v3.c - a part of driver for MOTU FireWire series
  3. *
  4. * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include <linux/delay.h>
  9. #include "motu.h"
  10. #define V3_CLOCK_STATUS_OFFSET 0x0b14
  11. #define V3_FETCH_PCM_FRAMES 0x02000000
  12. #define V3_CLOCK_RATE_MASK 0x0000ff00
  13. #define V3_CLOCK_RATE_SHIFT 8
  14. #define V3_CLOCK_SOURCE_MASK 0x000000ff
  15. #define V3_OPT_IFACE_MODE_OFFSET 0x0c94
  16. #define V3_ENABLE_OPT_IN_IFACE_A 0x00000001
  17. #define V3_ENABLE_OPT_IN_IFACE_B 0x00000002
  18. #define V3_ENABLE_OPT_OUT_IFACE_A 0x00000100
  19. #define V3_ENABLE_OPT_OUT_IFACE_B 0x00000200
  20. #define V3_NO_ADAT_OPT_IN_IFACE_A 0x00010000
  21. #define V3_NO_ADAT_OPT_IN_IFACE_B 0x00100000
  22. #define V3_NO_ADAT_OPT_OUT_IFACE_A 0x00040000
  23. #define V3_NO_ADAT_OPT_OUT_IFACE_B 0x00400000
  24. static int v3_get_clock_rate(struct snd_motu *motu, unsigned int *rate)
  25. {
  26. __be32 reg;
  27. u32 data;
  28. int err;
  29. err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, &reg,
  30. sizeof(reg));
  31. if (err < 0)
  32. return err;
  33. data = be32_to_cpu(reg);
  34. data = (data & V3_CLOCK_RATE_MASK) >> V3_CLOCK_RATE_SHIFT;
  35. if (data >= ARRAY_SIZE(snd_motu_clock_rates))
  36. return -EIO;
  37. *rate = snd_motu_clock_rates[data];
  38. return 0;
  39. }
  40. static int v3_set_clock_rate(struct snd_motu *motu, unsigned int rate)
  41. {
  42. __be32 reg;
  43. u32 data;
  44. bool need_to_wait;
  45. int i, err;
  46. for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
  47. if (snd_motu_clock_rates[i] == rate)
  48. break;
  49. }
  50. if (i == ARRAY_SIZE(snd_motu_clock_rates))
  51. return -EINVAL;
  52. err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, &reg,
  53. sizeof(reg));
  54. if (err < 0)
  55. return err;
  56. data = be32_to_cpu(reg);
  57. data &= ~(V3_CLOCK_RATE_MASK | V3_FETCH_PCM_FRAMES);
  58. data |= i << V3_CLOCK_RATE_SHIFT;
  59. need_to_wait = data != be32_to_cpu(reg);
  60. reg = cpu_to_be32(data);
  61. err = snd_motu_transaction_write(motu, V3_CLOCK_STATUS_OFFSET, &reg,
  62. sizeof(reg));
  63. if (err < 0)
  64. return err;
  65. if (need_to_wait) {
  66. /* Cost expensive. */
  67. if (msleep_interruptible(4000) > 0)
  68. return -EINTR;
  69. }
  70. return 0;
  71. }
  72. static int v3_get_clock_source(struct snd_motu *motu,
  73. enum snd_motu_clock_source *src)
  74. {
  75. __be32 reg;
  76. u32 data;
  77. unsigned int val;
  78. int err;
  79. err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, &reg,
  80. sizeof(reg));
  81. if (err < 0)
  82. return err;
  83. data = be32_to_cpu(reg);
  84. val = data & V3_CLOCK_SOURCE_MASK;
  85. if (val == 0x00) {
  86. *src = SND_MOTU_CLOCK_SOURCE_INTERNAL;
  87. } else if (val == 0x01) {
  88. *src = SND_MOTU_CLOCK_SOURCE_WORD_ON_BNC;
  89. } else if (val == 0x10) {
  90. *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_COAX;
  91. } else if (val == 0x18 || val == 0x19) {
  92. err = snd_motu_transaction_read(motu, V3_OPT_IFACE_MODE_OFFSET,
  93. &reg, sizeof(reg));
  94. if (err < 0)
  95. return err;
  96. data = be32_to_cpu(reg);
  97. if (val == 0x18) {
  98. if (data & V3_NO_ADAT_OPT_IN_IFACE_A)
  99. *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_A;
  100. else
  101. *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_A;
  102. } else {
  103. if (data & V3_NO_ADAT_OPT_IN_IFACE_B)
  104. *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_B;
  105. else
  106. *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_B;
  107. }
  108. } else {
  109. *src = SND_MOTU_CLOCK_SOURCE_UNKNOWN;
  110. }
  111. return 0;
  112. }
  113. static int v3_switch_fetching_mode(struct snd_motu *motu, bool enable)
  114. {
  115. __be32 reg;
  116. u32 data;
  117. int err;
  118. err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, &reg,
  119. sizeof(reg));
  120. if (err < 0)
  121. return 0;
  122. data = be32_to_cpu(reg);
  123. if (enable)
  124. data |= V3_FETCH_PCM_FRAMES;
  125. else
  126. data &= ~V3_FETCH_PCM_FRAMES;
  127. reg = cpu_to_be32(data);
  128. return snd_motu_transaction_write(motu, V3_CLOCK_STATUS_OFFSET, &reg,
  129. sizeof(reg));
  130. }
  131. static void calculate_fixed_part(struct snd_motu_packet_format *formats,
  132. enum amdtp_stream_direction dir,
  133. enum snd_motu_spec_flags flags,
  134. unsigned char analog_ports)
  135. {
  136. unsigned char pcm_chunks[3] = {0, 0, 0};
  137. formats->msg_chunks = 2;
  138. pcm_chunks[0] = analog_ports;
  139. pcm_chunks[1] = analog_ports;
  140. if (flags & SND_MOTU_SPEC_SUPPORT_CLOCK_X4)
  141. pcm_chunks[2] = analog_ports;
  142. if (dir == AMDTP_IN_STREAM) {
  143. if (flags & SND_MOTU_SPEC_TX_MICINST_CHUNK) {
  144. pcm_chunks[0] += 2;
  145. pcm_chunks[1] += 2;
  146. if (flags & SND_MOTU_SPEC_SUPPORT_CLOCK_X4)
  147. pcm_chunks[2] += 2;
  148. }
  149. if (flags & SND_MOTU_SPEC_TX_RETURN_CHUNK) {
  150. pcm_chunks[0] += 2;
  151. pcm_chunks[1] += 2;
  152. if (flags & SND_MOTU_SPEC_SUPPORT_CLOCK_X4)
  153. pcm_chunks[2] += 2;
  154. }
  155. if (flags & SND_MOTU_SPEC_TX_REVERB_CHUNK) {
  156. pcm_chunks[0] += 2;
  157. pcm_chunks[1] += 2;
  158. }
  159. } else {
  160. /*
  161. * Packets to v2 units transfer main-out-1/2 and phone-out-1/2.
  162. */
  163. pcm_chunks[0] += 4;
  164. pcm_chunks[1] += 4;
  165. }
  166. /*
  167. * At least, packets have two data chunks for S/PDIF on coaxial
  168. * interface.
  169. */
  170. pcm_chunks[0] += 2;
  171. pcm_chunks[1] += 2;
  172. /*
  173. * Fixed part consists of PCM chunks multiple of 4, with msg chunks. As
  174. * a result, this part can includes empty data chunks.
  175. */
  176. formats->fixed_part_pcm_chunks[0] = round_up(2 + pcm_chunks[0], 4) - 2;
  177. formats->fixed_part_pcm_chunks[1] = round_up(2 + pcm_chunks[1], 4) - 2;
  178. if (flags & SND_MOTU_SPEC_SUPPORT_CLOCK_X4)
  179. formats->fixed_part_pcm_chunks[2] =
  180. round_up(2 + pcm_chunks[2], 4) - 2;
  181. }
  182. static void calculate_differed_part(struct snd_motu_packet_format *formats,
  183. enum snd_motu_spec_flags flags, u32 data,
  184. u32 a_enable_mask, u32 a_no_adat_mask,
  185. u32 b_enable_mask, u32 b_no_adat_mask)
  186. {
  187. unsigned char pcm_chunks[3] = {0, 0, 0};
  188. int i;
  189. if ((flags & SND_MOTU_SPEC_HAS_OPT_IFACE_A) && (data & a_enable_mask)) {
  190. if (data & a_no_adat_mask) {
  191. /*
  192. * Additional two data chunks for S/PDIF on optical
  193. * interface A. This includes empty data chunks.
  194. */
  195. pcm_chunks[0] += 4;
  196. pcm_chunks[1] += 4;
  197. } else {
  198. /*
  199. * Additional data chunks for ADAT on optical interface
  200. * A.
  201. */
  202. pcm_chunks[0] += 8;
  203. pcm_chunks[1] += 4;
  204. }
  205. }
  206. if ((flags & SND_MOTU_SPEC_HAS_OPT_IFACE_B) && (data & b_enable_mask)) {
  207. if (data & b_no_adat_mask) {
  208. /*
  209. * Additional two data chunks for S/PDIF on optical
  210. * interface B. This includes empty data chunks.
  211. */
  212. pcm_chunks[0] += 4;
  213. pcm_chunks[1] += 4;
  214. } else {
  215. /*
  216. * Additional data chunks for ADAT on optical interface
  217. * B.
  218. */
  219. pcm_chunks[0] += 8;
  220. pcm_chunks[1] += 4;
  221. }
  222. }
  223. for (i = 0; i < 3; ++i) {
  224. if (pcm_chunks[i] > 0)
  225. pcm_chunks[i] = round_up(pcm_chunks[i], 4);
  226. formats->differed_part_pcm_chunks[i] = pcm_chunks[i];
  227. }
  228. }
  229. static int v3_cache_packet_formats(struct snd_motu *motu)
  230. {
  231. __be32 reg;
  232. u32 data;
  233. int err;
  234. err = snd_motu_transaction_read(motu, V3_OPT_IFACE_MODE_OFFSET, &reg,
  235. sizeof(reg));
  236. if (err < 0)
  237. return err;
  238. data = be32_to_cpu(reg);
  239. calculate_fixed_part(&motu->tx_packet_formats, AMDTP_IN_STREAM,
  240. motu->spec->flags, motu->spec->analog_in_ports);
  241. calculate_differed_part(&motu->tx_packet_formats,
  242. motu->spec->flags, data,
  243. V3_ENABLE_OPT_IN_IFACE_A, V3_NO_ADAT_OPT_IN_IFACE_A,
  244. V3_ENABLE_OPT_IN_IFACE_B, V3_NO_ADAT_OPT_IN_IFACE_B);
  245. calculate_fixed_part(&motu->rx_packet_formats, AMDTP_OUT_STREAM,
  246. motu->spec->flags, motu->spec->analog_out_ports);
  247. calculate_differed_part(&motu->rx_packet_formats,
  248. motu->spec->flags, data,
  249. V3_ENABLE_OPT_OUT_IFACE_A, V3_NO_ADAT_OPT_OUT_IFACE_A,
  250. V3_ENABLE_OPT_OUT_IFACE_B, V3_NO_ADAT_OPT_OUT_IFACE_B);
  251. motu->tx_packet_formats.pcm_byte_offset = 10;
  252. motu->rx_packet_formats.pcm_byte_offset = 10;
  253. return 0;
  254. }
  255. const struct snd_motu_protocol snd_motu_protocol_v3 = {
  256. .get_clock_rate = v3_get_clock_rate,
  257. .set_clock_rate = v3_set_clock_rate,
  258. .get_clock_source = v3_get_clock_source,
  259. .switch_fetching_mode = v3_switch_fetching_mode,
  260. .cache_packet_formats = v3_cache_packet_formats,
  261. };