motu-protocol-v1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // motu-protocol-v1.c - a part of driver for MOTU FireWire series
  3. //
  4. // Copyright (c) 2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>
  5. #include "motu.h"
  6. #include <linux/delay.h>
  7. // Status register for MOTU 828 (0x'ffff'f000'0b00).
  8. //
  9. // 0xffff0000: ISOC_COMM_CONTROL_MASK in motu-stream.c.
  10. // 0x00008000: mode of optical input interface.
  11. // 0x00008000: for S/PDIF signal.
  12. // 0x00000000: disabled or for ADAT signal.
  13. // 0x00004000: mode of optical output interface.
  14. // 0x00004000: for S/PDIF signal.
  15. // 0x00000000: disabled or for ADAT signal.
  16. // 0x00003f00: monitor input mode.
  17. // 0x00000800: analog-1/2
  18. // 0x00001a00: analog-3/4
  19. // 0x00002c00: analog-5/6
  20. // 0x00003e00: analog-7/8
  21. // 0x00000000: analog-1
  22. // 0x00000900: analog-2
  23. // 0x00001200: analog-3
  24. // 0x00001b00: analog-4
  25. // 0x00002400: analog-5
  26. // 0x00002d00: analog-6
  27. // 0x00003600: analog-7
  28. // 0x00003f00: analog-8
  29. // 0x00000080: enable stream input.
  30. // 0x00000040: disable monitor input.
  31. // 0x00000008: enable main out.
  32. // 0x00000004: rate of sampling clock.
  33. // 0x00000004: 48.0 kHz
  34. // 0x00000000: 44.1 kHz
  35. // 0x00000023: source of sampling clock.
  36. // 0x00000003: source packet header (SPH)
  37. // 0x00000002: S/PDIF on optical/coaxial interface.
  38. // 0x00000021: ADAT on optical interface
  39. // 0x00000001: ADAT on Dsub 9pin
  40. // 0x00000000: internal
  41. #define CLK_828_STATUS_OFFSET 0x0b00
  42. #define CLK_828_STATUS_MASK 0x0000ffff
  43. #define CLK_828_STATUS_FLAG_OPT_IN_IFACE_IS_SPDIF 0x00008000
  44. #define CLK_828_STATUS_FLAG_OPT_OUT_IFACE_IS_SPDIF 0x00004000
  45. #define CLK_828_STATUS_FLAG_FETCH_PCM_FRAMES 0x00000080
  46. #define CLK_828_STATUS_FLAG_ENABLE_OUTPUT 0x00000008
  47. #define CLK_828_STATUS_FLAG_RATE_48000 0x00000004
  48. #define CLK_828_STATUS_MASK_SRC 0x00000023
  49. #define CLK_828_STATUS_FLAG_SRC_ADAT_ON_OPT 0x00000021
  50. #define CLK_828_STATUS_FLAG_SRC_SPH 0x00000003
  51. #define CLK_828_STATUS_FLAG_SRC_SPDIF 0x00000002
  52. #define CLK_828_STATUS_FLAG_SRC_ADAT_ON_DSUB 0x00000001
  53. #define CLK_828_STATUS_FLAG_SRC_INTERNAL 0x00000000
  54. // Status register for MOTU 896 (0x'ffff'f000'0b14).
  55. //
  56. // 0xf0000000: enable physical and stream input to DAC.
  57. // 0x80000000: disable
  58. // 0x40000000: disable
  59. // 0x20000000: enable (prior to the other bits)
  60. // 0x10000000: disable
  61. // 0x00000000: disable
  62. // 0x08000000: speed of word clock signal output on BNC interface.
  63. // 0x00000000: force to low rate (44.1/48.0 kHz).
  64. // 0x08000000: follow to system clock.
  65. // 0x04000000: something relevant to clock.
  66. // 0x03000000: enable output.
  67. // 0x02000000: enabled irreversibly once standing unless the device voluntarily disables it.
  68. // 0x01000000: enabled irreversibly once standing unless the device voluntarily disables it.
  69. // 0x00ffff00: monitor input mode.
  70. // 0x00000000: disabled
  71. // 0x00004800: analog-1/2
  72. // 0x00005a00: analog-3/4
  73. // 0x00006c00: analog-5/6
  74. // 0x00007e00: analog-7/8
  75. // 0x00104800: AES/EBU-1/2
  76. // 0x00004000: analog-1
  77. // 0x00004900: analog-2
  78. // 0x00005200: analog-3
  79. // 0x00005b00: analog-4
  80. // 0x00006400: analog-5
  81. // 0x00006d00: analog-6
  82. // 0x00007600: analog-7
  83. // 0x00007f00: analog-8
  84. // 0x00104000: AES/EBU-1
  85. // 0x00104900: AES/EBU-2
  86. // 0x00000060: sample rate conversion for AES/EBU input/output.
  87. // 0x00000000: None
  88. // 0x00000020: input signal is converted to system rate
  89. // 0x00000040: output is slave to input, ignoring system rate
  90. // 0x00000060: output is double rate than system rate
  91. // 0x00000018: nominal rate of sampling clock.
  92. // 0x00000000: 44.1 kHz
  93. // 0x00000008: 48.0 kHz
  94. // 0x00000010: 88.2 kHz
  95. // 0x00000018: 96.0 kHz
  96. // 0x00000007: source of sampling clock.
  97. // 0x00000000: internal
  98. // 0x00000001: ADAT on optical interface
  99. // 0x00000002: AES/EBU on XLR
  100. // 0x00000003: source packet header (SPH)
  101. // 0x00000004: word clock on BNC
  102. // 0x00000005: ADAT on Dsub 9pin
  103. #define CLK_896_STATUS_OFFSET 0x0b14
  104. #define CLK_896_STATUS_FLAG_FETCH_ENABLE 0x20000000
  105. #define CLK_896_STATUS_FLAG_OUTPUT_ON 0x03000000
  106. #define CLK_896_STATUS_MASK_SRC 0x00000007
  107. #define CLK_896_STATUS_FLAG_SRC_INTERNAL 0x00000000
  108. #define CLK_896_STATUS_FLAG_SRC_ADAT_ON_OPT 0x00000001
  109. #define CLK_896_STATUS_FLAG_SRC_AESEBU 0x00000002
  110. #define CLK_896_STATUS_FLAG_SRC_SPH 0x00000003
  111. #define CLK_896_STATUS_FLAG_SRC_WORD 0x00000004
  112. #define CLK_896_STATUS_FLAG_SRC_ADAT_ON_DSUB 0x00000005
  113. #define CLK_896_STATUS_MASK_RATE 0x00000018
  114. #define CLK_896_STATUS_FLAG_RATE_44100 0x00000000
  115. #define CLK_896_STATUS_FLAG_RATE_48000 0x00000008
  116. #define CLK_896_STATUS_FLAG_RATE_88200 0x00000010
  117. #define CLK_896_STATUS_FLAG_RATE_96000 0x00000018
  118. static void parse_clock_rate_828(u32 data, unsigned int *rate)
  119. {
  120. if (data & CLK_828_STATUS_FLAG_RATE_48000)
  121. *rate = 48000;
  122. else
  123. *rate = 44100;
  124. }
  125. static int get_clock_rate_828(struct snd_motu *motu, unsigned int *rate)
  126. {
  127. __be32 reg;
  128. int err;
  129. err = snd_motu_transaction_read(motu, CLK_828_STATUS_OFFSET, &reg, sizeof(reg));
  130. if (err < 0)
  131. return err;
  132. parse_clock_rate_828(be32_to_cpu(reg), rate);
  133. return 0;
  134. }
  135. static int parse_clock_rate_896(u32 data, unsigned int *rate)
  136. {
  137. switch (data & CLK_896_STATUS_MASK_RATE) {
  138. case CLK_896_STATUS_FLAG_RATE_44100:
  139. *rate = 44100;
  140. break;
  141. case CLK_896_STATUS_FLAG_RATE_48000:
  142. *rate = 48000;
  143. break;
  144. case CLK_896_STATUS_FLAG_RATE_88200:
  145. *rate = 88200;
  146. break;
  147. case CLK_896_STATUS_FLAG_RATE_96000:
  148. *rate = 96000;
  149. break;
  150. default:
  151. return -ENXIO;
  152. }
  153. return 0;
  154. }
  155. static int get_clock_rate_896(struct snd_motu *motu, unsigned int *rate)
  156. {
  157. __be32 reg;
  158. int err;
  159. err = snd_motu_transaction_read(motu, CLK_896_STATUS_OFFSET, &reg, sizeof(reg));
  160. if (err < 0)
  161. return err;
  162. return parse_clock_rate_896(be32_to_cpu(reg), rate);
  163. }
  164. int snd_motu_protocol_v1_get_clock_rate(struct snd_motu *motu, unsigned int *rate)
  165. {
  166. if (motu->spec == &snd_motu_spec_828)
  167. return get_clock_rate_828(motu, rate);
  168. else if (motu->spec == &snd_motu_spec_896)
  169. return get_clock_rate_896(motu, rate);
  170. else
  171. return -ENXIO;
  172. }
  173. static int set_clock_rate_828(struct snd_motu *motu, unsigned int rate)
  174. {
  175. __be32 reg;
  176. u32 data;
  177. int err;
  178. err = snd_motu_transaction_read(motu, CLK_828_STATUS_OFFSET, &reg, sizeof(reg));
  179. if (err < 0)
  180. return err;
  181. data = be32_to_cpu(reg) & CLK_828_STATUS_MASK;
  182. data &= ~CLK_828_STATUS_FLAG_RATE_48000;
  183. if (rate == 48000)
  184. data |= CLK_828_STATUS_FLAG_RATE_48000;
  185. reg = cpu_to_be32(data);
  186. return snd_motu_transaction_write(motu, CLK_828_STATUS_OFFSET, &reg, sizeof(reg));
  187. }
  188. static int set_clock_rate_896(struct snd_motu *motu, unsigned int rate)
  189. {
  190. unsigned int flag;
  191. __be32 reg;
  192. u32 data;
  193. int err;
  194. err = snd_motu_transaction_read(motu, CLK_896_STATUS_OFFSET, &reg, sizeof(reg));
  195. if (err < 0)
  196. return err;
  197. data = be32_to_cpu(reg);
  198. switch (rate) {
  199. case 44100:
  200. flag = CLK_896_STATUS_FLAG_RATE_44100;
  201. break;
  202. case 48000:
  203. flag = CLK_896_STATUS_FLAG_RATE_48000;
  204. break;
  205. case 88200:
  206. flag = CLK_896_STATUS_FLAG_RATE_88200;
  207. break;
  208. case 96000:
  209. flag = CLK_896_STATUS_FLAG_RATE_96000;
  210. break;
  211. default:
  212. return -EINVAL;
  213. }
  214. data &= ~CLK_896_STATUS_MASK_RATE;
  215. data |= flag;
  216. reg = cpu_to_be32(data);
  217. return snd_motu_transaction_write(motu, CLK_896_STATUS_OFFSET, &reg, sizeof(reg));
  218. }
  219. int snd_motu_protocol_v1_set_clock_rate(struct snd_motu *motu, unsigned int rate)
  220. {
  221. if (motu->spec == &snd_motu_spec_828)
  222. return set_clock_rate_828(motu, rate);
  223. else if (motu->spec == &snd_motu_spec_896)
  224. return set_clock_rate_896(motu, rate);
  225. else
  226. return -ENXIO;
  227. }
  228. static int get_clock_source_828(struct snd_motu *motu, enum snd_motu_clock_source *src)
  229. {
  230. __be32 reg;
  231. u32 data;
  232. int err;
  233. err = snd_motu_transaction_read(motu, CLK_828_STATUS_OFFSET, &reg, sizeof(reg));
  234. if (err < 0)
  235. return err;
  236. data = be32_to_cpu(reg) & CLK_828_STATUS_MASK;
  237. switch (data & CLK_828_STATUS_MASK_SRC) {
  238. case CLK_828_STATUS_FLAG_SRC_ADAT_ON_OPT:
  239. *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT;
  240. break;
  241. case CLK_828_STATUS_FLAG_SRC_SPH:
  242. *src = SND_MOTU_CLOCK_SOURCE_SPH;
  243. break;
  244. case CLK_828_STATUS_FLAG_SRC_SPDIF:
  245. {
  246. if (data & CLK_828_STATUS_FLAG_OPT_IN_IFACE_IS_SPDIF)
  247. *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_COAX;
  248. else
  249. *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT;
  250. break;
  251. }
  252. case CLK_828_STATUS_FLAG_SRC_ADAT_ON_DSUB:
  253. *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_DSUB;
  254. break;
  255. case CLK_828_STATUS_FLAG_SRC_INTERNAL:
  256. *src = SND_MOTU_CLOCK_SOURCE_INTERNAL;
  257. break;
  258. default:
  259. return -ENXIO;
  260. }
  261. return 0;
  262. }
  263. static int get_clock_source_896(struct snd_motu *motu, enum snd_motu_clock_source *src)
  264. {
  265. __be32 reg;
  266. u32 data;
  267. int err;
  268. err = snd_motu_transaction_read(motu, CLK_896_STATUS_OFFSET, &reg, sizeof(reg));
  269. if (err < 0)
  270. return err;
  271. data = be32_to_cpu(reg);
  272. switch (data & CLK_896_STATUS_MASK_SRC) {
  273. case CLK_896_STATUS_FLAG_SRC_INTERNAL:
  274. *src = SND_MOTU_CLOCK_SOURCE_INTERNAL;
  275. break;
  276. case CLK_896_STATUS_FLAG_SRC_ADAT_ON_OPT:
  277. *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT;
  278. break;
  279. case CLK_896_STATUS_FLAG_SRC_AESEBU:
  280. *src = SND_MOTU_CLOCK_SOURCE_AESEBU_ON_XLR;
  281. break;
  282. case CLK_896_STATUS_FLAG_SRC_SPH:
  283. *src = SND_MOTU_CLOCK_SOURCE_SPH;
  284. break;
  285. case CLK_896_STATUS_FLAG_SRC_WORD:
  286. *src = SND_MOTU_CLOCK_SOURCE_WORD_ON_BNC;
  287. break;
  288. case CLK_896_STATUS_FLAG_SRC_ADAT_ON_DSUB:
  289. *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_DSUB;
  290. break;
  291. default:
  292. return -ENXIO;
  293. }
  294. return 0;
  295. }
  296. int snd_motu_protocol_v1_get_clock_source(struct snd_motu *motu, enum snd_motu_clock_source *src)
  297. {
  298. if (motu->spec == &snd_motu_spec_828)
  299. return get_clock_source_828(motu, src);
  300. else if (motu->spec == &snd_motu_spec_896)
  301. return get_clock_source_896(motu, src);
  302. else
  303. return -ENXIO;
  304. }
  305. static int switch_fetching_mode_828(struct snd_motu *motu, bool enable)
  306. {
  307. __be32 reg;
  308. u32 data;
  309. int err;
  310. err = snd_motu_transaction_read(motu, CLK_828_STATUS_OFFSET, &reg, sizeof(reg));
  311. if (err < 0)
  312. return err;
  313. data = be32_to_cpu(reg) & CLK_828_STATUS_MASK;
  314. data &= ~(CLK_828_STATUS_FLAG_FETCH_PCM_FRAMES | CLK_828_STATUS_FLAG_ENABLE_OUTPUT);
  315. if (enable) {
  316. // This transaction should be initiated after the device receives batch of packets
  317. // since the device voluntarily mutes outputs. As a workaround, yield processor over
  318. // 100 msec.
  319. msleep(100);
  320. data |= CLK_828_STATUS_FLAG_FETCH_PCM_FRAMES | CLK_828_STATUS_FLAG_ENABLE_OUTPUT;
  321. }
  322. reg = cpu_to_be32(data);
  323. return snd_motu_transaction_write(motu, CLK_828_STATUS_OFFSET, &reg, sizeof(reg));
  324. }
  325. static int switch_fetching_mode_896(struct snd_motu *motu, bool enable)
  326. {
  327. __be32 reg;
  328. u32 data;
  329. int err;
  330. err = snd_motu_transaction_read(motu, CLK_896_STATUS_OFFSET, &reg, sizeof(reg));
  331. if (err < 0)
  332. return err;
  333. data = be32_to_cpu(reg);
  334. data &= ~CLK_896_STATUS_FLAG_FETCH_ENABLE;
  335. if (enable)
  336. data |= CLK_896_STATUS_FLAG_FETCH_ENABLE | CLK_896_STATUS_FLAG_OUTPUT_ON;
  337. reg = cpu_to_be32(data);
  338. return snd_motu_transaction_write(motu, CLK_896_STATUS_OFFSET, &reg, sizeof(reg));
  339. }
  340. int snd_motu_protocol_v1_switch_fetching_mode(struct snd_motu *motu, bool enable)
  341. {
  342. if (motu->spec == &snd_motu_spec_828)
  343. return switch_fetching_mode_828(motu, enable);
  344. else if (motu->spec == &snd_motu_spec_896)
  345. return switch_fetching_mode_896(motu, enable);
  346. else
  347. return -ENXIO;
  348. }
  349. static int detect_packet_formats_828(struct snd_motu *motu)
  350. {
  351. __be32 reg;
  352. u32 data;
  353. int err;
  354. motu->tx_packet_formats.pcm_byte_offset = 4;
  355. motu->tx_packet_formats.msg_chunks = 2;
  356. motu->rx_packet_formats.pcm_byte_offset = 4;
  357. motu->rx_packet_formats.msg_chunks = 0;
  358. err = snd_motu_transaction_read(motu, CLK_828_STATUS_OFFSET, &reg, sizeof(reg));
  359. if (err < 0)
  360. return err;
  361. data = be32_to_cpu(reg) & CLK_828_STATUS_MASK;
  362. // The number of chunks is just reduced when SPDIF is activated.
  363. if (!(data & CLK_828_STATUS_FLAG_OPT_IN_IFACE_IS_SPDIF))
  364. motu->tx_packet_formats.pcm_chunks[0] += 8;
  365. if (!(data & CLK_828_STATUS_FLAG_OPT_OUT_IFACE_IS_SPDIF))
  366. motu->rx_packet_formats.pcm_chunks[0] += 8;
  367. return 0;
  368. }
  369. static int detect_packet_formats_896(struct snd_motu *motu)
  370. {
  371. // 24bit PCM frames follow to source packet header without message chunk.
  372. motu->tx_packet_formats.pcm_byte_offset = 4;
  373. motu->rx_packet_formats.pcm_byte_offset = 4;
  374. // No message chunk in data block.
  375. motu->tx_packet_formats.msg_chunks = 0;
  376. motu->rx_packet_formats.msg_chunks = 0;
  377. // Always enable optical interface for ADAT signal since the device have no registers
  378. // to refer to current configuration.
  379. motu->tx_packet_formats.pcm_chunks[0] += 8;
  380. motu->tx_packet_formats.pcm_chunks[1] += 8;
  381. motu->rx_packet_formats.pcm_chunks[0] += 8;
  382. motu->rx_packet_formats.pcm_chunks[1] += 8;
  383. return 0;
  384. }
  385. int snd_motu_protocol_v1_cache_packet_formats(struct snd_motu *motu)
  386. {
  387. memcpy(motu->tx_packet_formats.pcm_chunks, motu->spec->tx_fixed_pcm_chunks,
  388. sizeof(motu->tx_packet_formats.pcm_chunks));
  389. memcpy(motu->rx_packet_formats.pcm_chunks, motu->spec->rx_fixed_pcm_chunks,
  390. sizeof(motu->rx_packet_formats.pcm_chunks));
  391. if (motu->spec == &snd_motu_spec_828)
  392. return detect_packet_formats_828(motu);
  393. else if (motu->spec == &snd_motu_spec_896)
  394. return detect_packet_formats_896(motu);
  395. else
  396. return 0;
  397. }
  398. const struct snd_motu_spec snd_motu_spec_828 = {
  399. .name = "828",
  400. .protocol_version = SND_MOTU_PROTOCOL_V1,
  401. .tx_fixed_pcm_chunks = {10, 0, 0},
  402. .rx_fixed_pcm_chunks = {10, 0, 0},
  403. };
  404. const struct snd_motu_spec snd_motu_spec_896 = {
  405. .name = "896",
  406. .tx_fixed_pcm_chunks = {10, 10, 0},
  407. .rx_fixed_pcm_chunks = {10, 10, 0},
  408. };