1 // SPDX-License-Identifier: GPL-2.0-only
3 * motu-protocol-v2.c - a part of driver for MOTU FireWire series
5 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
10 #define V2_CLOCK_STATUS_OFFSET 0x0b14
11 #define V2_CLOCK_RATE_MASK 0x00000038
12 #define V2_CLOCK_RATE_SHIFT 3
13 #define V2_CLOCK_SRC_MASK 0x00000007
14 #define V2_CLOCK_SRC_SHIFT 0
15 #define V2_CLOCK_TRAVELER_FETCH_DISABLE 0x04000000
16 #define V2_CLOCK_TRAVELER_FETCH_ENABLE 0x03000000
17 #define V2_CLOCK_8PRE_FETCH_DISABLE 0x02000000
18 #define V2_CLOCK_8PRE_FETCH_ENABLE 0x00000000
20 #define V2_IN_OUT_CONF_OFFSET 0x0c04
21 #define V2_OPT_OUT_IFACE_MASK 0x00000c00
22 #define V2_OPT_OUT_IFACE_SHIFT 10
23 #define V2_OPT_IN_IFACE_MASK 0x00000300
24 #define V2_OPT_IN_IFACE_SHIFT 8
25 #define V2_OPT_IFACE_MODE_NONE 0
26 #define V2_OPT_IFACE_MODE_ADAT 1
27 #define V2_OPT_IFACE_MODE_SPDIF 2
29 static int get_clock_rate(u32 data, unsigned int *rate)
31 unsigned int index = (data & V2_CLOCK_RATE_MASK) >> V2_CLOCK_RATE_SHIFT;
32 if (index >= ARRAY_SIZE(snd_motu_clock_rates))
35 *rate = snd_motu_clock_rates[index];
40 static int v2_get_clock_rate(struct snd_motu *motu, unsigned int *rate)
45 err = snd_motu_transaction_read(motu, V2_CLOCK_STATUS_OFFSET, ®,
50 return get_clock_rate(be32_to_cpu(reg), rate);
53 static int v2_set_clock_rate(struct snd_motu *motu, unsigned int rate)
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, V2_CLOCK_STATUS_OFFSET, ®,
71 data = be32_to_cpu(reg);
73 data &= ~V2_CLOCK_RATE_MASK;
74 data |= i << V2_CLOCK_RATE_SHIFT;
76 if (motu->spec == &snd_motu_spec_traveler) {
77 data &= ~V2_CLOCK_TRAVELER_FETCH_ENABLE;
78 data |= V2_CLOCK_TRAVELER_FETCH_DISABLE;
81 reg = cpu_to_be32(data);
82 return snd_motu_transaction_write(motu, V2_CLOCK_STATUS_OFFSET, ®,
86 static int get_clock_source(struct snd_motu *motu, u32 data,
87 enum snd_motu_clock_source *src)
89 unsigned int index = data & V2_CLOCK_SRC_MASK;
95 *src = SND_MOTU_CLOCK_SOURCE_INTERNAL;
101 // To check the configuration of optical interface.
102 int err = snd_motu_transaction_read(motu, V2_IN_OUT_CONF_OFFSET,
107 if (be32_to_cpu(reg) & 0x00000200)
108 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT;
110 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT;
114 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_COAX;
117 *src = SND_MOTU_CLOCK_SOURCE_SPH;
120 *src = SND_MOTU_CLOCK_SOURCE_WORD_ON_BNC;
123 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_DSUB;
132 static int v2_get_clock_source(struct snd_motu *motu,
133 enum snd_motu_clock_source *src)
138 err = snd_motu_transaction_read(motu, V2_CLOCK_STATUS_OFFSET, ®,
143 return get_clock_source(motu, be32_to_cpu(reg), src);
146 static int v2_switch_fetching_mode(struct snd_motu *motu, bool enable)
152 if (motu->spec == &snd_motu_spec_traveler ||
153 motu->spec == &snd_motu_spec_8pre) {
154 err = snd_motu_transaction_read(motu, V2_CLOCK_STATUS_OFFSET,
158 data = be32_to_cpu(reg);
160 if (motu->spec == &snd_motu_spec_traveler) {
161 data &= ~(V2_CLOCK_TRAVELER_FETCH_DISABLE |
162 V2_CLOCK_TRAVELER_FETCH_ENABLE);
165 data |= V2_CLOCK_TRAVELER_FETCH_ENABLE;
167 data |= V2_CLOCK_TRAVELER_FETCH_DISABLE;
168 } else if (motu->spec == &snd_motu_spec_8pre) {
169 data &= ~(V2_CLOCK_8PRE_FETCH_DISABLE |
170 V2_CLOCK_8PRE_FETCH_ENABLE);
173 data |= V2_CLOCK_8PRE_FETCH_DISABLE;
175 data |= V2_CLOCK_8PRE_FETCH_ENABLE;
178 reg = cpu_to_be32(data);
179 err = snd_motu_transaction_write(motu, V2_CLOCK_STATUS_OFFSET,
186 static void calculate_fixed_part(struct snd_motu_packet_format *formats,
187 enum amdtp_stream_direction dir,
188 enum snd_motu_spec_flags flags,
189 unsigned char analog_ports)
191 unsigned char pcm_chunks[3] = {0, 0, 0};
193 formats->msg_chunks = 2;
195 pcm_chunks[0] = analog_ports;
196 pcm_chunks[1] = analog_ports;
197 if (flags & SND_MOTU_SPEC_SUPPORT_CLOCK_X4)
198 pcm_chunks[2] = analog_ports;
200 if (dir == AMDTP_IN_STREAM) {
201 if (flags & SND_MOTU_SPEC_TX_MICINST_CHUNK) {
205 if (flags & SND_MOTU_SPEC_TX_RETURN_CHUNK) {
210 if (flags & SND_MOTU_SPEC_RX_SEPARATED_MAIN) {
215 // Packets to v2 units include 2 chunks for phone 1/2, except
216 // for 176.4/192.0 kHz.
221 if (flags & SND_MOTU_SPEC_HAS_AESEBU_IFACE) {
227 * All of v2 models have a pair of coaxial interfaces for digital in/out
228 * port. At 44.1/48.0/88.2/96.0 kHz, packets includes PCM from these
234 formats->fixed_part_pcm_chunks[0] = pcm_chunks[0];
235 formats->fixed_part_pcm_chunks[1] = pcm_chunks[1];
236 formats->fixed_part_pcm_chunks[2] = pcm_chunks[2];
239 static void calculate_differed_part(struct snd_motu_packet_format *formats,
240 enum snd_motu_spec_flags flags,
241 u32 data, u32 mask, u32 shift)
243 unsigned char pcm_chunks[2] = {0, 0};
246 * When optical interfaces are configured for S/PDIF (TOSLINK),
247 * the above PCM frames come from them, instead of coaxial
250 data = (data & mask) >> shift;
251 if (data == V2_OPT_IFACE_MODE_ADAT) {
252 if (flags & SND_MOTU_SPEC_HAS_OPT_IFACE_A) {
256 // 8pre has two sets of optical interface and doesn't reduce
257 // chunks for ADAT signals.
258 if (flags & SND_MOTU_SPEC_HAS_OPT_IFACE_B) {
263 /* At mode x4, no data chunks are supported in this part. */
264 formats->differed_part_pcm_chunks[0] = pcm_chunks[0];
265 formats->differed_part_pcm_chunks[1] = pcm_chunks[1];
268 static int v2_cache_packet_formats(struct snd_motu *motu)
274 err = snd_motu_transaction_read(motu, V2_IN_OUT_CONF_OFFSET, ®,
278 data = be32_to_cpu(reg);
280 calculate_fixed_part(&motu->tx_packet_formats, AMDTP_IN_STREAM,
281 motu->spec->flags, motu->spec->analog_in_ports);
282 calculate_differed_part(&motu->tx_packet_formats, motu->spec->flags,
283 data, V2_OPT_IN_IFACE_MASK, V2_OPT_IN_IFACE_SHIFT);
285 calculate_fixed_part(&motu->rx_packet_formats, AMDTP_OUT_STREAM,
286 motu->spec->flags, motu->spec->analog_out_ports);
287 calculate_differed_part(&motu->rx_packet_formats, motu->spec->flags,
288 data, V2_OPT_OUT_IFACE_MASK, V2_OPT_OUT_IFACE_SHIFT);
290 motu->tx_packet_formats.pcm_byte_offset = 10;
291 motu->rx_packet_formats.pcm_byte_offset = 10;
296 const struct snd_motu_protocol snd_motu_protocol_v2 = {
297 .get_clock_rate = v2_get_clock_rate,
298 .set_clock_rate = v2_set_clock_rate,
299 .get_clock_source = v2_get_clock_source,
300 .switch_fetching_mode = v2_switch_fetching_mode,
301 .cache_packet_formats = v2_cache_packet_formats,