blob: 45c8f76d81878c4313f7c515efe3d9568533a7ca [file] [log] [blame]
Takashi Sakamoto25784ec2014-04-25 22:45:24 +09001/*
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
9#include "./bebob.h"
10
11#define ANA_IN "Analog In"
12#define DIG_IN "Digital In"
13#define ANA_OUT "Analog Out"
14#define DIG_OUT "Digital Out"
15#define STM_IN "Stream In"
16
Takashi Sakamoto9b5f0ed2014-05-28 00:14:42 +090017#define SAFFIRE_ADDRESS_BASE 0x000100000000ULL
Takashi Sakamoto25784ec2014-04-25 22:45:24 +090018
Takashi Sakamoto9b5f0ed2014-05-28 00:14:42 +090019#define SAFFIRE_OFFSET_CLOCK_SOURCE 0x00f8
20#define SAFFIREPRO_OFFSET_CLOCK_SOURCE 0x0174
Takashi Sakamoto25784ec2014-04-25 22:45:24 +090021
22/* whether sync to external device or not */
Takashi Sakamoto9b5f0ed2014-05-28 00:14:42 +090023#define SAFFIRE_OFFSET_CLOCK_SYNC_EXT 0x013c
24#define SAFFIRE_LE_OFFSET_CLOCK_SYNC_EXT 0x0432
25#define SAFFIREPRO_OFFSET_CLOCK_SYNC_EXT 0x0164
Takashi Sakamoto25784ec2014-04-25 22:45:24 +090026
27#define SAFFIRE_CLOCK_SOURCE_INTERNAL 0
28#define SAFFIRE_CLOCK_SOURCE_SPDIF 1
29
Christian Vogeld1d0b6b2014-10-25 13:40:41 +020030/* clock sources as returned from register of Saffire Pro 10 and 26 */
Takashi Sakamoto25784ec2014-04-25 22:45:24 +090031#define SAFFIREPRO_CLOCK_SOURCE_INTERNAL 0
Christian Vogeld1d0b6b2014-10-25 13:40:41 +020032#define SAFFIREPRO_CLOCK_SOURCE_SKIP 1 /* never used on hardware */
Takashi Sakamoto25784ec2014-04-25 22:45:24 +090033#define SAFFIREPRO_CLOCK_SOURCE_SPDIF 2
Christian Vogeld1d0b6b2014-10-25 13:40:41 +020034#define SAFFIREPRO_CLOCK_SOURCE_ADAT1 3 /* not used on s.pro. 10 */
35#define SAFFIREPRO_CLOCK_SOURCE_ADAT2 4 /* not used on s.pro. 10 */
Takashi Sakamoto25784ec2014-04-25 22:45:24 +090036#define SAFFIREPRO_CLOCK_SOURCE_WORDCLOCK 5
Christian Vogeld1d0b6b2014-10-25 13:40:41 +020037#define SAFFIREPRO_CLOCK_SOURCE_COUNT 6
Takashi Sakamoto25784ec2014-04-25 22:45:24 +090038
39/* S/PDIF, ADAT1, ADAT2 is enabled or not. three quadlets */
Takashi Sakamoto9b5f0ed2014-05-28 00:14:42 +090040#define SAFFIREPRO_ENABLE_DIG_IFACES 0x01a4
Takashi Sakamoto25784ec2014-04-25 22:45:24 +090041
42/* saffirepro has its own parameter for sampling frequency */
Takashi Sakamoto9b5f0ed2014-05-28 00:14:42 +090043#define SAFFIREPRO_RATE_NOREBOOT 0x01cc
Takashi Sakamoto25784ec2014-04-25 22:45:24 +090044/* index is the value for this register */
45static const unsigned int rates[] = {
46 [0] = 0,
47 [1] = 44100,
48 [2] = 48000,
49 [3] = 88200,
50 [4] = 96000,
51 [5] = 176400,
52 [6] = 192000
53};
54
55/* saffire(no label)/saffire LE has metering */
Takashi Sakamoto9b5f0ed2014-05-28 00:14:42 +090056#define SAFFIRE_OFFSET_METER 0x0100
57#define SAFFIRE_LE_OFFSET_METER 0x0168
Takashi Sakamoto25784ec2014-04-25 22:45:24 +090058
59static inline int
60saffire_read_block(struct snd_bebob *bebob, u64 offset,
61 u32 *buf, unsigned int size)
62{
63 unsigned int i;
64 int err;
65 __be32 *tmp = (__be32 *)buf;
66
67 err = snd_fw_transaction(bebob->unit, TCODE_READ_BLOCK_REQUEST,
68 SAFFIRE_ADDRESS_BASE + offset,
69 tmp, size, 0);
70 if (err < 0)
71 goto end;
72
73 for (i = 0; i < size / sizeof(u32); i++)
74 buf[i] = be32_to_cpu(tmp[i]);
75end:
76 return err;
77}
78
79static inline int
80saffire_read_quad(struct snd_bebob *bebob, u64 offset, u32 *value)
81{
82 int err;
83 __be32 tmp;
84
85 err = snd_fw_transaction(bebob->unit, TCODE_READ_QUADLET_REQUEST,
86 SAFFIRE_ADDRESS_BASE + offset,
87 &tmp, sizeof(__be32), 0);
88 if (err < 0)
89 goto end;
90
91 *value = be32_to_cpu(tmp);
92end:
93 return err;
94}
95
96static inline int
97saffire_write_quad(struct snd_bebob *bebob, u64 offset, u32 value)
98{
99 __be32 data = cpu_to_be32(value);
100
101 return snd_fw_transaction(bebob->unit, TCODE_WRITE_QUADLET_REQUEST,
102 SAFFIRE_ADDRESS_BASE + offset,
103 &data, sizeof(__be32), 0);
104}
105
Takashi Iwaie7ced412014-10-21 08:26:10 +0200106static const char *const saffirepro_10_clk_src_labels[] = {
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900107 SND_BEBOB_CLOCK_INTERNAL, "S/PDIF", "Word Clock"
108};
Takashi Sakamotoba517712015-06-14 12:49:29 +0900109static enum snd_bebob_clock_type saffirepro_10_clk_src_types[] = {
110 SND_BEBOB_CLOCK_TYPE_INTERNAL,
111 SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* S/PDIF */
112 SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* Word Clock */
113};
Takashi Iwai85c24cd2014-10-27 14:11:07 +0100114static const char *const saffirepro_26_clk_src_labels[] = {
Christian Vogeld1d0b6b2014-10-25 13:40:41 +0200115 SND_BEBOB_CLOCK_INTERNAL, "S/PDIF", "ADAT1", "ADAT2", "Word Clock"
116};
Takashi Sakamotoba517712015-06-14 12:49:29 +0900117static enum snd_bebob_clock_type saffirepro_26_clk_src_types[] = {
118 SND_BEBOB_CLOCK_TYPE_INTERNAL,
119 SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* S/PDIF */
120 SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* ADAT1 */
121 SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* ADAT2 */
122 SND_BEBOB_CLOCK_TYPE_EXTERNAL, /* Word Clock */
123};
Christian Vogeld1d0b6b2014-10-25 13:40:41 +0200124/* Value maps between registers and labels for SaffirePro 10/26. */
125static const signed char saffirepro_clk_maps[][SAFFIREPRO_CLOCK_SOURCE_COUNT] = {
126 /* SaffirePro 10 */
127 [0] = {
128 [SAFFIREPRO_CLOCK_SOURCE_INTERNAL] = 0,
129 [SAFFIREPRO_CLOCK_SOURCE_SKIP] = -1, /* not supported */
130 [SAFFIREPRO_CLOCK_SOURCE_SPDIF] = 1,
131 [SAFFIREPRO_CLOCK_SOURCE_ADAT1] = -1, /* not supported */
132 [SAFFIREPRO_CLOCK_SOURCE_ADAT2] = -1, /* not supported */
133 [SAFFIREPRO_CLOCK_SOURCE_WORDCLOCK] = 2,
134 },
135 /* SaffirePro 26 */
136 [1] = {
137 [SAFFIREPRO_CLOCK_SOURCE_INTERNAL] = 0,
138 [SAFFIREPRO_CLOCK_SOURCE_SKIP] = -1, /* not supported */
139 [SAFFIREPRO_CLOCK_SOURCE_SPDIF] = 1,
140 [SAFFIREPRO_CLOCK_SOURCE_ADAT1] = 2,
141 [SAFFIREPRO_CLOCK_SOURCE_ADAT2] = 3,
142 [SAFFIREPRO_CLOCK_SOURCE_WORDCLOCK] = 4,
143 }
144};
145
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900146static int
147saffirepro_both_clk_freq_get(struct snd_bebob *bebob, unsigned int *rate)
148{
149 u32 id;
150 int err;
151
152 err = saffire_read_quad(bebob, SAFFIREPRO_RATE_NOREBOOT, &id);
153 if (err < 0)
154 goto end;
155 if (id >= ARRAY_SIZE(rates))
156 err = -EIO;
157 else
158 *rate = rates[id];
159end:
160 return err;
161}
162static int
163saffirepro_both_clk_freq_set(struct snd_bebob *bebob, unsigned int rate)
164{
165 u32 id;
166
167 for (id = 0; id < ARRAY_SIZE(rates); id++) {
168 if (rates[id] == rate)
169 break;
170 }
171 if (id == ARRAY_SIZE(rates))
172 return -EINVAL;
173
174 return saffire_write_quad(bebob, SAFFIREPRO_RATE_NOREBOOT, id);
175}
Christian Vogeld1d0b6b2014-10-25 13:40:41 +0200176
177/*
178 * query hardware for current clock source, return our internally
179 * used clock index in *id, depending on hardware.
180 */
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900181static int
182saffirepro_both_clk_src_get(struct snd_bebob *bebob, unsigned int *id)
183{
184 int err;
Christian Vogeld1d0b6b2014-10-25 13:40:41 +0200185 u32 value; /* clock source read from hw register */
186 const signed char *map;
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900187
188 err = saffire_read_quad(bebob, SAFFIREPRO_OFFSET_CLOCK_SOURCE, &value);
189 if (err < 0)
190 goto end;
191
Christian Vogeld1d0b6b2014-10-25 13:40:41 +0200192 /* depending on hardware, use a different mapping */
Takashi Sakamotoba517712015-06-14 12:49:29 +0900193 if (bebob->spec->clock->types == saffirepro_10_clk_src_types)
Christian Vogeld1d0b6b2014-10-25 13:40:41 +0200194 map = saffirepro_clk_maps[0];
195 else
196 map = saffirepro_clk_maps[1];
197
198 /* In a case that this driver cannot handle the value of register. */
199 if (value >= SAFFIREPRO_CLOCK_SOURCE_COUNT || map[value] < 0) {
200 err = -EIO;
201 goto end;
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900202 }
Christian Vogeld1d0b6b2014-10-25 13:40:41 +0200203
204 *id = (unsigned int)map[value];
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900205end:
206 return err;
207}
208
209struct snd_bebob_spec saffire_le_spec;
Takashi Iwaie7ced412014-10-21 08:26:10 +0200210static const char *const saffire_both_clk_src_labels[] = {
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900211 SND_BEBOB_CLOCK_INTERNAL, "S/PDIF"
212};
Takashi Sakamotoba517712015-06-14 12:49:29 +0900213static enum snd_bebob_clock_type saffire_both_clk_src_types[] = {
214 SND_BEBOB_CLOCK_TYPE_INTERNAL,
215 SND_BEBOB_CLOCK_TYPE_EXTERNAL,
216};
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900217static int
218saffire_both_clk_src_get(struct snd_bebob *bebob, unsigned int *id)
219{
220 int err;
221 u32 value;
222
223 err = saffire_read_quad(bebob, SAFFIRE_OFFSET_CLOCK_SOURCE, &value);
224 if (err >= 0)
225 *id = 0xff & value;
226
227 return err;
228};
Takashi Iwaie7ced412014-10-21 08:26:10 +0200229static const char *const saffire_le_meter_labels[] = {
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900230 ANA_IN, ANA_IN, DIG_IN,
231 ANA_OUT, ANA_OUT, ANA_OUT, ANA_OUT,
232 STM_IN, STM_IN
233};
Takashi Iwaie7ced412014-10-21 08:26:10 +0200234static const char *const saffire_meter_labels[] = {
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900235 ANA_IN, ANA_IN,
236 STM_IN, STM_IN, STM_IN, STM_IN, STM_IN,
237};
238static int
239saffire_meter_get(struct snd_bebob *bebob, u32 *buf, unsigned int size)
240{
241 struct snd_bebob_meter_spec *spec = bebob->spec->meter;
242 unsigned int channels;
243 u64 offset;
244 int err;
245
246 if (spec->labels == saffire_le_meter_labels)
247 offset = SAFFIRE_LE_OFFSET_METER;
248 else
249 offset = SAFFIRE_OFFSET_METER;
250
251 channels = spec->num * 2;
252 if (size < channels * sizeof(u32))
253 return -EIO;
254
255 err = saffire_read_block(bebob, offset, buf, size);
256 if (err >= 0 && spec->labels == saffire_le_meter_labels) {
257 swap(buf[1], buf[3]);
258 swap(buf[2], buf[3]);
259 swap(buf[3], buf[4]);
260
261 swap(buf[7], buf[10]);
262 swap(buf[8], buf[10]);
263 swap(buf[9], buf[11]);
264 swap(buf[11], buf[12]);
265
266 swap(buf[15], buf[16]);
267 }
268
269 return err;
270}
271
272static struct snd_bebob_rate_spec saffirepro_both_rate_spec = {
273 .get = &saffirepro_both_clk_freq_get,
274 .set = &saffirepro_both_clk_freq_set,
275};
276/* Saffire Pro 26 I/O */
277static struct snd_bebob_clock_spec saffirepro_26_clk_spec = {
Takashi Sakamotoba517712015-06-14 12:49:29 +0900278 .num = ARRAY_SIZE(saffirepro_26_clk_src_types),
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900279 .labels = saffirepro_26_clk_src_labels,
Takashi Sakamotoba517712015-06-14 12:49:29 +0900280 .types = saffirepro_26_clk_src_types,
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900281 .get = &saffirepro_both_clk_src_get,
282};
283struct snd_bebob_spec saffirepro_26_spec = {
284 .clock = &saffirepro_26_clk_spec,
285 .rate = &saffirepro_both_rate_spec,
286 .meter = NULL
287};
288/* Saffire Pro 10 I/O */
289static struct snd_bebob_clock_spec saffirepro_10_clk_spec = {
Takashi Sakamotoba517712015-06-14 12:49:29 +0900290 .num = ARRAY_SIZE(saffirepro_10_clk_src_types),
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900291 .labels = saffirepro_10_clk_src_labels,
Takashi Sakamotoba517712015-06-14 12:49:29 +0900292 .types = saffirepro_10_clk_src_types,
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900293 .get = &saffirepro_both_clk_src_get,
294};
295struct snd_bebob_spec saffirepro_10_spec = {
296 .clock = &saffirepro_10_clk_spec,
297 .rate = &saffirepro_both_rate_spec,
298 .meter = NULL
299};
300
301static struct snd_bebob_rate_spec saffire_both_rate_spec = {
302 .get = &snd_bebob_stream_get_rate,
303 .set = &snd_bebob_stream_set_rate,
304};
305static struct snd_bebob_clock_spec saffire_both_clk_spec = {
Takashi Sakamotoba517712015-06-14 12:49:29 +0900306 .num = ARRAY_SIZE(saffire_both_clk_src_types),
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900307 .labels = saffire_both_clk_src_labels,
Takashi Sakamotoba517712015-06-14 12:49:29 +0900308 .types = saffire_both_clk_src_types,
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900309 .get = &saffire_both_clk_src_get,
310};
311/* Saffire LE */
Takashi Sakamoto9fb01cd2014-05-28 00:14:45 +0900312static struct snd_bebob_meter_spec saffire_le_meter_spec = {
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900313 .num = ARRAY_SIZE(saffire_le_meter_labels),
314 .labels = saffire_le_meter_labels,
315 .get = &saffire_meter_get,
316};
317struct snd_bebob_spec saffire_le_spec = {
318 .clock = &saffire_both_clk_spec,
319 .rate = &saffire_both_rate_spec,
320 .meter = &saffire_le_meter_spec
321};
322/* Saffire */
Takashi Sakamoto9fb01cd2014-05-28 00:14:45 +0900323static struct snd_bebob_meter_spec saffire_meter_spec = {
Takashi Sakamoto25784ec2014-04-25 22:45:24 +0900324 .num = ARRAY_SIZE(saffire_meter_labels),
325 .labels = saffire_meter_labels,
326 .get = &saffire_meter_get,
327};
328struct snd_bebob_spec saffire_spec = {
329 .clock = &saffire_both_clk_spec,
330 .rate = &saffire_both_rate_spec,
331 .meter = &saffire_meter_spec
332};