bebob_focusrite.c 8.7 KB

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