1 // SPDX-License-Identifier: GPL-2.0-only
3 * motu-protocol-v3.c - a part of driver for MOTU FireWire series
5 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
8 #include <linux/delay.h>
11 #define V3_CLOCK_STATUS_OFFSET 0x0b14
12 #define V3_FETCH_PCM_FRAMES 0x02000000
13 #define V3_CLOCK_RATE_MASK 0x0000ff00
14 #define V3_CLOCK_RATE_SHIFT 8
15 #define V3_CLOCK_SOURCE_MASK 0x000000ff
17 #define V3_OPT_IFACE_MODE_OFFSET 0x0c94
18 #define V3_ENABLE_OPT_IN_IFACE_A 0x00000001
19 #define V3_ENABLE_OPT_IN_IFACE_B 0x00000002
20 #define V3_ENABLE_OPT_OUT_IFACE_A 0x00000100
21 #define V3_ENABLE_OPT_OUT_IFACE_B 0x00000200
22 #define V3_NO_ADAT_OPT_IN_IFACE_A 0x00010000
23 #define V3_NO_ADAT_OPT_IN_IFACE_B 0x00100000
24 #define V3_NO_ADAT_OPT_OUT_IFACE_A 0x00040000
25 #define V3_NO_ADAT_OPT_OUT_IFACE_B 0x00400000
27 int snd_motu_protocol_v3_get_clock_rate(struct snd_motu *motu,
34 err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, ®,
38 data = be32_to_cpu(reg);
40 data = (data & V3_CLOCK_RATE_MASK) >> V3_CLOCK_RATE_SHIFT;
41 if (data >= ARRAY_SIZE(snd_motu_clock_rates))
44 *rate = snd_motu_clock_rates[data];
49 int snd_motu_protocol_v3_set_clock_rate(struct snd_motu *motu,
57 for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
58 if (snd_motu_clock_rates[i] == rate)
61 if (i == ARRAY_SIZE(snd_motu_clock_rates))
64 err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, ®,
68 data = be32_to_cpu(reg);
70 data &= ~(V3_CLOCK_RATE_MASK | V3_FETCH_PCM_FRAMES);
71 data |= i << V3_CLOCK_RATE_SHIFT;
73 need_to_wait = data != be32_to_cpu(reg);
75 reg = cpu_to_be32(data);
76 err = snd_motu_transaction_write(motu, V3_CLOCK_STATUS_OFFSET, ®,
83 if (msleep_interruptible(4000) > 0)
90 int snd_motu_protocol_v3_get_clock_source(struct snd_motu *motu,
91 enum snd_motu_clock_source *src)
98 err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, ®,
102 data = be32_to_cpu(reg);
104 val = data & V3_CLOCK_SOURCE_MASK;
106 *src = SND_MOTU_CLOCK_SOURCE_INTERNAL;
107 } else if (val == 0x01) {
108 *src = SND_MOTU_CLOCK_SOURCE_WORD_ON_BNC;
109 } else if (val == 0x02) {
110 *src = SND_MOTU_CLOCK_SOURCE_SPH;
111 } else if (val == 0x10) {
112 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_COAX;
113 } else if (val == 0x18 || val == 0x19) {
114 err = snd_motu_transaction_read(motu, V3_OPT_IFACE_MODE_OFFSET,
118 data = be32_to_cpu(reg);
121 if (data & V3_NO_ADAT_OPT_IN_IFACE_A)
122 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_A;
124 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_A;
126 if (data & V3_NO_ADAT_OPT_IN_IFACE_B)
127 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_B;
129 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_B;
132 *src = SND_MOTU_CLOCK_SOURCE_UNKNOWN;
138 int snd_motu_protocol_v3_switch_fetching_mode(struct snd_motu *motu,
145 err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, ®,
149 data = be32_to_cpu(reg);
152 data |= V3_FETCH_PCM_FRAMES;
154 data &= ~V3_FETCH_PCM_FRAMES;
156 reg = cpu_to_be32(data);
157 return snd_motu_transaction_write(motu, V3_CLOCK_STATUS_OFFSET, ®,
161 static void calculate_fixed_part(struct snd_motu_packet_format *formats,
162 enum amdtp_stream_direction dir,
163 enum snd_motu_spec_flags flags,
164 unsigned char analog_ports)
166 unsigned char pcm_chunks[3] = {0, 0, 0};
168 pcm_chunks[0] = analog_ports;
169 pcm_chunks[1] = analog_ports;
170 if (flags & SND_MOTU_SPEC_SUPPORT_CLOCK_X4)
171 pcm_chunks[2] = analog_ports;
173 if (dir == AMDTP_IN_STREAM) {
174 if (flags & SND_MOTU_SPEC_TX_MICINST_CHUNK) {
177 if (flags & SND_MOTU_SPEC_SUPPORT_CLOCK_X4)
181 if (flags & SND_MOTU_SPEC_TX_RETURN_CHUNK) {
184 if (flags & SND_MOTU_SPEC_SUPPORT_CLOCK_X4)
188 if (flags & SND_MOTU_SPEC_TX_REVERB_CHUNK) {
193 if (flags & SND_MOTU_SPEC_RX_SEPARATED_MAIN) {
198 // Packets to v3 units include 2 chunks for phone 1/2, except
199 // for 176.4/192.0 kHz.
204 if (flags & SND_MOTU_SPEC_HAS_AESEBU_IFACE) {
210 * At least, packets have two data chunks for S/PDIF on coaxial
217 * Fixed part consists of PCM chunks multiple of 4, with msg chunks. As
218 * a result, this part can includes empty data chunks.
220 formats->fixed_part_pcm_chunks[0] = round_up(2 + pcm_chunks[0], 4) - 2;
221 formats->fixed_part_pcm_chunks[1] = round_up(2 + pcm_chunks[1], 4) - 2;
222 if (flags & SND_MOTU_SPEC_SUPPORT_CLOCK_X4)
223 formats->fixed_part_pcm_chunks[2] =
224 round_up(2 + pcm_chunks[2], 4) - 2;
227 static void calculate_differed_part(struct snd_motu_packet_format *formats,
228 enum snd_motu_spec_flags flags, u32 data,
229 u32 a_enable_mask, u32 a_no_adat_mask,
230 u32 b_enable_mask, u32 b_no_adat_mask)
232 unsigned char pcm_chunks[3] = {0, 0, 0};
235 if ((flags & SND_MOTU_SPEC_HAS_OPT_IFACE_A) && (data & a_enable_mask)) {
236 if (data & a_no_adat_mask) {
238 * Additional two data chunks for S/PDIF on optical
239 * interface A. This includes empty data chunks.
245 * Additional data chunks for ADAT on optical interface
253 if ((flags & SND_MOTU_SPEC_HAS_OPT_IFACE_B) && (data & b_enable_mask)) {
254 if (data & b_no_adat_mask) {
256 * Additional two data chunks for S/PDIF on optical
257 * interface B. This includes empty data chunks.
263 * Additional data chunks for ADAT on optical interface
271 for (i = 0; i < 3; ++i) {
272 if (pcm_chunks[i] > 0)
273 pcm_chunks[i] = round_up(pcm_chunks[i], 4);
275 formats->differed_part_pcm_chunks[i] = pcm_chunks[i];
279 static int detect_packet_formats_828mk3(struct snd_motu *motu, u32 data)
281 if (data & V3_ENABLE_OPT_IN_IFACE_A) {
282 if (data & V3_NO_ADAT_OPT_IN_IFACE_A) {
283 motu->tx_packet_formats.pcm_chunks[0] += 4;
284 motu->tx_packet_formats.pcm_chunks[1] += 4;
286 motu->tx_packet_formats.pcm_chunks[0] += 8;
287 motu->tx_packet_formats.pcm_chunks[1] += 4;
291 if (data & V3_ENABLE_OPT_IN_IFACE_B) {
292 if (data & V3_NO_ADAT_OPT_IN_IFACE_B) {
293 motu->tx_packet_formats.pcm_chunks[0] += 4;
294 motu->tx_packet_formats.pcm_chunks[1] += 4;
296 motu->tx_packet_formats.pcm_chunks[0] += 8;
297 motu->tx_packet_formats.pcm_chunks[1] += 4;
301 if (data & V3_ENABLE_OPT_OUT_IFACE_A) {
302 if (data & V3_NO_ADAT_OPT_OUT_IFACE_A) {
303 motu->rx_packet_formats.pcm_chunks[0] += 4;
304 motu->rx_packet_formats.pcm_chunks[1] += 4;
306 motu->rx_packet_formats.pcm_chunks[0] += 8;
307 motu->rx_packet_formats.pcm_chunks[1] += 4;
311 if (data & V3_ENABLE_OPT_OUT_IFACE_B) {
312 if (data & V3_NO_ADAT_OPT_OUT_IFACE_B) {
313 motu->rx_packet_formats.pcm_chunks[0] += 4;
314 motu->rx_packet_formats.pcm_chunks[1] += 4;
316 motu->rx_packet_formats.pcm_chunks[0] += 8;
317 motu->rx_packet_formats.pcm_chunks[1] += 4;
324 int snd_motu_protocol_v3_cache_packet_formats(struct snd_motu *motu)
330 motu->tx_packet_formats.pcm_byte_offset = 10;
331 motu->rx_packet_formats.pcm_byte_offset = 10;
333 motu->tx_packet_formats.msg_chunks = 2;
334 motu->rx_packet_formats.msg_chunks = 2;
336 err = snd_motu_transaction_read(motu, V3_OPT_IFACE_MODE_OFFSET, ®,
340 data = be32_to_cpu(reg);
342 calculate_fixed_part(&motu->tx_packet_formats, AMDTP_IN_STREAM,
343 motu->spec->flags, motu->spec->analog_in_ports);
344 calculate_differed_part(&motu->tx_packet_formats,
345 motu->spec->flags, data,
346 V3_ENABLE_OPT_IN_IFACE_A, V3_NO_ADAT_OPT_IN_IFACE_A,
347 V3_ENABLE_OPT_IN_IFACE_B, V3_NO_ADAT_OPT_IN_IFACE_B);
349 calculate_fixed_part(&motu->rx_packet_formats, AMDTP_OUT_STREAM,
350 motu->spec->flags, motu->spec->analog_out_ports);
351 calculate_differed_part(&motu->rx_packet_formats,
352 motu->spec->flags, data,
353 V3_ENABLE_OPT_OUT_IFACE_A, V3_NO_ADAT_OPT_OUT_IFACE_A,
354 V3_ENABLE_OPT_OUT_IFACE_B, V3_NO_ADAT_OPT_OUT_IFACE_B);
356 memcpy(motu->tx_packet_formats.pcm_chunks,
357 motu->spec->tx_fixed_pcm_chunks,
358 sizeof(motu->tx_packet_formats.pcm_chunks));
359 memcpy(motu->rx_packet_formats.pcm_chunks,
360 motu->spec->rx_fixed_pcm_chunks,
361 sizeof(motu->rx_packet_formats.pcm_chunks));
363 if (motu->spec == &snd_motu_spec_828mk3)
364 return detect_packet_formats_828mk3(motu, data);
370 const struct snd_motu_spec snd_motu_spec_828mk3 = {
372 .protocol_version = SND_MOTU_PROTOCOL_V3,
373 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
374 SND_MOTU_SPEC_SUPPORT_CLOCK_X4 |
375 SND_MOTU_SPEC_TX_MICINST_CHUNK |
376 SND_MOTU_SPEC_TX_RETURN_CHUNK |
377 SND_MOTU_SPEC_TX_REVERB_CHUNK |
378 SND_MOTU_SPEC_RX_SEPARATED_MAIN |
379 SND_MOTU_SPEC_HAS_OPT_IFACE_A |
380 SND_MOTU_SPEC_HAS_OPT_IFACE_B |
381 SND_MOTU_SPEC_RX_MIDI_3RD_Q |
382 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
383 .tx_fixed_pcm_chunks = {18, 18, 14},
384 .rx_fixed_pcm_chunks = {14, 14, 10},
385 .analog_in_ports = 8,
386 .analog_out_ports = 8,
389 const struct snd_motu_spec snd_motu_spec_audio_express = {
390 .name = "AudioExpress",
391 .protocol_version = SND_MOTU_PROTOCOL_V3,
392 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
393 SND_MOTU_SPEC_TX_MICINST_CHUNK |
394 SND_MOTU_SPEC_TX_RETURN_CHUNK |
395 SND_MOTU_SPEC_RX_SEPARATED_MAIN |
396 SND_MOTU_SPEC_RX_MIDI_2ND_Q |
397 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
398 .tx_fixed_pcm_chunks = {10, 10, 0},
399 .rx_fixed_pcm_chunks = {10, 10, 0},
400 .analog_in_ports = 2,
401 .analog_out_ports = 4,
404 const struct snd_motu_spec snd_motu_spec_4pre = {
406 .protocol_version = SND_MOTU_PROTOCOL_V3,
407 .flags = SND_MOTU_SPEC_SUPPORT_CLOCK_X2 |
408 SND_MOTU_SPEC_TX_MICINST_CHUNK |
409 SND_MOTU_SPEC_TX_RETURN_CHUNK |
410 SND_MOTU_SPEC_RX_SEPARATED_MAIN,
411 .tx_fixed_pcm_chunks = {10, 10, 0},
412 .rx_fixed_pcm_chunks = {10, 10, 0},
413 .analog_in_ports = 2,
414 .analog_out_ports = 2,