bebob_focusrite.c 8.6 KB

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