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 #define V3_MSG_FLAG_CLK_CHANGED 0x00000002
28 #define V3_CLK_WAIT_MSEC 4000
30 int snd_motu_protocol_v3_get_clock_rate(struct snd_motu *motu,
37 err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, ®,
41 data = be32_to_cpu(reg);
43 data = (data & V3_CLOCK_RATE_MASK) >> V3_CLOCK_RATE_SHIFT;
44 if (data >= ARRAY_SIZE(snd_motu_clock_rates))
47 *rate = snd_motu_clock_rates[data];
52 int snd_motu_protocol_v3_set_clock_rate(struct snd_motu *motu,
60 for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
61 if (snd_motu_clock_rates[i] == rate)
64 if (i == ARRAY_SIZE(snd_motu_clock_rates))
67 err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, ®,
71 data = be32_to_cpu(reg);
73 data &= ~(V3_CLOCK_RATE_MASK | V3_FETCH_PCM_FRAMES);
74 data |= i << V3_CLOCK_RATE_SHIFT;
76 need_to_wait = data != be32_to_cpu(reg);
78 reg = cpu_to_be32(data);
79 err = snd_motu_transaction_write(motu, V3_CLOCK_STATUS_OFFSET, ®,
88 result = wait_event_interruptible_timeout(motu->hwdep_wait,
89 motu->msg & V3_MSG_FLAG_CLK_CHANGED,
90 msecs_to_jiffies(V3_CLK_WAIT_MSEC));
100 int snd_motu_protocol_v3_get_clock_source(struct snd_motu *motu,
101 enum snd_motu_clock_source *src)
107 err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, ®,
111 data = be32_to_cpu(reg) & V3_CLOCK_SOURCE_MASK;
115 *src = SND_MOTU_CLOCK_SOURCE_INTERNAL;
118 *src = SND_MOTU_CLOCK_SOURCE_WORD_ON_BNC;
121 *src = SND_MOTU_CLOCK_SOURCE_SPH;
124 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_COAX;
132 err = snd_motu_transaction_read(motu,
133 V3_OPT_IFACE_MODE_OFFSET, ®, sizeof(reg));
136 options = be32_to_cpu(reg);
139 if (options & V3_NO_ADAT_OPT_IN_IFACE_A)
140 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_A;
142 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_A;
144 if (options & V3_NO_ADAT_OPT_IN_IFACE_B)
145 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_B;
147 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_B;
152 *src = SND_MOTU_CLOCK_SOURCE_UNKNOWN;
159 int snd_motu_protocol_v3_switch_fetching_mode(struct snd_motu *motu,
166 err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, ®,
170 data = be32_to_cpu(reg);
173 data |= V3_FETCH_PCM_FRAMES;
175 data &= ~V3_FETCH_PCM_FRAMES;
177 reg = cpu_to_be32(data);
178 return snd_motu_transaction_write(motu, V3_CLOCK_STATUS_OFFSET, ®,
182 static int detect_packet_formats_828mk3(struct snd_motu *motu, u32 data)
184 if (data & V3_ENABLE_OPT_IN_IFACE_A) {
185 if (data & V3_NO_ADAT_OPT_IN_IFACE_A) {
186 motu->tx_packet_formats.pcm_chunks[0] += 4;
187 motu->tx_packet_formats.pcm_chunks[1] += 4;
189 motu->tx_packet_formats.pcm_chunks[0] += 8;
190 motu->tx_packet_formats.pcm_chunks[1] += 4;
194 if (data & V3_ENABLE_OPT_IN_IFACE_B) {
195 if (data & V3_NO_ADAT_OPT_IN_IFACE_B) {
196 motu->tx_packet_formats.pcm_chunks[0] += 4;
197 motu->tx_packet_formats.pcm_chunks[1] += 4;
199 motu->tx_packet_formats.pcm_chunks[0] += 8;
200 motu->tx_packet_formats.pcm_chunks[1] += 4;
204 if (data & V3_ENABLE_OPT_OUT_IFACE_A) {
205 if (data & V3_NO_ADAT_OPT_OUT_IFACE_A) {
206 motu->rx_packet_formats.pcm_chunks[0] += 4;
207 motu->rx_packet_formats.pcm_chunks[1] += 4;
209 motu->rx_packet_formats.pcm_chunks[0] += 8;
210 motu->rx_packet_formats.pcm_chunks[1] += 4;
214 if (data & V3_ENABLE_OPT_OUT_IFACE_B) {
215 if (data & V3_NO_ADAT_OPT_OUT_IFACE_B) {
216 motu->rx_packet_formats.pcm_chunks[0] += 4;
217 motu->rx_packet_formats.pcm_chunks[1] += 4;
219 motu->rx_packet_formats.pcm_chunks[0] += 8;
220 motu->rx_packet_formats.pcm_chunks[1] += 4;
227 int snd_motu_protocol_v3_cache_packet_formats(struct snd_motu *motu)
233 motu->tx_packet_formats.pcm_byte_offset = 10;
234 motu->rx_packet_formats.pcm_byte_offset = 10;
236 motu->tx_packet_formats.msg_chunks = 2;
237 motu->rx_packet_formats.msg_chunks = 2;
239 err = snd_motu_transaction_read(motu, V3_OPT_IFACE_MODE_OFFSET, ®,
243 data = be32_to_cpu(reg);
245 memcpy(motu->tx_packet_formats.pcm_chunks,
246 motu->spec->tx_fixed_pcm_chunks,
247 sizeof(motu->tx_packet_formats.pcm_chunks));
248 memcpy(motu->rx_packet_formats.pcm_chunks,
249 motu->spec->rx_fixed_pcm_chunks,
250 sizeof(motu->rx_packet_formats.pcm_chunks));
252 if (motu->spec == &snd_motu_spec_828mk3_fw || motu->spec == &snd_motu_spec_828mk3_hybrid)
253 return detect_packet_formats_828mk3(motu, data);
259 const struct snd_motu_spec snd_motu_spec_828mk3_fw = {
261 .protocol_version = SND_MOTU_PROTOCOL_V3,
262 .flags = SND_MOTU_SPEC_RX_MIDI_3RD_Q |
263 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
264 .tx_fixed_pcm_chunks = {18, 18, 14},
265 .rx_fixed_pcm_chunks = {14, 14, 10},
268 const struct snd_motu_spec snd_motu_spec_828mk3_hybrid = {
270 .protocol_version = SND_MOTU_PROTOCOL_V3,
271 .flags = SND_MOTU_SPEC_RX_MIDI_3RD_Q |
272 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
273 .tx_fixed_pcm_chunks = {18, 18, 14},
274 .rx_fixed_pcm_chunks = {14, 14, 14}, // Additional 4 dummy chunks at higher rate.
277 const struct snd_motu_spec snd_motu_spec_ultralite_mk3 = {
278 .name = "UltraLiteMk3",
279 .protocol_version = SND_MOTU_PROTOCOL_V3,
280 .flags = SND_MOTU_SPEC_RX_MIDI_3RD_Q |
281 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
282 .tx_fixed_pcm_chunks = {18, 14, 10},
283 .rx_fixed_pcm_chunks = {14, 14, 14},
286 const struct snd_motu_spec snd_motu_spec_audio_express = {
287 .name = "AudioExpress",
288 .protocol_version = SND_MOTU_PROTOCOL_V3,
289 .flags = SND_MOTU_SPEC_RX_MIDI_2ND_Q |
290 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
291 .tx_fixed_pcm_chunks = {10, 10, 0},
292 .rx_fixed_pcm_chunks = {10, 10, 0},
295 const struct snd_motu_spec snd_motu_spec_4pre = {
297 .protocol_version = SND_MOTU_PROTOCOL_V3,
298 .tx_fixed_pcm_chunks = {10, 10, 0},
299 .rx_fixed_pcm_chunks = {10, 10, 0},