2 * ff-protocol-ff400.c - a part of driver for RME Fireface series
4 * Copyright (c) 2015-2017 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
9 #include <linux/delay.h>
12 #define FF400_STF 0x000080100500ull
13 #define FF400_RX_PACKET_FORMAT 0x000080100504ull
14 #define FF400_ISOC_COMM_START 0x000080100508ull
15 #define FF400_TX_PACKET_FORMAT 0x00008010050cull
16 #define FF400_ISOC_COMM_STOP 0x000080100510ull
17 #define FF400_SYNC_STATUS 0x0000801c0000ull
18 #define FF400_FETCH_PCM_FRAMES 0x0000801c0000ull /* For block request. */
19 #define FF400_CLOCK_CONFIG 0x0000801c0004ull
21 #define FF400_MIDI_HIGH_ADDR 0x0000801003f4ull
22 #define FF400_MIDI_RX_PORT_0 0x000080180000ull
23 #define FF400_MIDI_RX_PORT_1 0x000080190000ull
25 static int ff400_get_clock(struct snd_ff *ff, unsigned int *rate,
26 enum snd_ff_clock_src *src)
32 err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
33 FF400_SYNC_STATUS, ®, sizeof(reg), 0);
36 data = le32_to_cpu(reg);
38 /* Calculate sampling rate. */
39 switch ((data >> 1) & 0x03) {
59 /* Calculate source of clock. */
61 *src = SND_FF_CLOCK_SRC_INTERNAL;
63 /* TODO: 0x00, 0x01, 0x02, 0x06, 0x07? */
64 switch ((data >> 10) & 0x07) {
66 *src = SND_FF_CLOCK_SRC_SPDIF;
69 *src = SND_FF_CLOCK_SRC_WORD;
72 *src = SND_FF_CLOCK_SRC_LTC;
76 *src = SND_FF_CLOCK_SRC_ADAT;
84 static int ff400_begin_session(struct snd_ff *ff, unsigned int rate)
89 /* Check whether the given value is supported or not. */
90 for (i = 0; i < CIP_SFC_COUNT; i++) {
91 if (amdtp_rate_table[i] == rate)
94 if (i == CIP_SFC_COUNT)
97 /* Set the number of data blocks transferred in a second. */
98 reg = cpu_to_le32(rate);
99 err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
100 FF400_STF, ®, sizeof(reg), 0);
107 * Set isochronous channel and the number of quadlets of received
110 reg = cpu_to_le32(((ff->rx_stream.data_block_quadlets << 3) << 8) |
111 ff->rx_resources.channel);
112 err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
113 FF400_RX_PACKET_FORMAT, ®, sizeof(reg), 0);
118 * Set isochronous channel and the number of quadlets of transmitted
121 /* TODO: investigate the purpose of this 0x80. */
122 reg = cpu_to_le32((0x80 << 24) |
123 (ff->tx_resources.channel << 5) |
124 (ff->tx_stream.data_block_quadlets));
125 err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
126 FF400_TX_PACKET_FORMAT, ®, sizeof(reg), 0);
130 /* Allow to transmit packets. */
131 reg = cpu_to_le32(0x00000001);
132 return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
133 FF400_ISOC_COMM_START, ®, sizeof(reg), 0);
136 static void ff400_finish_session(struct snd_ff *ff)
140 reg = cpu_to_le32(0x80000000);
141 snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
142 FF400_ISOC_COMM_STOP, ®, sizeof(reg), 0);
145 static int ff400_switch_fetching_mode(struct snd_ff *ff, bool enable)
150 reg = kcalloc(18, sizeof(__le32), GFP_KERNEL);
156 * Each quadlet is corresponding to data channels in a data
157 * blocks in reverse order. Precisely, quadlets for available
158 * data channels should be enabled. Here, I take second best
159 * to fetch PCM frames from all of data channels regardless of
162 for (i = 0; i < 18; ++i)
163 reg[i] = cpu_to_le32(0x00000001);
166 return snd_fw_transaction(ff->unit, TCODE_WRITE_BLOCK_REQUEST,
167 FF400_FETCH_PCM_FRAMES, reg,
168 sizeof(__le32) * 18, 0);
171 static void ff400_dump_sync_status(struct snd_ff *ff,
172 struct snd_info_buffer *buffer)
178 err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
179 FF400_SYNC_STATUS, ®, sizeof(reg), 0);
183 data = le32_to_cpu(reg);
185 snd_iprintf(buffer, "External source detection:\n");
187 snd_iprintf(buffer, "Word Clock:");
188 if ((data >> 24) & 0x20) {
189 if ((data >> 24) & 0x40)
190 snd_iprintf(buffer, "sync\n");
192 snd_iprintf(buffer, "lock\n");
194 snd_iprintf(buffer, "none\n");
197 snd_iprintf(buffer, "S/PDIF:");
198 if ((data >> 16) & 0x10) {
199 if ((data >> 16) & 0x04)
200 snd_iprintf(buffer, "sync\n");
202 snd_iprintf(buffer, "lock\n");
204 snd_iprintf(buffer, "none\n");
207 snd_iprintf(buffer, "ADAT:");
208 if ((data >> 8) & 0x04) {
209 if ((data >> 8) & 0x10)
210 snd_iprintf(buffer, "sync\n");
212 snd_iprintf(buffer, "lock\n");
214 snd_iprintf(buffer, "none\n");
217 snd_iprintf(buffer, "\nUsed external source:\n");
219 if (((data >> 22) & 0x07) == 0x07) {
220 snd_iprintf(buffer, "None\n");
222 switch ((data >> 22) & 0x07) {
224 snd_iprintf(buffer, "ADAT:");
227 snd_iprintf(buffer, "S/PDIF:");
230 snd_iprintf(buffer, "Word:");
233 snd_iprintf(buffer, "Nothing:");
240 snd_iprintf(buffer, "unknown:");
244 if ((data >> 25) & 0x07) {
245 switch ((data >> 25) & 0x07) {
247 snd_iprintf(buffer, "32000\n");
250 snd_iprintf(buffer, "44100\n");
253 snd_iprintf(buffer, "48000\n");
256 snd_iprintf(buffer, "64000\n");
259 snd_iprintf(buffer, "88200\n");
262 snd_iprintf(buffer, "96000\n");
265 snd_iprintf(buffer, "128000\n");
268 snd_iprintf(buffer, "176400\n");
271 snd_iprintf(buffer, "192000\n");
274 snd_iprintf(buffer, "unknown\n");
280 snd_iprintf(buffer, "Multiplied:");
281 snd_iprintf(buffer, "%d\n", (data & 0x3ff) * 250);
284 static void ff400_dump_clock_config(struct snd_ff *ff,
285 struct snd_info_buffer *buffer)
293 err = snd_fw_transaction(ff->unit, TCODE_READ_BLOCK_REQUEST,
294 FF400_CLOCK_CONFIG, ®, sizeof(reg), 0);
298 data = le32_to_cpu(reg);
300 snd_iprintf(buffer, "Output S/PDIF format: %s (Emphasis: %s)\n",
301 (data & 0x20) ? "Professional" : "Consumer",
302 (data & 0x40) ? "on" : "off");
304 snd_iprintf(buffer, "Optical output interface format: %s\n",
305 ((data >> 8) & 0x01) ? "S/PDIF" : "ADAT");
307 snd_iprintf(buffer, "Word output single speed: %s\n",
308 ((data >> 8) & 0x20) ? "on" : "off");
310 snd_iprintf(buffer, "S/PDIF input interface: %s\n",
311 ((data >> 8) & 0x02) ? "Optical" : "Coaxial");
313 switch ((data >> 1) & 0x03) {
330 else if (data & 0x10)
333 snd_iprintf(buffer, "Sampling rate: %d\n", rate);
338 switch ((data >> 10) & 0x07) {
356 snd_iprintf(buffer, "Sync to clock source: %s\n", src);
359 const struct snd_ff_protocol snd_ff_protocol_ff400 = {
360 .get_clock = ff400_get_clock,
361 .begin_session = ff400_begin_session,
362 .finish_session = ff400_finish_session,
363 .switch_fetching_mode = ff400_switch_fetching_mode,
365 .dump_sync_status = ff400_dump_sync_status,
366 .dump_clock_config = ff400_dump_clock_config,
368 .midi_high_addr_reg = FF400_MIDI_HIGH_ADDR,
369 .midi_rx_port_0_reg = FF400_MIDI_RX_PORT_0,
370 .midi_rx_port_1_reg = FF400_MIDI_RX_PORT_1,