bebob_focusrite.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. /* clock sources as returned from register of Saffire Pro 10 and 26 */
  24. #define SAFFIREPRO_CLOCK_SOURCE_INTERNAL 0
  25. #define SAFFIREPRO_CLOCK_SOURCE_SKIP 1 /* never used on hardware */
  26. #define SAFFIREPRO_CLOCK_SOURCE_SPDIF 2
  27. #define SAFFIREPRO_CLOCK_SOURCE_ADAT1 3 /* not used on s.pro. 10 */
  28. #define SAFFIREPRO_CLOCK_SOURCE_ADAT2 4 /* not used on s.pro. 10 */
  29. #define SAFFIREPRO_CLOCK_SOURCE_WORDCLOCK 5
  30. #define SAFFIREPRO_CLOCK_SOURCE_COUNT 6
  31. /* S/PDIF, ADAT1, ADAT2 is enabled or not. three quadlets */
  32. #define SAFFIREPRO_ENABLE_DIG_IFACES 0x01a4
  33. /* saffirepro has its own parameter for sampling frequency */
  34. #define SAFFIREPRO_RATE_NOREBOOT 0x01cc
  35. /* index is the value for this register */
  36. static const unsigned int rates[] = {
  37. [0] = 0,
  38. [1] = 44100,
  39. [2] = 48000,
  40. [3] = 88200,
  41. [4] = 96000,
  42. [5] = 176400,
  43. [6] = 192000
  44. };
  45. /* saffire(no label)/saffire LE has metering */
  46. #define SAFFIRE_OFFSET_METER 0x0100
  47. #define SAFFIRE_LE_OFFSET_METER 0x0168
  48. static inline int
  49. saffire_read_block(struct snd_bebob *bebob, u64 offset,
  50. u32 *buf, unsigned int size)
  51. {
  52. unsigned int i;
  53. int err;
  54. __be32 *tmp = (__be32 *)buf;
  55. err = snd_fw_transaction(bebob->unit, TCODE_READ_BLOCK_REQUEST,
  56. SAFFIRE_ADDRESS_BASE + offset,
  57. tmp, size, 0);
  58. if (err < 0)
  59. goto end;
  60. for (i = 0; i < size / sizeof(u32); i++)
  61. buf[i] = be32_to_cpu(tmp[i]);
  62. end:
  63. return err;
  64. }
  65. static inline int
  66. saffire_read_quad(struct snd_bebob *bebob, u64 offset, u32 *value)
  67. {
  68. int err;
  69. __be32 tmp;
  70. err = snd_fw_transaction(bebob->unit, TCODE_READ_QUADLET_REQUEST,
  71. SAFFIRE_ADDRESS_BASE + offset,
  72. &tmp, sizeof(__be32), 0);
  73. if (err < 0)
  74. goto end;
  75. *value = be32_to_cpu(tmp);
  76. end:
  77. return err;
  78. }
  79. static inline int
  80. saffire_write_quad(struct snd_bebob *bebob, u64 offset, u32 value)
  81. {
  82. __be32 data = cpu_to_be32(value);
  83. return snd_fw_transaction(bebob->unit, TCODE_WRITE_QUADLET_REQUEST,
  84. SAFFIRE_ADDRESS_BASE + offset,
  85. &data, sizeof(__be32), 0);
  86. }
  87. static const char *const saffirepro_10_clk_src_labels[] = {
  88. SND_BEBOB_CLOCK_INTERNAL, "S/PDIF", "Word Clock"
  89. };
  90. static const char *const saffirepro_26_clk_src_labels[] = {
  91. SND_BEBOB_CLOCK_INTERNAL, "S/PDIF", "ADAT1", "ADAT2", "Word Clock"
  92. };
  93. /* Value maps between registers and labels for SaffirePro 10/26. */
  94. static const signed char saffirepro_clk_maps[][SAFFIREPRO_CLOCK_SOURCE_COUNT] = {
  95. /* SaffirePro 10 */
  96. [0] = {
  97. [SAFFIREPRO_CLOCK_SOURCE_INTERNAL] = 0,
  98. [SAFFIREPRO_CLOCK_SOURCE_SKIP] = -1, /* not supported */
  99. [SAFFIREPRO_CLOCK_SOURCE_SPDIF] = 1,
  100. [SAFFIREPRO_CLOCK_SOURCE_ADAT1] = -1, /* not supported */
  101. [SAFFIREPRO_CLOCK_SOURCE_ADAT2] = -1, /* not supported */
  102. [SAFFIREPRO_CLOCK_SOURCE_WORDCLOCK] = 2,
  103. },
  104. /* SaffirePro 26 */
  105. [1] = {
  106. [SAFFIREPRO_CLOCK_SOURCE_INTERNAL] = 0,
  107. [SAFFIREPRO_CLOCK_SOURCE_SKIP] = -1, /* not supported */
  108. [SAFFIREPRO_CLOCK_SOURCE_SPDIF] = 1,
  109. [SAFFIREPRO_CLOCK_SOURCE_ADAT1] = 2,
  110. [SAFFIREPRO_CLOCK_SOURCE_ADAT2] = 3,
  111. [SAFFIREPRO_CLOCK_SOURCE_WORDCLOCK] = 4,
  112. }
  113. };
  114. static int
  115. saffirepro_both_clk_freq_get(struct snd_bebob *bebob, unsigned int *rate)
  116. {
  117. u32 id;
  118. int err;
  119. err = saffire_read_quad(bebob, SAFFIREPRO_RATE_NOREBOOT, &id);
  120. if (err < 0)
  121. goto end;
  122. if (id >= ARRAY_SIZE(rates))
  123. err = -EIO;
  124. else
  125. *rate = rates[id];
  126. end:
  127. return err;
  128. }
  129. static int
  130. saffirepro_both_clk_freq_set(struct snd_bebob *bebob, unsigned int rate)
  131. {
  132. u32 id;
  133. for (id = 0; id < ARRAY_SIZE(rates); id++) {
  134. if (rates[id] == rate)
  135. break;
  136. }
  137. if (id == ARRAY_SIZE(rates))
  138. return -EINVAL;
  139. return saffire_write_quad(bebob, SAFFIREPRO_RATE_NOREBOOT, id);
  140. }
  141. /*
  142. * query hardware for current clock source, return our internally
  143. * used clock index in *id, depending on hardware.
  144. */
  145. static int
  146. saffirepro_both_clk_src_get(struct snd_bebob *bebob, unsigned int *id)
  147. {
  148. int err;
  149. u32 value; /* clock source read from hw register */
  150. const signed char *map;
  151. err = saffire_read_quad(bebob, SAFFIREPRO_OFFSET_CLOCK_SOURCE, &value);
  152. if (err < 0)
  153. goto end;
  154. /* depending on hardware, use a different mapping */
  155. if (bebob->spec->clock->labels == saffirepro_10_clk_src_labels)
  156. map = saffirepro_clk_maps[0];
  157. else
  158. map = saffirepro_clk_maps[1];
  159. /* In a case that this driver cannot handle the value of register. */
  160. if (value >= SAFFIREPRO_CLOCK_SOURCE_COUNT || map[value] < 0) {
  161. err = -EIO;
  162. goto end;
  163. }
  164. *id = (unsigned int)map[value];
  165. end:
  166. return err;
  167. }
  168. struct snd_bebob_spec saffire_le_spec;
  169. static const char *const saffire_both_clk_src_labels[] = {
  170. SND_BEBOB_CLOCK_INTERNAL, "S/PDIF"
  171. };
  172. static int
  173. saffire_both_clk_src_get(struct snd_bebob *bebob, unsigned int *id)
  174. {
  175. int err;
  176. u32 value;
  177. err = saffire_read_quad(bebob, SAFFIRE_OFFSET_CLOCK_SOURCE, &value);
  178. if (err >= 0)
  179. *id = 0xff & value;
  180. return err;
  181. };
  182. static const char *const saffire_le_meter_labels[] = {
  183. ANA_IN, ANA_IN, DIG_IN,
  184. ANA_OUT, ANA_OUT, ANA_OUT, ANA_OUT,
  185. STM_IN, STM_IN
  186. };
  187. static const char *const saffire_meter_labels[] = {
  188. ANA_IN, ANA_IN,
  189. STM_IN, STM_IN, STM_IN, STM_IN, STM_IN,
  190. };
  191. static int
  192. saffire_meter_get(struct snd_bebob *bebob, u32 *buf, unsigned int size)
  193. {
  194. struct snd_bebob_meter_spec *spec = bebob->spec->meter;
  195. unsigned int channels;
  196. u64 offset;
  197. int err;
  198. if (spec->labels == saffire_le_meter_labels)
  199. offset = SAFFIRE_LE_OFFSET_METER;
  200. else
  201. offset = SAFFIRE_OFFSET_METER;
  202. channels = spec->num * 2;
  203. if (size < channels * sizeof(u32))
  204. return -EIO;
  205. err = saffire_read_block(bebob, offset, buf, size);
  206. if (err >= 0 && spec->labels == saffire_le_meter_labels) {
  207. swap(buf[1], buf[3]);
  208. swap(buf[2], buf[3]);
  209. swap(buf[3], buf[4]);
  210. swap(buf[7], buf[10]);
  211. swap(buf[8], buf[10]);
  212. swap(buf[9], buf[11]);
  213. swap(buf[11], buf[12]);
  214. swap(buf[15], buf[16]);
  215. }
  216. return err;
  217. }
  218. static struct snd_bebob_rate_spec saffirepro_both_rate_spec = {
  219. .get = &saffirepro_both_clk_freq_get,
  220. .set = &saffirepro_both_clk_freq_set,
  221. };
  222. /* Saffire Pro 26 I/O */
  223. static struct snd_bebob_clock_spec saffirepro_26_clk_spec = {
  224. .num = ARRAY_SIZE(saffirepro_26_clk_src_labels),
  225. .labels = saffirepro_26_clk_src_labels,
  226. .get = &saffirepro_both_clk_src_get,
  227. };
  228. struct snd_bebob_spec saffirepro_26_spec = {
  229. .clock = &saffirepro_26_clk_spec,
  230. .rate = &saffirepro_both_rate_spec,
  231. .meter = NULL
  232. };
  233. /* Saffire Pro 10 I/O */
  234. static struct snd_bebob_clock_spec saffirepro_10_clk_spec = {
  235. .num = ARRAY_SIZE(saffirepro_10_clk_src_labels),
  236. .labels = saffirepro_10_clk_src_labels,
  237. .get = &saffirepro_both_clk_src_get,
  238. };
  239. struct snd_bebob_spec saffirepro_10_spec = {
  240. .clock = &saffirepro_10_clk_spec,
  241. .rate = &saffirepro_both_rate_spec,
  242. .meter = NULL
  243. };
  244. static struct snd_bebob_rate_spec saffire_both_rate_spec = {
  245. .get = &snd_bebob_stream_get_rate,
  246. .set = &snd_bebob_stream_set_rate,
  247. };
  248. static struct snd_bebob_clock_spec saffire_both_clk_spec = {
  249. .num = ARRAY_SIZE(saffire_both_clk_src_labels),
  250. .labels = saffire_both_clk_src_labels,
  251. .get = &saffire_both_clk_src_get,
  252. };
  253. /* Saffire LE */
  254. static struct snd_bebob_meter_spec saffire_le_meter_spec = {
  255. .num = ARRAY_SIZE(saffire_le_meter_labels),
  256. .labels = saffire_le_meter_labels,
  257. .get = &saffire_meter_get,
  258. };
  259. struct snd_bebob_spec saffire_le_spec = {
  260. .clock = &saffire_both_clk_spec,
  261. .rate = &saffire_both_rate_spec,
  262. .meter = &saffire_le_meter_spec
  263. };
  264. /* Saffire */
  265. static struct snd_bebob_meter_spec saffire_meter_spec = {
  266. .num = ARRAY_SIZE(saffire_meter_labels),
  267. .labels = saffire_meter_labels,
  268. .get = &saffire_meter_get,
  269. };
  270. struct snd_bebob_spec saffire_spec = {
  271. .clock = &saffire_both_clk_spec,
  272. .rate = &saffire_both_rate_spec,
  273. .meter = &saffire_meter_spec
  274. };