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_CLOCK_CONFIG, ®, 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)
151 reg = kcalloc(18, sizeof(__le32), GFP_KERNEL);
157 * Each quadlet is corresponding to data channels in a data
158 * blocks in reverse order. Precisely, quadlets for available
159 * data channels should be enabled. Here, I take second best
160 * to fetch PCM frames from all of data channels regardless of
163 for (i = 0; i < 18; ++i)
164 reg[i] = cpu_to_le32(0x00000001);
167 err = snd_fw_transaction(ff->unit, TCODE_WRITE_BLOCK_REQUEST,
168 FF400_FETCH_PCM_FRAMES, reg,
169 sizeof(__le32) * 18, 0);
174 static void ff400_dump_sync_status(struct snd_ff *ff,
175 struct snd_info_buffer *buffer)
181 err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
182 FF400_SYNC_STATUS, ®, sizeof(reg), 0);
186 data = le32_to_cpu(reg);
188 snd_iprintf(buffer, "External source detection:\n");
190 snd_iprintf(buffer, "Word Clock:");
191 if ((data >> 24) & 0x20) {
192 if ((data >> 24) & 0x40)
193 snd_iprintf(buffer, "sync\n");
195 snd_iprintf(buffer, "lock\n");
197 snd_iprintf(buffer, "none\n");
200 snd_iprintf(buffer, "S/PDIF:");
201 if ((data >> 16) & 0x10) {
202 if ((data >> 16) & 0x04)
203 snd_iprintf(buffer, "sync\n");
205 snd_iprintf(buffer, "lock\n");
207 snd_iprintf(buffer, "none\n");
210 snd_iprintf(buffer, "ADAT:");
211 if ((data >> 8) & 0x04) {
212 if ((data >> 8) & 0x10)
213 snd_iprintf(buffer, "sync\n");
215 snd_iprintf(buffer, "lock\n");
217 snd_iprintf(buffer, "none\n");
220 snd_iprintf(buffer, "\nUsed external source:\n");
222 if (((data >> 22) & 0x07) == 0x07) {
223 snd_iprintf(buffer, "None\n");
225 switch ((data >> 22) & 0x07) {
227 snd_iprintf(buffer, "ADAT:");
230 snd_iprintf(buffer, "S/PDIF:");
233 snd_iprintf(buffer, "Word:");
236 snd_iprintf(buffer, "Nothing:");
243 snd_iprintf(buffer, "unknown:");
247 if ((data >> 25) & 0x07) {
248 switch ((data >> 25) & 0x07) {
250 snd_iprintf(buffer, "32000\n");
253 snd_iprintf(buffer, "44100\n");
256 snd_iprintf(buffer, "48000\n");
259 snd_iprintf(buffer, "64000\n");
262 snd_iprintf(buffer, "88200\n");
265 snd_iprintf(buffer, "96000\n");
268 snd_iprintf(buffer, "128000\n");
271 snd_iprintf(buffer, "176400\n");
274 snd_iprintf(buffer, "192000\n");
277 snd_iprintf(buffer, "unknown\n");
283 snd_iprintf(buffer, "Multiplied:");
284 snd_iprintf(buffer, "%d\n", (data & 0x3ff) * 250);
287 static void ff400_dump_clock_config(struct snd_ff *ff,
288 struct snd_info_buffer *buffer)
296 err = snd_fw_transaction(ff->unit, TCODE_READ_BLOCK_REQUEST,
297 FF400_CLOCK_CONFIG, ®, sizeof(reg), 0);
301 data = le32_to_cpu(reg);
303 snd_iprintf(buffer, "Output S/PDIF format: %s (Emphasis: %s)\n",
304 (data & 0x20) ? "Professional" : "Consumer",
305 (data & 0x40) ? "on" : "off");
307 snd_iprintf(buffer, "Optical output interface format: %s\n",
308 ((data >> 8) & 0x01) ? "S/PDIF" : "ADAT");
310 snd_iprintf(buffer, "Word output single speed: %s\n",
311 ((data >> 8) & 0x20) ? "on" : "off");
313 snd_iprintf(buffer, "S/PDIF input interface: %s\n",
314 ((data >> 8) & 0x02) ? "Optical" : "Coaxial");
316 switch ((data >> 1) & 0x03) {
333 else if (data & 0x10)
336 snd_iprintf(buffer, "Sampling rate: %d\n", rate);
341 switch ((data >> 10) & 0x07) {
359 snd_iprintf(buffer, "Sync to clock source: %s\n", src);
362 const struct snd_ff_protocol snd_ff_protocol_ff400 = {
363 .get_clock = ff400_get_clock,
364 .begin_session = ff400_begin_session,
365 .finish_session = ff400_finish_session,
366 .switch_fetching_mode = ff400_switch_fetching_mode,
368 .dump_sync_status = ff400_dump_sync_status,
369 .dump_clock_config = ff400_dump_clock_config,
371 .midi_high_addr_reg = FF400_MIDI_HIGH_ADDR,
372 .midi_rx_port_0_reg = FF400_MIDI_RX_PORT_0,
373 .midi_rx_port_1_reg = FF400_MIDI_RX_PORT_1,