bebob_focusrite.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * bebob_focusrite.c - a part of driver for BeBoB based devices
  3. *
  4. * Copyright (c) 2013-2014 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include "./bebob.h"
  9. #define ANA_IN "Analog In"
  10. #define DIG_IN "Digital In"
  11. #define ANA_OUT "Analog Out"
  12. #define DIG_OUT "Digital Out"
  13. #define STM_IN "Stream In"
  14. #define SAFFIRE_ADDRESS_BASE 0x000100000000ULL
  15. #define SAFFIRE_OFFSET_CLOCK_SOURCE 0x00f8
  16. #define SAFFIREPRO_OFFSET_CLOCK_SOURCE 0x0174
  17. /* whether sync to external device or not */
  18. #define SAFFIRE_OFFSET_CLOCK_SYNC_EXT 0x013c
  19. #define SAFFIRE_LE_OFFSET_CLOCK_SYNC_EXT 0x0432
  20. #define SAFFIREPRO_OFFSET_CLOCK_SYNC_EXT 0x0164
  21. #define SAFFIRE_CLOCK_SOURCE_INTERNAL 0
  22. #define SAFFIRE_CLOCK_SOURCE_SPDIF 1
  23. /* '1' is absent, why... */
  24. #define SAFFIREPRO_CLOCK_SOURCE_INTERNAL 0
  25. #define SAFFIREPRO_CLOCK_SOURCE_SPDIF 2
  26. #define SAFFIREPRO_CLOCK_SOURCE_ADAT1 3
  27. #define SAFFIREPRO_CLOCK_SOURCE_ADAT2 4
  28. #define SAFFIREPRO_CLOCK_SOURCE_WORDCLOCK 5
  29. /* S/PDIF, ADAT1, ADAT2 is enabled or not. three quadlets */
  30. #define SAFFIREPRO_ENABLE_DIG_IFACES 0x01a4
  31. /* saffirepro has its own parameter for sampling frequency */
  32. #define SAFFIREPRO_RATE_NOREBOOT 0x01cc
  33. /* index is the value for this register */
  34. static const unsigned int rates[] = {
  35. [0] = 0,
  36. [1] = 44100,
  37. [2] = 48000,
  38. [3] = 88200,
  39. [4] = 96000,
  40. [5] = 176400,
  41. [6] = 192000
  42. };
  43. /* saffire(no label)/saffire LE has metering */
  44. #define SAFFIRE_OFFSET_METER 0x0100
  45. #define SAFFIRE_LE_OFFSET_METER 0x0168
  46. static inline int
  47. saffire_read_block(struct snd_bebob *bebob, u64 offset,
  48. u32 *buf, unsigned int size)
  49. {
  50. unsigned int i;
  51. int err;
  52. __be32 *tmp = (__be32 *)buf;
  53. err = snd_fw_transaction(bebob->unit, TCODE_READ_BLOCK_REQUEST,
  54. SAFFIRE_ADDRESS_BASE + offset,
  55. tmp, size, 0);
  56. if (err < 0)
  57. goto end;
  58. for (i = 0; i < size / sizeof(u32); i++)
  59. buf[i] = be32_to_cpu(tmp[i]);
  60. end:
  61. return err;
  62. }
  63. static inline int
  64. saffire_read_quad(struct snd_bebob *bebob, u64 offset, u32 *value)
  65. {
  66. int err;
  67. __be32 tmp;
  68. err = snd_fw_transaction(bebob->unit, TCODE_READ_QUADLET_REQUEST,
  69. SAFFIRE_ADDRESS_BASE + offset,
  70. &tmp, sizeof(__be32), 0);
  71. if (err < 0)
  72. goto end;
  73. *value = be32_to_cpu(tmp);
  74. end:
  75. return err;
  76. }
  77. static inline int
  78. saffire_write_quad(struct snd_bebob *bebob, u64 offset, u32 value)
  79. {
  80. __be32 data = cpu_to_be32(value);
  81. return snd_fw_transaction(bebob->unit, TCODE_WRITE_QUADLET_REQUEST,
  82. SAFFIRE_ADDRESS_BASE + offset,
  83. &data, sizeof(__be32), 0);
  84. }
  85. static char *const saffirepro_26_clk_src_labels[] = {
  86. SND_BEBOB_CLOCK_INTERNAL, "S/PDIF", "ADAT1", "ADAT2", "Word Clock"
  87. };
  88. static char *const saffirepro_10_clk_src_labels[] = {
  89. SND_BEBOB_CLOCK_INTERNAL, "S/PDIF", "Word Clock"
  90. };
  91. static int
  92. saffirepro_both_clk_freq_get(struct snd_bebob *bebob, unsigned int *rate)
  93. {
  94. u32 id;
  95. int err;
  96. err = saffire_read_quad(bebob, SAFFIREPRO_RATE_NOREBOOT, &id);
  97. if (err < 0)
  98. goto end;
  99. if (id >= ARRAY_SIZE(rates))
  100. err = -EIO;
  101. else
  102. *rate = rates[id];
  103. end:
  104. return err;
  105. }
  106. static int
  107. saffirepro_both_clk_freq_set(struct snd_bebob *bebob, unsigned int rate)
  108. {
  109. u32 id;
  110. for (id = 0; id < ARRAY_SIZE(rates); id++) {
  111. if (rates[id] == rate)
  112. break;
  113. }
  114. if (id == ARRAY_SIZE(rates))
  115. return -EINVAL;
  116. return saffire_write_quad(bebob, SAFFIREPRO_RATE_NOREBOOT, id);
  117. }
  118. static int
  119. saffirepro_both_clk_src_get(struct snd_bebob *bebob, unsigned int *id)
  120. {
  121. int err;
  122. u32 value;
  123. err = saffire_read_quad(bebob, SAFFIREPRO_OFFSET_CLOCK_SOURCE, &value);
  124. if (err < 0)
  125. goto end;
  126. if (bebob->spec->clock->labels == saffirepro_10_clk_src_labels) {
  127. if (value == SAFFIREPRO_CLOCK_SOURCE_WORDCLOCK)
  128. *id = 2;
  129. else if (value == SAFFIREPRO_CLOCK_SOURCE_SPDIF)
  130. *id = 1;
  131. } else if (value > 1) {
  132. *id = value - 1;
  133. }
  134. end:
  135. return err;
  136. }
  137. struct snd_bebob_spec saffire_le_spec;
  138. static char *const saffire_both_clk_src_labels[] = {
  139. SND_BEBOB_CLOCK_INTERNAL, "S/PDIF"
  140. };
  141. static int
  142. saffire_both_clk_src_get(struct snd_bebob *bebob, unsigned int *id)
  143. {
  144. int err;
  145. u32 value;
  146. err = saffire_read_quad(bebob, SAFFIRE_OFFSET_CLOCK_SOURCE, &value);
  147. if (err >= 0)
  148. *id = 0xff & value;
  149. return err;
  150. };
  151. static char *const saffire_le_meter_labels[] = {
  152. ANA_IN, ANA_IN, DIG_IN,
  153. ANA_OUT, ANA_OUT, ANA_OUT, ANA_OUT,
  154. STM_IN, STM_IN
  155. };
  156. static char *const saffire_meter_labels[] = {
  157. ANA_IN, ANA_IN,
  158. STM_IN, STM_IN, STM_IN, STM_IN, STM_IN,
  159. };
  160. static int
  161. saffire_meter_get(struct snd_bebob *bebob, u32 *buf, unsigned int size)
  162. {
  163. struct snd_bebob_meter_spec *spec = bebob->spec->meter;
  164. unsigned int channels;
  165. u64 offset;
  166. int err;
  167. if (spec->labels == saffire_le_meter_labels)
  168. offset = SAFFIRE_LE_OFFSET_METER;
  169. else
  170. offset = SAFFIRE_OFFSET_METER;
  171. channels = spec->num * 2;
  172. if (size < channels * sizeof(u32))
  173. return -EIO;
  174. err = saffire_read_block(bebob, offset, buf, size);
  175. if (err >= 0 && spec->labels == saffire_le_meter_labels) {
  176. swap(buf[1], buf[3]);
  177. swap(buf[2], buf[3]);
  178. swap(buf[3], buf[4]);
  179. swap(buf[7], buf[10]);
  180. swap(buf[8], buf[10]);
  181. swap(buf[9], buf[11]);
  182. swap(buf[11], buf[12]);
  183. swap(buf[15], buf[16]);
  184. }
  185. return err;
  186. }
  187. static struct snd_bebob_rate_spec saffirepro_both_rate_spec = {
  188. .get = &saffirepro_both_clk_freq_get,
  189. .set = &saffirepro_both_clk_freq_set,
  190. };
  191. /* Saffire Pro 26 I/O */
  192. static struct snd_bebob_clock_spec saffirepro_26_clk_spec = {
  193. .num = ARRAY_SIZE(saffirepro_26_clk_src_labels),
  194. .labels = saffirepro_26_clk_src_labels,
  195. .get = &saffirepro_both_clk_src_get,
  196. };
  197. struct snd_bebob_spec saffirepro_26_spec = {
  198. .clock = &saffirepro_26_clk_spec,
  199. .rate = &saffirepro_both_rate_spec,
  200. .meter = NULL
  201. };
  202. /* Saffire Pro 10 I/O */
  203. static struct snd_bebob_clock_spec saffirepro_10_clk_spec = {
  204. .num = ARRAY_SIZE(saffirepro_10_clk_src_labels),
  205. .labels = saffirepro_10_clk_src_labels,
  206. .get = &saffirepro_both_clk_src_get,
  207. };
  208. struct snd_bebob_spec saffirepro_10_spec = {
  209. .clock = &saffirepro_10_clk_spec,
  210. .rate = &saffirepro_both_rate_spec,
  211. .meter = NULL
  212. };
  213. static struct snd_bebob_rate_spec saffire_both_rate_spec = {
  214. .get = &snd_bebob_stream_get_rate,
  215. .set = &snd_bebob_stream_set_rate,
  216. };
  217. static struct snd_bebob_clock_spec saffire_both_clk_spec = {
  218. .num = ARRAY_SIZE(saffire_both_clk_src_labels),
  219. .labels = saffire_both_clk_src_labels,
  220. .get = &saffire_both_clk_src_get,
  221. };
  222. /* Saffire LE */
  223. static struct snd_bebob_meter_spec saffire_le_meter_spec = {
  224. .num = ARRAY_SIZE(saffire_le_meter_labels),
  225. .labels = saffire_le_meter_labels,
  226. .get = &saffire_meter_get,
  227. };
  228. struct snd_bebob_spec saffire_le_spec = {
  229. .clock = &saffire_both_clk_spec,
  230. .rate = &saffire_both_rate_spec,
  231. .meter = &saffire_le_meter_spec
  232. };
  233. /* Saffire */
  234. static struct snd_bebob_meter_spec saffire_meter_spec = {
  235. .num = ARRAY_SIZE(saffire_meter_labels),
  236. .labels = saffire_meter_labels,
  237. .get = &saffire_meter_get,
  238. };
  239. struct snd_bebob_spec saffire_spec = {
  240. .clock = &saffire_both_clk_spec,
  241. .rate = &saffire_both_rate_spec,
  242. .meter = &saffire_meter_spec
  243. };