bebob_focusrite.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 enum snd_bebob_clock_type saffirepro_10_clk_src_types[] = {
  88. SND_BEBOB_CLOCK_TYPE_INTERNAL,
  89. SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* S/PDIF */
  90. SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* Word Clock */
  91. };
  92. static enum snd_bebob_clock_type saffirepro_26_clk_src_types[] = {
  93. SND_BEBOB_CLOCK_TYPE_INTERNAL,
  94. SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* S/PDIF */
  95. SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* ADAT1 */
  96. SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* ADAT2 */
  97. SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* Word Clock */
  98. };
  99. /* Value maps between registers and labels for SaffirePro 10/26. */
  100. static const signed char saffirepro_clk_maps[][SAFFIREPRO_CLOCK_SOURCE_COUNT] = {
  101. /* SaffirePro 10 */
  102. [0] = {
  103. [SAFFIREPRO_CLOCK_SOURCE_INTERNAL] = 0,
  104. [SAFFIREPRO_CLOCK_SOURCE_SKIP] = -1, /* not supported */
  105. [SAFFIREPRO_CLOCK_SOURCE_SPDIF] = 1,
  106. [SAFFIREPRO_CLOCK_SOURCE_ADAT1] = -1, /* not supported */
  107. [SAFFIREPRO_CLOCK_SOURCE_ADAT2] = -1, /* not supported */
  108. [SAFFIREPRO_CLOCK_SOURCE_WORDCLOCK] = 2,
  109. },
  110. /* SaffirePro 26 */
  111. [1] = {
  112. [SAFFIREPRO_CLOCK_SOURCE_INTERNAL] = 0,
  113. [SAFFIREPRO_CLOCK_SOURCE_SKIP] = -1, /* not supported */
  114. [SAFFIREPRO_CLOCK_SOURCE_SPDIF] = 1,
  115. [SAFFIREPRO_CLOCK_SOURCE_ADAT1] = 2,
  116. [SAFFIREPRO_CLOCK_SOURCE_ADAT2] = 3,
  117. [SAFFIREPRO_CLOCK_SOURCE_WORDCLOCK] = 4,
  118. }
  119. };
  120. static int
  121. saffirepro_both_clk_freq_get(struct snd_bebob *bebob, unsigned int *rate)
  122. {
  123. u32 id;
  124. int err;
  125. err = saffire_read_quad(bebob, SAFFIREPRO_RATE_NOREBOOT, &id);
  126. if (err < 0)
  127. goto end;
  128. if (id >= ARRAY_SIZE(rates))
  129. err = -EIO;
  130. else
  131. *rate = rates[id];
  132. end:
  133. return err;
  134. }
  135. static int
  136. saffirepro_both_clk_freq_set(struct snd_bebob *bebob, unsigned int rate)
  137. {
  138. u32 id;
  139. for (id = 0; id < ARRAY_SIZE(rates); id++) {
  140. if (rates[id] == rate)
  141. break;
  142. }
  143. if (id == ARRAY_SIZE(rates))
  144. return -EINVAL;
  145. return saffire_write_quad(bebob, SAFFIREPRO_RATE_NOREBOOT, id);
  146. }
  147. /*
  148. * query hardware for current clock source, return our internally
  149. * used clock index in *id, depending on hardware.
  150. */
  151. static int
  152. saffirepro_both_clk_src_get(struct snd_bebob *bebob, unsigned int *id)
  153. {
  154. int err;
  155. u32 value; /* clock source read from hw register */
  156. const signed char *map;
  157. err = saffire_read_quad(bebob, SAFFIREPRO_OFFSET_CLOCK_SOURCE, &value);
  158. if (err < 0)
  159. goto end;
  160. /* depending on hardware, use a different mapping */
  161. if (bebob->spec->clock->types == saffirepro_10_clk_src_types)
  162. map = saffirepro_clk_maps[0];
  163. else
  164. map = saffirepro_clk_maps[1];
  165. /* In a case that this driver cannot handle the value of register. */
  166. if (value >= SAFFIREPRO_CLOCK_SOURCE_COUNT || map[value] < 0) {
  167. err = -EIO;
  168. goto end;
  169. }
  170. *id = (unsigned int)map[value];
  171. end:
  172. return err;
  173. }
  174. const struct snd_bebob_spec saffire_le_spec;
  175. static enum snd_bebob_clock_type saffire_both_clk_src_types[] = {
  176. SND_BEBOB_CLOCK_TYPE_INTERNAL,
  177. SND_BEBOB_CLOCK_TYPE_EXTERNAL,
  178. };
  179. static int
  180. saffire_both_clk_src_get(struct snd_bebob *bebob, unsigned int *id)
  181. {
  182. int err;
  183. u32 value;
  184. err = saffire_read_quad(bebob, SAFFIRE_OFFSET_CLOCK_SOURCE, &value);
  185. if (err >= 0)
  186. *id = 0xff & value;
  187. return err;
  188. };
  189. static const char *const saffire_le_meter_labels[] = {
  190. ANA_IN, ANA_IN, DIG_IN,
  191. ANA_OUT, ANA_OUT, ANA_OUT, ANA_OUT,
  192. STM_IN, STM_IN
  193. };
  194. static const char *const saffire_meter_labels[] = {
  195. ANA_IN, ANA_IN,
  196. STM_IN, STM_IN, STM_IN, STM_IN, STM_IN,
  197. };
  198. static int
  199. saffire_meter_get(struct snd_bebob *bebob, u32 *buf, unsigned int size)
  200. {
  201. const struct snd_bebob_meter_spec *spec = bebob->spec->meter;
  202. unsigned int channels;
  203. u64 offset;
  204. int err;
  205. if (spec->labels == saffire_le_meter_labels)
  206. offset = SAFFIRE_LE_OFFSET_METER;
  207. else
  208. offset = SAFFIRE_OFFSET_METER;
  209. channels = spec->num * 2;
  210. if (size < channels * sizeof(u32))
  211. return -EIO;
  212. err = saffire_read_block(bebob, offset, buf, size);
  213. if (err >= 0 && spec->labels == saffire_le_meter_labels) {
  214. swap(buf[1], buf[3]);
  215. swap(buf[2], buf[3]);
  216. swap(buf[3], buf[4]);
  217. swap(buf[7], buf[10]);
  218. swap(buf[8], buf[10]);
  219. swap(buf[9], buf[11]);
  220. swap(buf[11], buf[12]);
  221. swap(buf[15], buf[16]);
  222. }
  223. return err;
  224. }
  225. static const struct snd_bebob_rate_spec saffirepro_both_rate_spec = {
  226. .get = &saffirepro_both_clk_freq_get,
  227. .set = &saffirepro_both_clk_freq_set,
  228. };
  229. /* Saffire Pro 26 I/O */
  230. static const struct snd_bebob_clock_spec saffirepro_26_clk_spec = {
  231. .num = ARRAY_SIZE(saffirepro_26_clk_src_types),
  232. .types = saffirepro_26_clk_src_types,
  233. .get = &saffirepro_both_clk_src_get,
  234. };
  235. const struct snd_bebob_spec saffirepro_26_spec = {
  236. .clock = &saffirepro_26_clk_spec,
  237. .rate = &saffirepro_both_rate_spec,
  238. .meter = NULL
  239. };
  240. /* Saffire Pro 10 I/O */
  241. static const struct snd_bebob_clock_spec saffirepro_10_clk_spec = {
  242. .num = ARRAY_SIZE(saffirepro_10_clk_src_types),
  243. .types = saffirepro_10_clk_src_types,
  244. .get = &saffirepro_both_clk_src_get,
  245. };
  246. const struct snd_bebob_spec saffirepro_10_spec = {
  247. .clock = &saffirepro_10_clk_spec,
  248. .rate = &saffirepro_both_rate_spec,
  249. .meter = NULL
  250. };
  251. static const struct snd_bebob_rate_spec saffire_both_rate_spec = {
  252. .get = &snd_bebob_stream_get_rate,
  253. .set = &snd_bebob_stream_set_rate,
  254. };
  255. static const struct snd_bebob_clock_spec saffire_both_clk_spec = {
  256. .num = ARRAY_SIZE(saffire_both_clk_src_types),
  257. .types = saffire_both_clk_src_types,
  258. .get = &saffire_both_clk_src_get,
  259. };
  260. /* Saffire LE */
  261. static const struct snd_bebob_meter_spec saffire_le_meter_spec = {
  262. .num = ARRAY_SIZE(saffire_le_meter_labels),
  263. .labels = saffire_le_meter_labels,
  264. .get = &saffire_meter_get,
  265. };
  266. const struct snd_bebob_spec saffire_le_spec = {
  267. .clock = &saffire_both_clk_spec,
  268. .rate = &saffire_both_rate_spec,
  269. .meter = &saffire_le_meter_spec
  270. };
  271. /* Saffire */
  272. static const struct snd_bebob_meter_spec saffire_meter_spec = {
  273. .num = ARRAY_SIZE(saffire_meter_labels),
  274. .labels = saffire_meter_labels,
  275. .get = &saffire_meter_get,
  276. };
  277. const struct snd_bebob_spec saffire_spec = {
  278. .clock = &saffire_both_clk_spec,
  279. .rate = &saffire_both_rate_spec,
  280. .meter = &saffire_meter_spec
  281. };