2 * motu-protocol-v2.c - a part of driver for MOTU FireWire series
4 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6 * Licensed under the terms of the GNU General Public License, version 2.
11 #define V2_CLOCK_STATUS_OFFSET 0x0b14
12 #define V2_CLOCK_RATE_MASK 0x00000038
13 #define V2_CLOCK_RATE_SHIFT 3
14 #define V2_CLOCK_SRC_MASK 0x00000007
15 #define V2_CLOCK_SRC_SHIFT 0
17 #define V2_IN_OUT_CONF_OFFSET 0x0c04
18 #define V2_OPT_OUT_IFACE_MASK 0x00000c00
19 #define V2_OPT_OUT_IFACE_SHIFT 10
20 #define V2_OPT_IN_IFACE_MASK 0x00000300
21 #define V2_OPT_IN_IFACE_SHIFT 8
22 #define V2_OPT_IFACE_MODE_NONE 0
23 #define V2_OPT_IFACE_MODE_ADAT 1
24 #define V2_OPT_IFACE_MODE_SPDIF 2
26 static int v2_get_clock_rate(struct snd_motu *motu, unsigned int *rate)
32 err = snd_motu_transaction_read(motu, V2_CLOCK_STATUS_OFFSET, ®,
37 index = (be32_to_cpu(reg) & V2_CLOCK_RATE_MASK) >> V2_CLOCK_RATE_SHIFT;
38 if (index >= ARRAY_SIZE(snd_motu_clock_rates))
41 *rate = snd_motu_clock_rates[index];
46 static int v2_set_clock_rate(struct snd_motu *motu, unsigned int rate)
53 for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
54 if (snd_motu_clock_rates[i] == rate)
57 if (i == ARRAY_SIZE(snd_motu_clock_rates))
60 err = snd_motu_transaction_read(motu, V2_CLOCK_STATUS_OFFSET, ®,
64 data = be32_to_cpu(reg);
66 data &= ~V2_CLOCK_RATE_MASK;
67 data |= i << V2_CLOCK_RATE_SHIFT;
69 reg = cpu_to_be32(data);
70 return snd_motu_transaction_write(motu, V2_CLOCK_STATUS_OFFSET, ®,
74 static int v2_get_clock_source(struct snd_motu *motu,
75 enum snd_motu_clock_source *src)
81 err = snd_motu_transaction_read(motu, V2_CLOCK_STATUS_OFFSET, ®,
86 index = be32_to_cpu(reg) & V2_CLOCK_SRC_MASK;
90 /* To check the configuration of optical interface. */
91 err = snd_motu_transaction_read(motu, V2_IN_OUT_CONF_OFFSET, ®,
98 *src = SND_MOTU_CLOCK_SOURCE_INTERNAL;
101 if (be32_to_cpu(reg) & 0x00000200)
102 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT;
104 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT;
107 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_COAX;
110 *src = SND_MOTU_CLOCK_SOURCE_WORD_ON_BNC;
113 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_DSUB;
122 static int v2_switch_fetching_mode(struct snd_motu *motu, bool enable)
124 /* V2 protocol doesn't have this feature. */
128 static void calculate_fixed_part(struct snd_motu_packet_format *formats,
129 enum amdtp_stream_direction dir,
130 enum snd_motu_spec_flags flags,
131 unsigned char analog_ports)
133 unsigned char pcm_chunks[3] = {0, 0, 0};
135 formats->msg_chunks = 2;
137 pcm_chunks[0] = analog_ports;
138 pcm_chunks[1] = analog_ports;
139 if (flags & SND_MOTU_SPEC_SUPPORT_CLOCK_X4)
140 pcm_chunks[2] = analog_ports;
142 if (dir == AMDTP_IN_STREAM) {
143 if (flags & SND_MOTU_SPEC_TX_MICINST_CHUNK) {
147 if (flags & SND_MOTU_SPEC_TX_RETURN_CHUNK) {
153 * Packets to v2 units transfer main-out-1/2 and phone-out-1/2.
160 * All of v2 models have a pair of coaxial interfaces for digital in/out
161 * port. At 44.1/48.0/88.2/96.0 kHz, packets includes PCM from these
167 /* This part should be multiples of 4. */
168 formats->fixed_part_pcm_chunks[0] = round_up(2 + pcm_chunks[0], 4) - 2;
169 formats->fixed_part_pcm_chunks[1] = round_up(2 + pcm_chunks[1], 4) - 2;
170 if (flags & SND_MOTU_SPEC_SUPPORT_CLOCK_X4)
171 formats->fixed_part_pcm_chunks[2] =
172 round_up(2 + pcm_chunks[2], 4) - 2;
175 static void calculate_differed_part(struct snd_motu_packet_format *formats,
176 enum snd_motu_spec_flags flags,
177 u32 data, u32 mask, u32 shift)
179 unsigned char pcm_chunks[3] = {0, 0};
182 * When optical interfaces are configured for S/PDIF (TOSLINK),
183 * the above PCM frames come from them, instead of coaxial
186 data = (data & mask) >> shift;
187 if ((flags & SND_MOTU_SPEC_HAS_OPT_IFACE_A) &&
188 data == V2_OPT_IFACE_MODE_ADAT) {
193 /* At mode x4, no data chunks are supported in this part. */
194 formats->differed_part_pcm_chunks[0] = pcm_chunks[0];
195 formats->differed_part_pcm_chunks[1] = pcm_chunks[1];
198 static int v2_cache_packet_formats(struct snd_motu *motu)
204 err = snd_motu_transaction_read(motu, V2_IN_OUT_CONF_OFFSET, ®,
208 data = be32_to_cpu(reg);
210 calculate_fixed_part(&motu->tx_packet_formats, AMDTP_IN_STREAM,
211 motu->spec->flags, motu->spec->analog_in_ports);
212 calculate_differed_part(&motu->tx_packet_formats, motu->spec->flags,
213 data, V2_OPT_IN_IFACE_MASK, V2_OPT_IN_IFACE_SHIFT);
215 calculate_fixed_part(&motu->rx_packet_formats, AMDTP_OUT_STREAM,
216 motu->spec->flags, motu->spec->analog_out_ports);
217 calculate_differed_part(&motu->rx_packet_formats, motu->spec->flags,
218 data, V2_OPT_OUT_IFACE_MASK, V2_OPT_OUT_IFACE_SHIFT);
220 motu->tx_packet_formats.pcm_byte_offset = 10;
221 motu->rx_packet_formats.pcm_byte_offset = 10;
226 const struct snd_motu_protocol snd_motu_protocol_v2 = {
227 .get_clock_rate = v2_get_clock_rate,
228 .set_clock_rate = v2_set_clock_rate,
229 .get_clock_source = v2_get_clock_source,
230 .switch_fetching_mode = v2_switch_fetching_mode,
231 .cache_packet_formats = v2_cache_packet_formats,