ff-protocol-ff400.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * ff-protocol-ff400.c - a part of driver for RME Fireface series
  3. *
  4. * Copyright (c) 2015-2017 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include <linux/delay.h>
  9. #include "ff.h"
  10. #define FF400_STF 0x000080100500ull
  11. #define FF400_RX_PACKET_FORMAT 0x000080100504ull
  12. #define FF400_ISOC_COMM_START 0x000080100508ull
  13. #define FF400_TX_PACKET_FORMAT 0x00008010050cull
  14. #define FF400_ISOC_COMM_STOP 0x000080100510ull
  15. #define FF400_SYNC_STATUS 0x0000801c0000ull
  16. #define FF400_FETCH_PCM_FRAMES 0x0000801c0000ull /* For block request. */
  17. #define FF400_CLOCK_CONFIG 0x0000801c0004ull
  18. #define FF400_MIDI_HIGH_ADDR 0x0000801003f4ull
  19. #define FF400_MIDI_RX_PORT_0 0x000080180000ull
  20. #define FF400_MIDI_RX_PORT_1 0x000080190000ull
  21. static int ff400_get_clock(struct snd_ff *ff, unsigned int *rate,
  22. enum snd_ff_clock_src *src)
  23. {
  24. __le32 reg;
  25. u32 data;
  26. int err;
  27. err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
  28. FF400_CLOCK_CONFIG, &reg, sizeof(reg), 0);
  29. if (err < 0)
  30. return err;
  31. data = le32_to_cpu(reg);
  32. /* Calculate sampling rate. */
  33. switch ((data >> 1) & 0x03) {
  34. case 0x01:
  35. *rate = 32000;
  36. break;
  37. case 0x00:
  38. *rate = 44100;
  39. break;
  40. case 0x03:
  41. *rate = 48000;
  42. break;
  43. case 0x02:
  44. default:
  45. return -EIO;
  46. }
  47. if (data & 0x08)
  48. *rate *= 2;
  49. else if (data & 0x10)
  50. *rate *= 4;
  51. /* Calculate source of clock. */
  52. if (data & 0x01) {
  53. *src = SND_FF_CLOCK_SRC_INTERNAL;
  54. } else {
  55. /* TODO: 0x00, 0x01, 0x02, 0x06, 0x07? */
  56. switch ((data >> 10) & 0x07) {
  57. case 0x03:
  58. *src = SND_FF_CLOCK_SRC_SPDIF;
  59. break;
  60. case 0x04:
  61. *src = SND_FF_CLOCK_SRC_WORD;
  62. break;
  63. case 0x05:
  64. *src = SND_FF_CLOCK_SRC_LTC;
  65. break;
  66. case 0x00:
  67. default:
  68. *src = SND_FF_CLOCK_SRC_ADAT;
  69. break;
  70. }
  71. }
  72. return 0;
  73. }
  74. static int ff400_begin_session(struct snd_ff *ff, unsigned int rate)
  75. {
  76. __le32 reg;
  77. int i, err;
  78. /* Check whether the given value is supported or not. */
  79. for (i = 0; i < CIP_SFC_COUNT; i++) {
  80. if (amdtp_rate_table[i] == rate)
  81. break;
  82. }
  83. if (i == CIP_SFC_COUNT)
  84. return -EINVAL;
  85. /* Set the number of data blocks transferred in a second. */
  86. reg = cpu_to_le32(rate);
  87. err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  88. FF400_STF, &reg, sizeof(reg), 0);
  89. if (err < 0)
  90. return err;
  91. msleep(100);
  92. /*
  93. * Set isochronous channel and the number of quadlets of received
  94. * packets.
  95. */
  96. reg = cpu_to_le32(((ff->rx_stream.data_block_quadlets << 3) << 8) |
  97. ff->rx_resources.channel);
  98. err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  99. FF400_RX_PACKET_FORMAT, &reg, sizeof(reg), 0);
  100. if (err < 0)
  101. return err;
  102. /*
  103. * Set isochronous channel and the number of quadlets of transmitted
  104. * packet.
  105. */
  106. /* TODO: investigate the purpose of this 0x80. */
  107. reg = cpu_to_le32((0x80 << 24) |
  108. (ff->tx_resources.channel << 5) |
  109. (ff->tx_stream.data_block_quadlets));
  110. err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  111. FF400_TX_PACKET_FORMAT, &reg, sizeof(reg), 0);
  112. if (err < 0)
  113. return err;
  114. /* Allow to transmit packets. */
  115. reg = cpu_to_le32(0x00000001);
  116. return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  117. FF400_ISOC_COMM_START, &reg, sizeof(reg), 0);
  118. }
  119. static void ff400_finish_session(struct snd_ff *ff)
  120. {
  121. __le32 reg;
  122. reg = cpu_to_le32(0x80000000);
  123. snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  124. FF400_ISOC_COMM_STOP, &reg, sizeof(reg), 0);
  125. }
  126. static int ff400_switch_fetching_mode(struct snd_ff *ff, bool enable)
  127. {
  128. __le32 *reg;
  129. int i;
  130. int err;
  131. reg = kcalloc(18, sizeof(__le32), GFP_KERNEL);
  132. if (reg == NULL)
  133. return -ENOMEM;
  134. if (!enable) {
  135. /*
  136. * Each quadlet is corresponding to data channels in a data
  137. * blocks in reverse order. Precisely, quadlets for available
  138. * data channels should be enabled. Here, I take second best
  139. * to fetch PCM frames from all of data channels regardless of
  140. * stf.
  141. */
  142. for (i = 0; i < 18; ++i)
  143. reg[i] = cpu_to_le32(0x00000001);
  144. }
  145. err = snd_fw_transaction(ff->unit, TCODE_WRITE_BLOCK_REQUEST,
  146. FF400_FETCH_PCM_FRAMES, reg,
  147. sizeof(__le32) * 18, 0);
  148. kfree(reg);
  149. return err;
  150. }
  151. static void ff400_dump_sync_status(struct snd_ff *ff,
  152. struct snd_info_buffer *buffer)
  153. {
  154. __le32 reg;
  155. u32 data;
  156. int err;
  157. err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
  158. FF400_SYNC_STATUS, &reg, sizeof(reg), 0);
  159. if (err < 0)
  160. return;
  161. data = le32_to_cpu(reg);
  162. snd_iprintf(buffer, "External source detection:\n");
  163. snd_iprintf(buffer, "Word Clock:");
  164. if ((data >> 24) & 0x20) {
  165. if ((data >> 24) & 0x40)
  166. snd_iprintf(buffer, "sync\n");
  167. else
  168. snd_iprintf(buffer, "lock\n");
  169. } else {
  170. snd_iprintf(buffer, "none\n");
  171. }
  172. snd_iprintf(buffer, "S/PDIF:");
  173. if ((data >> 16) & 0x10) {
  174. if ((data >> 16) & 0x04)
  175. snd_iprintf(buffer, "sync\n");
  176. else
  177. snd_iprintf(buffer, "lock\n");
  178. } else {
  179. snd_iprintf(buffer, "none\n");
  180. }
  181. snd_iprintf(buffer, "ADAT:");
  182. if ((data >> 8) & 0x04) {
  183. if ((data >> 8) & 0x10)
  184. snd_iprintf(buffer, "sync\n");
  185. else
  186. snd_iprintf(buffer, "lock\n");
  187. } else {
  188. snd_iprintf(buffer, "none\n");
  189. }
  190. snd_iprintf(buffer, "\nUsed external source:\n");
  191. if (((data >> 22) & 0x07) == 0x07) {
  192. snd_iprintf(buffer, "None\n");
  193. } else {
  194. switch ((data >> 22) & 0x07) {
  195. case 0x00:
  196. snd_iprintf(buffer, "ADAT:");
  197. break;
  198. case 0x03:
  199. snd_iprintf(buffer, "S/PDIF:");
  200. break;
  201. case 0x04:
  202. snd_iprintf(buffer, "Word:");
  203. break;
  204. case 0x07:
  205. snd_iprintf(buffer, "Nothing:");
  206. break;
  207. case 0x01:
  208. case 0x02:
  209. case 0x05:
  210. case 0x06:
  211. default:
  212. snd_iprintf(buffer, "unknown:");
  213. break;
  214. }
  215. if ((data >> 25) & 0x07) {
  216. switch ((data >> 25) & 0x07) {
  217. case 0x01:
  218. snd_iprintf(buffer, "32000\n");
  219. break;
  220. case 0x02:
  221. snd_iprintf(buffer, "44100\n");
  222. break;
  223. case 0x03:
  224. snd_iprintf(buffer, "48000\n");
  225. break;
  226. case 0x04:
  227. snd_iprintf(buffer, "64000\n");
  228. break;
  229. case 0x05:
  230. snd_iprintf(buffer, "88200\n");
  231. break;
  232. case 0x06:
  233. snd_iprintf(buffer, "96000\n");
  234. break;
  235. case 0x07:
  236. snd_iprintf(buffer, "128000\n");
  237. break;
  238. case 0x08:
  239. snd_iprintf(buffer, "176400\n");
  240. break;
  241. case 0x09:
  242. snd_iprintf(buffer, "192000\n");
  243. break;
  244. case 0x00:
  245. snd_iprintf(buffer, "unknown\n");
  246. break;
  247. }
  248. }
  249. }
  250. snd_iprintf(buffer, "Multiplied:");
  251. snd_iprintf(buffer, "%d\n", (data & 0x3ff) * 250);
  252. }
  253. static void ff400_dump_clock_config(struct snd_ff *ff,
  254. struct snd_info_buffer *buffer)
  255. {
  256. __le32 reg;
  257. u32 data;
  258. unsigned int rate;
  259. const char *src;
  260. int err;
  261. err = snd_fw_transaction(ff->unit, TCODE_READ_BLOCK_REQUEST,
  262. FF400_CLOCK_CONFIG, &reg, sizeof(reg), 0);
  263. if (err < 0)
  264. return;
  265. data = le32_to_cpu(reg);
  266. snd_iprintf(buffer, "Output S/PDIF format: %s (Emphasis: %s)\n",
  267. (data & 0x20) ? "Professional" : "Consumer",
  268. (data & 0x40) ? "on" : "off");
  269. snd_iprintf(buffer, "Optical output interface format: %s\n",
  270. ((data >> 8) & 0x01) ? "S/PDIF" : "ADAT");
  271. snd_iprintf(buffer, "Word output single speed: %s\n",
  272. ((data >> 8) & 0x20) ? "on" : "off");
  273. snd_iprintf(buffer, "S/PDIF input interface: %s\n",
  274. ((data >> 8) & 0x02) ? "Optical" : "Coaxial");
  275. switch ((data >> 1) & 0x03) {
  276. case 0x01:
  277. rate = 32000;
  278. break;
  279. case 0x00:
  280. rate = 44100;
  281. break;
  282. case 0x03:
  283. rate = 48000;
  284. break;
  285. case 0x02:
  286. default:
  287. return;
  288. }
  289. if (data & 0x08)
  290. rate *= 2;
  291. else if (data & 0x10)
  292. rate *= 4;
  293. snd_iprintf(buffer, "Sampling rate: %d\n", rate);
  294. if (data & 0x01) {
  295. src = "Internal";
  296. } else {
  297. switch ((data >> 10) & 0x07) {
  298. case 0x00:
  299. src = "ADAT";
  300. break;
  301. case 0x03:
  302. src = "S/PDIF";
  303. break;
  304. case 0x04:
  305. src = "Word";
  306. break;
  307. case 0x05:
  308. src = "LTC";
  309. break;
  310. default:
  311. return;
  312. }
  313. }
  314. snd_iprintf(buffer, "Sync to clock source: %s\n", src);
  315. }
  316. const struct snd_ff_protocol snd_ff_protocol_ff400 = {
  317. .get_clock = ff400_get_clock,
  318. .begin_session = ff400_begin_session,
  319. .finish_session = ff400_finish_session,
  320. .switch_fetching_mode = ff400_switch_fetching_mode,
  321. .dump_sync_status = ff400_dump_sync_status,
  322. .dump_clock_config = ff400_dump_clock_config,
  323. .midi_high_addr_reg = FF400_MIDI_HIGH_ADDR,
  324. .midi_rx_port_0_reg = FF400_MIDI_RX_PORT_0,
  325. .midi_rx_port_1_reg = FF400_MIDI_RX_PORT_1,
  326. };