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
16 #define V3_CLOCK_SRC_INTERNAL 0x00
17 #define V3_CLOCK_SRC_WORD_ON_BNC 0x01
18 #define V3_CLOCK_SRC_SPH 0x02
19 #define V3_CLOCK_SRC_SPDIF_ON_COAX 0x10
20 #define V3_CLOCK_SRC_OPT_IFACE_A 0x18
21 #define V3_CLOCK_SRC_OPT_IFACE_B 0x19
23 #define V3_OPT_IFACE_MODE_OFFSET 0x0c94
24 #define V3_ENABLE_OPT_IN_IFACE_A 0x00000001
25 #define V3_ENABLE_OPT_IN_IFACE_B 0x00000002
26 #define V3_ENABLE_OPT_OUT_IFACE_A 0x00000100
27 #define V3_ENABLE_OPT_OUT_IFACE_B 0x00000200
28 #define V3_NO_ADAT_OPT_IN_IFACE_A 0x00010000
29 #define V3_NO_ADAT_OPT_IN_IFACE_B 0x00100000
30 #define V3_NO_ADAT_OPT_OUT_IFACE_A 0x00040000
31 #define V3_NO_ADAT_OPT_OUT_IFACE_B 0x00400000
33 #define V3_MSG_FLAG_CLK_CHANGED 0x00000002
34 #define V3_CLK_WAIT_MSEC 4000
36 int snd_motu_protocol_v3_get_clock_rate(struct snd_motu *motu,
43 err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, ®,
47 data = be32_to_cpu(reg);
49 data = (data & V3_CLOCK_RATE_MASK) >> V3_CLOCK_RATE_SHIFT;
50 if (data >= ARRAY_SIZE(snd_motu_clock_rates))
53 *rate = snd_motu_clock_rates[data];
58 int snd_motu_protocol_v3_set_clock_rate(struct snd_motu *motu,
66 for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
67 if (snd_motu_clock_rates[i] == rate)
70 if (i == ARRAY_SIZE(snd_motu_clock_rates))
73 err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, ®,
77 data = be32_to_cpu(reg);
79 data &= ~(V3_CLOCK_RATE_MASK | V3_FETCH_PCM_FRAMES);
80 data |= i << V3_CLOCK_RATE_SHIFT;
82 need_to_wait = data != be32_to_cpu(reg);
84 reg = cpu_to_be32(data);
85 err = snd_motu_transaction_write(motu, V3_CLOCK_STATUS_OFFSET, ®,
94 result = wait_event_interruptible_timeout(motu->hwdep_wait,
95 motu->msg & V3_MSG_FLAG_CLK_CHANGED,
96 msecs_to_jiffies(V3_CLK_WAIT_MSEC));
106 int snd_motu_protocol_v3_get_clock_source(struct snd_motu *motu,
107 enum snd_motu_clock_source *src)
113 err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, ®,
117 data = be32_to_cpu(reg) & V3_CLOCK_SOURCE_MASK;
120 case V3_CLOCK_SRC_INTERNAL:
121 *src = SND_MOTU_CLOCK_SOURCE_INTERNAL;
123 case V3_CLOCK_SRC_WORD_ON_BNC:
124 *src = SND_MOTU_CLOCK_SOURCE_WORD_ON_BNC;
126 case V3_CLOCK_SRC_SPH:
127 *src = SND_MOTU_CLOCK_SOURCE_SPH;
129 case V3_CLOCK_SRC_SPDIF_ON_COAX:
130 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_COAX;
132 case V3_CLOCK_SRC_OPT_IFACE_A:
133 case V3_CLOCK_SRC_OPT_IFACE_B:
138 err = snd_motu_transaction_read(motu,
139 V3_OPT_IFACE_MODE_OFFSET, ®, sizeof(reg));
142 options = be32_to_cpu(reg);
144 if (data == V3_CLOCK_SRC_OPT_IFACE_A) {
145 if (options & V3_NO_ADAT_OPT_IN_IFACE_A)
146 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_A;
148 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_A;
150 if (options & V3_NO_ADAT_OPT_IN_IFACE_B)
151 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_B;
153 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_B;
158 *src = SND_MOTU_CLOCK_SOURCE_UNKNOWN;
165 int snd_motu_protocol_v3_switch_fetching_mode(struct snd_motu *motu,
172 err = snd_motu_transaction_read(motu, V3_CLOCK_STATUS_OFFSET, ®,
176 data = be32_to_cpu(reg);
179 data |= V3_FETCH_PCM_FRAMES;
181 data &= ~V3_FETCH_PCM_FRAMES;
183 reg = cpu_to_be32(data);
184 return snd_motu_transaction_write(motu, V3_CLOCK_STATUS_OFFSET, ®,
188 static int detect_packet_formats_828mk3(struct snd_motu *motu, u32 data)
190 if (data & V3_ENABLE_OPT_IN_IFACE_A) {
191 if (data & V3_NO_ADAT_OPT_IN_IFACE_A) {
192 motu->tx_packet_formats.pcm_chunks[0] += 4;
193 motu->tx_packet_formats.pcm_chunks[1] += 4;
195 motu->tx_packet_formats.pcm_chunks[0] += 8;
196 motu->tx_packet_formats.pcm_chunks[1] += 4;
200 if (data & V3_ENABLE_OPT_IN_IFACE_B) {
201 if (data & V3_NO_ADAT_OPT_IN_IFACE_B) {
202 motu->tx_packet_formats.pcm_chunks[0] += 4;
203 motu->tx_packet_formats.pcm_chunks[1] += 4;
205 motu->tx_packet_formats.pcm_chunks[0] += 8;
206 motu->tx_packet_formats.pcm_chunks[1] += 4;
210 if (data & V3_ENABLE_OPT_OUT_IFACE_A) {
211 if (data & V3_NO_ADAT_OPT_OUT_IFACE_A) {
212 motu->rx_packet_formats.pcm_chunks[0] += 4;
213 motu->rx_packet_formats.pcm_chunks[1] += 4;
215 motu->rx_packet_formats.pcm_chunks[0] += 8;
216 motu->rx_packet_formats.pcm_chunks[1] += 4;
220 if (data & V3_ENABLE_OPT_OUT_IFACE_B) {
221 if (data & V3_NO_ADAT_OPT_OUT_IFACE_B) {
222 motu->rx_packet_formats.pcm_chunks[0] += 4;
223 motu->rx_packet_formats.pcm_chunks[1] += 4;
225 motu->rx_packet_formats.pcm_chunks[0] += 8;
226 motu->rx_packet_formats.pcm_chunks[1] += 4;
233 int snd_motu_protocol_v3_cache_packet_formats(struct snd_motu *motu)
239 motu->tx_packet_formats.pcm_byte_offset = 10;
240 motu->rx_packet_formats.pcm_byte_offset = 10;
242 motu->tx_packet_formats.msg_chunks = 2;
243 motu->rx_packet_formats.msg_chunks = 2;
245 err = snd_motu_transaction_read(motu, V3_OPT_IFACE_MODE_OFFSET, ®,
249 data = be32_to_cpu(reg);
251 memcpy(motu->tx_packet_formats.pcm_chunks,
252 motu->spec->tx_fixed_pcm_chunks,
253 sizeof(motu->tx_packet_formats.pcm_chunks));
254 memcpy(motu->rx_packet_formats.pcm_chunks,
255 motu->spec->rx_fixed_pcm_chunks,
256 sizeof(motu->rx_packet_formats.pcm_chunks));
258 if (motu->spec == &snd_motu_spec_828mk3_fw || motu->spec == &snd_motu_spec_828mk3_hybrid)
259 return detect_packet_formats_828mk3(motu, data);
265 const struct snd_motu_spec snd_motu_spec_828mk3_fw = {
267 .protocol_version = SND_MOTU_PROTOCOL_V3,
268 .flags = SND_MOTU_SPEC_RX_MIDI_3RD_Q |
269 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
270 .tx_fixed_pcm_chunks = {18, 18, 14},
271 .rx_fixed_pcm_chunks = {14, 14, 10},
274 const struct snd_motu_spec snd_motu_spec_828mk3_hybrid = {
276 .protocol_version = SND_MOTU_PROTOCOL_V3,
277 .flags = SND_MOTU_SPEC_RX_MIDI_3RD_Q |
278 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
279 .tx_fixed_pcm_chunks = {18, 18, 14},
280 .rx_fixed_pcm_chunks = {14, 14, 14}, // Additional 4 dummy chunks at higher rate.
283 const struct snd_motu_spec snd_motu_spec_ultralite_mk3 = {
284 .name = "UltraLiteMk3",
285 .protocol_version = SND_MOTU_PROTOCOL_V3,
286 .flags = SND_MOTU_SPEC_RX_MIDI_3RD_Q |
287 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
288 .tx_fixed_pcm_chunks = {18, 14, 10},
289 .rx_fixed_pcm_chunks = {14, 14, 14},
292 const struct snd_motu_spec snd_motu_spec_audio_express = {
293 .name = "AudioExpress",
294 .protocol_version = SND_MOTU_PROTOCOL_V3,
295 .flags = SND_MOTU_SPEC_RX_MIDI_2ND_Q |
296 SND_MOTU_SPEC_TX_MIDI_3RD_Q,
297 .tx_fixed_pcm_chunks = {10, 10, 0},
298 .rx_fixed_pcm_chunks = {10, 10, 0},
301 const struct snd_motu_spec snd_motu_spec_4pre = {
303 .protocol_version = SND_MOTU_PROTOCOL_V3,
304 .tx_fixed_pcm_chunks = {10, 10, 0},
305 .rx_fixed_pcm_chunks = {10, 10, 0},