Clean up compiler warnings when compiling on 64-bit systems. These are mostly fixing...
[ffado.git] / libffado / src / rme / fireface_hw.cpp
blobe0c18377490891adcc2f8fcc1641cd0114d97ea0
1 /*
2 * Copyright (C) 2009 by Jonathan Woithe
4 * This file is part of FFADO
5 * FFADO = Free Firewire (pro-)audio drivers for linux
7 * FFADO is based upon FreeBoB.
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) version 3 of the License.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* This file implements miscellaneous lower-level hardware functions for the Fireface */
26 #include <math.h>
28 #include "libieee1394/configrom.h"
29 #include "libieee1394/ieee1394service.h"
31 #include "rme/rme_avdevice.h"
32 #include "rme/fireface_def.h"
34 #include "debugmodule/debugmodule.h"
36 namespace Rme {
38 unsigned int
39 Device::multiplier_of_freq(unsigned int freq)
41 if (freq > MIN_QUAD_SPEED)
42 return 4;
43 if (freq > MIN_DOUBLE_SPEED)
44 return 2;
45 return 1;
48 void
49 Device::config_lock(void) {
50 rme_shm_lock(dev_config);
53 void
54 Device::config_unlock(void) {
55 rme_shm_unlock(dev_config);
58 signed int
59 Device::init_hardware(void)
61 signed int ret = 0;
62 signed int src, dest;
63 signed int n_channels;
64 signed int have_mixer_settings = 0;
66 switch (m_rme_model) {
67 case RME_MODEL_FIREFACE400: n_channels = RME_FF400_MAX_CHANNELS; break;
68 case RME_MODEL_FIREFACE800: n_channels = RME_FF800_MAX_CHANNELS; break;
69 default:
70 debugOutput(DEBUG_LEVEL_ERROR, "unknown model %d\n", m_rme_model);
71 return -1;
74 // Initialises the device's settings structure to a known state and then
75 // sets the hardware to reflect this state.
77 config_lock();
79 // If the software state is not yet valid, attempt to obtain settings
80 // from the device flash. If that fails for some reason, initialise
81 // with a static setup.
82 if (dev_config->settings_valid == 0) {
83 dev_config->settings_valid = read_device_flash_settings(settings) == 0;
84 // If valid settings were read, write them to the device so we can
85 // be sure that this mirrors how the device is currently configured.
86 // This is also needed so the "host" LED is extinguished on first
87 // use after power up. Also use the stored sample rate as the
88 // operational rate.
89 if (dev_config->settings_valid) {
90 dev_config->software_freq = settings->sample_rate;
91 // dds_freq can be used to run the audio clock at a slightly
92 // different frequency to what the software requests (to allow
93 // for drop-frame rates for example). For the moment FFADO
94 // provides no explicit support for this, so dds_freq should
95 // always be zero.
97 // If user access to dds_freq is implemented in future, it may
98 // or may not be desireable to set dds_freq from
99 // settings->sample_rate. This will probably be determined by
100 // the nature of the user interface to dds_freq.
102 // See also comments in getSamplingFrequency() and
103 // setDDSFrequency().
104 dev_config->dds_freq = 0;
105 set_hardware_params(settings);
109 // If no valid flash settings, configure with a static setup.
110 if (dev_config->settings_valid == 0) {
111 debugOutput(DEBUG_LEVEL_VERBOSE, "flash settings unavailable or invalid; using defaults\n");
112 memset(settings, 0, sizeof(*settings));
113 settings->spdif_input_mode = FF_SWPARAM_SPDIF_INPUT_COAX;
114 settings->spdif_output_mode = FF_SWPARAM_SPDIF_OUTPUT_COAX;
115 settings->clock_mode = FF_SWPARAM_CLOCK_MODE_MASTER;
116 settings->sync_ref = FF_SWPARAM_SYNCREF_WORDCLOCK;
117 settings->input_level = FF_SWPARAM_ILEVEL_LOGAIN;
118 settings->output_level = FF_SWPARAM_OLEVEL_HIGAIN;
119 settings->phones_level = FF_SWPARAM_PHONESLEVEL_HIGAIN;
120 settings->limit_bandwidth = FF_SWPARAM_BWLIMIT_SEND_ALL_CHANNELS;
122 // A default sampling rate. An explicit DDS frequency is not enabled
123 // by default (FFADO doesn't currently make explicit use of this - see
124 // above).
125 dev_config->software_freq = 44100;
126 dev_config->dds_freq = 0;
127 settings->sample_rate = dev_config->software_freq;
129 // TODO: set input amplifier gains to a value other than 0?
131 // TODO: store and manipulate channel mute/rec flags
133 // The FF800 needs the input source set via the input options.
134 // The device's default has the limiter enabled so we'll follow
135 // that convention.
136 if (m_rme_model == RME_MODEL_FIREFACE800) {
137 settings->input_opt[0] = settings->input_opt[1] =
138 settings->input_opt[2] = FF_SWPARAM_FF800_INPUT_OPT_FRONT;
139 settings->limiter = 1;
142 // Configure the hardware to match the current software status.
143 // This is only done if the settings valid flag is 0; if it is 1 it
144 // indicates that something has already set the device up to match
145 // the software settings so there's no need to do it again.
147 if (set_hardware_params(settings) != 0)
148 ret = -1;
150 if (ret==0) {
151 signed freq = dev_config->software_freq;
152 if (dev_config->dds_freq > 0)
153 freq = dev_config->dds_freq;
154 if (set_hardware_dds_freq(freq) != 0)
155 ret = -1;
158 if (m_rme_model == RME_MODEL_FIREFACE400) {
159 signed int i;
160 for (i=FF400_AMPGAIN_MIC1; i<=FF400_AMPGAIN_INPUT4; i++) {
161 set_hardware_ampgain(i, settings->amp_gains[i]);
165 dev_config->settings_valid = 1;
168 have_mixer_settings = read_device_mixer_settings(settings) == 0;
170 // Matrix mixer settings
171 for (dest=0; dest<n_channels; dest++) {
172 for (src=0; src<n_channels; src++) {
173 if (!have_mixer_settings)
174 settings->input_faders[getMixerGainIndex(src, dest)] = 0;
175 set_hardware_mixergain(RME_FF_MM_INPUT, src, dest, settings->input_faders[getMixerGainIndex(src, dest)]);
177 for (src=0; src<n_channels; src++) {
178 if (!have_mixer_settings)
179 settings->playback_faders[getMixerGainIndex(src, dest)] =
180 src==dest?0x8000:0;
181 set_hardware_mixergain(RME_FF_MM_PLAYBACK, src, dest,
182 settings->playback_faders[getMixerGainIndex(src, dest)]);
185 for (src=0; src<n_channels; src++) {
186 if (!have_mixer_settings)
187 settings->output_faders[src] = 0x8000;
188 set_hardware_mixergain(RME_FF_MM_OUTPUT, src, 0, settings->output_faders[src]);
191 set_hardware_output_rec(0);
193 if (ret==0 && m_rme_model==RME_MODEL_FIREFACE400) {
194 unsigned int node_id = getConfigRom().getNodeId();
195 unsigned int midi_hi_addr;
196 // For now we'll fix this since that's what's done under other
197 // systems.
198 midi_hi_addr = 0x01;
199 if (writeRegister(RME_FF400_MIDI_HIGH_ADDR, (node_id<<16) | midi_hi_addr) != 0) {
200 debugOutput(DEBUG_LEVEL_ERROR, "failed to write MIDI high address register\n");
201 ret = -1;
205 // Also configure the TCO (Time Code Option) settings for those devices
206 // which have a TCO.
207 if (ret==0 && dev_config->tco_settings_valid==0) {
208 if (dev_config->tco_present) {
209 memset(tco_settings, 0, sizeof(*tco_settings));
210 if (write_tco_settings(tco_settings) != 0) {
211 debugOutput(DEBUG_LEVEL_ERROR, "failed to write TCO settings\n");
214 dev_config->tco_settings_valid = 1;
217 config_unlock();
219 return ret;
222 signed int
223 Device::get_hardware_status(unsigned int *stat0, unsigned int *stat1)
225 unsigned int buf[2];
226 if (readBlock(RME_FF_STATUS_REG0, buf, 2) != 0)
227 return -1;
228 *stat0 = buf[0];
229 *stat1 = buf[1];
230 return 0;
233 signed int
234 Device::get_hardware_streaming_status(unsigned int *stat, unsigned int n)
236 // Get the hardware status as it applies to the streaming system. This
237 // involves a request of 4 quadlets from the status register. It
238 // appears that the first register's definition is slightly different in
239 // this situation compared to when only 2 quadlets are requested as is
240 // done in get_hardware_status().
242 // "n" is the size of the passed-in stat array. It must be >= 4.
243 if (n < 4)
244 return -1;
245 if (readBlock(RME_FF_STATUS_REG0, stat, 4) != 0)
246 return -1;
247 return 0;
250 signed int
251 Device::get_hardware_state(FF_state_t *state)
253 // Retrieve the hardware status and deduce the device state. Return
254 // -1 on error, 0 on success. The given state structure will be
255 // cleared by this call.
256 static signed int call_count = 0;
257 unsigned int stat0, stat1;
258 memset(state, 0, sizeof(*state));
259 if (get_hardware_status(&stat0, &stat1) != 0)
260 return -1;
262 state->is_streaming = dev_config->is_streaming;
264 state->clock_mode = (settings->clock_mode == FF_SWPARAM_CLOCK_MODE_MASTER)?FF_STATE_CLOCKMODE_MASTER:FF_STATE_CLOCKMODE_AUTOSYNC;
266 switch (stat0 & SR0_AUTOSYNC_SRC_MASK) {
267 case SR0_AUTOSYNC_SRC_ADAT1:
268 state->autosync_source = FF_STATE_AUTOSYNC_SRC_ADAT1;
269 break;
270 case SR0_AUTOSYNC_SRC_ADAT2:
271 state->autosync_source = FF_STATE_AUTOSYNC_SRC_ADAT2;
272 break;
273 case SR0_AUTOSYNC_SRC_SPDIF:
274 state->autosync_source = FF_STATE_AUTOSYNC_SRC_SPDIF;
275 break;
276 case SR0_AUTOSYNC_SRC_WCLK:
277 state->autosync_source = FF_STATE_AUTOSYNC_SRC_WCLK;
278 break;
279 case SR0_AUTOSYNC_SRC_TCO:
280 state->autosync_source = FF_STATE_AUTOSYNC_SRC_TCO;
281 break;
282 default: state->autosync_source = FF_STATE_AUTOSYNC_SRC_NOLOCK;
285 switch (stat0 & SR0_AUTOSYNC_FREQ_MASK) {
286 case SR0_AUTOSYNC_FREQ_32k: state->autosync_freq = 32000; break;
287 case SR0_AUTOSYNC_FREQ_44k1: state->autosync_freq = 44100; break;
288 case SR0_AUTOSYNC_FREQ_48k: state->autosync_freq = 48000; break;
289 case SR0_AUTOSYNC_FREQ_64k: state->autosync_freq = 64000; break;
290 case SR0_AUTOSYNC_FREQ_88k2: state->autosync_freq = 88200; break;
291 case SR0_AUTOSYNC_FREQ_96k: state->autosync_freq = 96000; break;
292 case SR0_AUTOSYNC_FREQ_128k: state->autosync_freq = 128000; break;
293 case SR0_AUTOSYNC_FREQ_176k4:state->autosync_freq = 176400; break;
294 case SR0_AUTOSYNC_FREQ_192k: state->autosync_freq = 192000; break;
297 switch (stat0 & SR0_SPDIF_FREQ_MASK) {
298 case SR0_SPDIF_FREQ_32k: state->spdif_freq = 32000; break;
299 case SR0_SPDIF_FREQ_44k1: state->spdif_freq = 41000; break;
300 case SR0_SPDIF_FREQ_48k: state->spdif_freq = 48000; break;
301 case SR0_SPDIF_FREQ_64k: state->spdif_freq = 64000; break;
302 case SR0_SPDIF_FREQ_88k2: state->spdif_freq = 88200; break;
303 case SR0_SPDIF_FREQ_96k: state->spdif_freq = 96000; break;
304 case SR0_SPDIF_FREQ_128k: state->spdif_freq = 128000; break;
305 case SR0_SPDIF_FREQ_176k4:state->spdif_freq = 176400; break;
306 case SR0_SPDIF_FREQ_192k: state->spdif_freq = 192000; break;
309 switch (stat0 & SR0_ADAT1_STATUS_MASK) {
310 case SR0_ADAT1_STATUS_NOLOCK:
311 state->adat1_sync_status = FF_STATE_SYNC_NOLOCK; break;
312 case SR0_ADAT1_STATUS_LOCK:
313 state->adat1_sync_status = FF_STATE_SYNC_LOCKED; break;
314 case SR0_ADAT1_STATUS_SYNC:
315 state->adat1_sync_status = FF_STATE_SYNC_SYNCED; break;
317 switch (stat0 & SR0_ADAT2_STATUS_MASK) {
318 case SR0_ADAT2_STATUS_NOLOCK:
319 state->adat2_sync_status = FF_STATE_SYNC_NOLOCK; break;
320 case SR0_ADAT2_STATUS_LOCK:
321 state->adat2_sync_status = FF_STATE_SYNC_LOCKED; break;
322 case SR0_ADAT2_STATUS_SYNC:
323 state->adat2_sync_status = FF_STATE_SYNC_SYNCED; break;
325 switch (stat0 & SR0_SPDIF_STATUS_MASK) {
326 case SR0_SPDIF_STATUS_NOLOCK:
327 state->spdif_sync_status = FF_STATE_SYNC_NOLOCK; break;
328 case SR0_SPDIF_STATUS_LOCK:
329 state->spdif_sync_status = FF_STATE_SYNC_LOCKED; break;
330 case SR0_SPDIF_STATUS_SYNC:
331 state->spdif_sync_status = FF_STATE_SYNC_SYNCED; break;
333 switch (stat0 & SR0_WCLK_STATUS_MASK) {
334 case SR0_WCLK_STATUS_NOLOCK:
335 state->wclk_sync_status = FF_STATE_SYNC_NOLOCK; break;
336 case SR0_WCLK_STATUS_LOCK:
337 state->wclk_sync_status = FF_STATE_SYNC_LOCKED; break;
338 case SR0_WCLK_STATUS_SYNC:
339 state->wclk_sync_status = FF_STATE_SYNC_SYNCED; break;
341 switch (stat1 & SR1_TCO_STATUS_MASK) {
342 case SR1_TCO_STATUS_NOLOCK:
343 state->tco_sync_status = FF_STATE_SYNC_NOLOCK; break;
344 case SR1_TCO_STATUS_LOCK:
345 state->tco_sync_status = FF_STATE_SYNC_LOCKED; break;
346 case SR1_TCO_STATUS_SYNC:
347 state->tco_sync_status = FF_STATE_SYNC_SYNCED; break;
350 // Report the state reported by the hardware if debug output is active.
351 // Only do this for the first few calls.
352 if (call_count < 2) {
353 debugOutput(DEBUG_LEVEL_VERBOSE, "State reported by hardware:\n");
354 debugOutput(DEBUG_LEVEL_VERBOSE, " is_streaming: %d\n", state->is_streaming);
355 debugOutput(DEBUG_LEVEL_VERBOSE, " clock_mode: %s\n", state->clock_mode==FF_STATE_CLOCKMODE_MASTER?"master":"autosync/slave");
356 debugOutput(DEBUG_LEVEL_VERBOSE, " autosync source: %d\n", state->autosync_source);
357 debugOutput(DEBUG_LEVEL_VERBOSE, " autosync freq: %d\n", state->autosync_freq);
358 debugOutput(DEBUG_LEVEL_VERBOSE, " spdif freq: %d\n", state->spdif_freq);
359 debugOutput(DEBUG_LEVEL_VERBOSE, " ADAT 1/2 status: %x, %x\n", state->adat1_sync_status, state->adat2_sync_status);
360 debugOutput(DEBUG_LEVEL_VERBOSE, " SDPIF status: %x\n", state->spdif_sync_status);
361 debugOutput(DEBUG_LEVEL_VERBOSE, " Wclk/tco status: %x, %x\n", state->wclk_sync_status, state->tco_sync_status);
362 call_count++;
364 return 0;
367 signed int
368 Device::set_hardware_params(FF_software_settings_t *use_settings)
370 // Initialises the hardware to the state defined by the supplied
371 // software settings structure (which will usually be the device's
372 // "settings" structure). This has the side effect of extinguishing the
373 // "Host" LED on the FF400 when done for the first time after the
374 // interface has been powered up.
376 // If use_settings is NULL, the device's current settings structure will
377 // be used to source the configuration information.
379 FF_software_settings_t *sw_settings;
380 quadlet_t data[3] = {0, 0, 0};
381 unsigned int conf_reg;
383 if (use_settings == NULL)
384 sw_settings = settings;
385 else
386 sw_settings = use_settings;
388 if (sw_settings->mic_phantom[0])
389 data[0] |= CR0_PHANTOM_MIC0;
390 if (sw_settings->mic_phantom[1])
391 data[0] |= CR0_PHANTOM_MIC1;
392 switch (m_rme_model) {
393 case RME_MODEL_FIREFACE800:
394 if (sw_settings->mic_phantom[2])
395 data[0] |= CR0_FF800_PHANTOM_MIC9;
396 if (sw_settings->mic_phantom[3])
397 data[0] |= CR0_FF800_PHANTOM_MIC10;
398 break;
399 case RME_MODEL_FIREFACE400:
400 if (sw_settings->ff400_input_pad[0])
401 data[0] |= CR0_FF400_CH3_PAD;
402 if (sw_settings->ff400_input_pad[1])
403 data[0] |= CR0_FF400_CH4_PAD;
404 break;
405 default:
406 break;
409 /* Phones level */
410 if (m_rme_model == RME_MODEL_FIREFACE400) {
411 switch (sw_settings->phones_level) {
412 case FF_SWPARAM_PHONESLEVEL_HIGAIN:
413 data[0] |= CRO_PHLEVEL_HIGAIN;
414 break;
415 case FF_SWPARAM_PHONESLEVEL_4dBU:
416 data[0] |= CR0_PHLEVEL_4dBU;
417 break;
418 case FF_SWPARAM_PHONESLEVEL_m10dBV:
419 data[0] |= CRO_PHLEVEL_m10dBV;
420 break;
424 /* Input level */
425 switch (sw_settings->input_level) {
426 case FF_SWPARAM_ILEVEL_LOGAIN: // Low gain
427 data[1] |= CR1_ILEVEL_CPLD_LOGAIN; // CPLD
428 data[0] |= CR0_ILEVEL_FPGA_LOGAIN; // LED control (used on FF800 only)
429 break;
430 case FF_SWPARAM_ILEVEL_4dBU: // +4 dBu
431 data[1] |= CR1_ILEVEL_CPLD_4dBU;
432 data[0] |= CR0_ILEVEL_FPGA_4dBU;
433 break;
434 case FF_SWPARAM_ILEVEL_m10dBV: // -10 dBV
435 data[1] |= CR1_ILEVEL_CPLD_m10dBV;
436 data[0] |= CR0_ILEVEL_FPGA_m10dBV;
437 break;
440 /* Output level */
441 switch (sw_settings->output_level) {
442 case FF_SWPARAM_OLEVEL_HIGAIN: // High gain
443 data[1] |= CR1_OLEVEL_CPLD_HIGAIN; // CPLD
444 data[0] |= CR0_OLEVEL_FPGA_HIGAIN; // LED control (used on FF800 only)
445 break;
446 case FF_SWPARAM_OLEVEL_4dBU: // +4 dBu
447 data[1] |= CR1_OLEVEL_CPLD_4dBU;
448 data[0] |= CR0_OLEVEL_FPGA_4dBU;
449 break;
450 case FF_SWPARAM_OLEVEL_m10dBV: // -10 dBV
451 data[1] |= CR1_OLEVEL_CPLD_m10dBV;
452 data[0] |= CR0_OLEVEL_FPGA_m10dBV;
453 break;
456 /* Set input options. The meaning of the options differs between
457 * devices, so we use the generic identifiers here.
459 data[1] |= (sw_settings->input_opt[1] & FF_SWPARAM_INPUT_OPT_A) ? CR1_INPUT_OPT1_A : 0;
460 data[1] |= (sw_settings->input_opt[1] & FF_SWPARAM_INPUT_OPT_B) ? CR1_INPUT_OPT1_B : 0;
461 data[1] |= (sw_settings->input_opt[2] & FF_SWPARAM_INPUT_OPT_A) ? CR1_INPUT_OPT2_A : 0;
462 data[1] |= (sw_settings->input_opt[2] & FF_SWPARAM_INPUT_OPT_B) ? CR1_INPUT_OPT2_B : 0;
464 // Drive the speaker emulation / filter LED via FPGA in FF800. In FF400
465 // the same bit controls the channel 4 "instrument" option.
466 if (m_rme_model == RME_MODEL_FIREFACE800) {
467 data[0] |= (sw_settings->filter) ? CR0_FF800_FILTER_FPGA : 0;
468 } else
469 if (m_rme_model == RME_MODEL_FIREFACE400) {
470 data[0] |= (sw_settings->ff400_instr_input[1]) ? CR0_FF400_CH4_INSTR : 0;
473 // Set the "rear" option for input 0 if selected
474 data[1] |= (sw_settings->input_opt[0] & FF_SWPARAM_FF800_INPUT_OPT_REAR) ? CR1_FF800_INPUT1_REAR : 0;
476 // The input 0 "front" option is activated using one of two bits
477 // depending on whether the filter (aka "speaker emulation") setting is
478 // active.
479 if (sw_settings->input_opt[0] & FF_SWPARAM_FF800_INPUT_OPT_FRONT) {
480 data[1] |= (sw_settings->filter) ? CR1_FF800_INPUT1_FRONT_WITH_FILTER : CR1_FF800_INPUT1_FRONT;
483 data[2] |= (sw_settings->spdif_output_emphasis==FF_SWPARAM_SPDIF_OUTPUT_EMPHASIS_ON) ? CR2_SPDIF_OUT_EMP : 0;
484 data[2] |= (sw_settings->spdif_output_pro==FF_SWPARAM_SPDIF_OUTPUT_PRO_ON) ? CR2_SPDIF_OUT_PRO : 0;
485 data[2] |= (sw_settings->spdif_output_nonaudio==FF_SWPARAM_SPDIF_OUTPUT_NONAUDIO_ON) ? CR2_SPDIF_OUT_NONAUDIO : 0;
486 data[2] |= (sw_settings->spdif_output_mode==FF_SWPARAM_SPDIF_OUTPUT_OPTICAL) ? CR2_SPDIF_OUT_ADAT2 : 0;
487 data[2] |= (sw_settings->clock_mode==FF_SWPARAM_CLOCK_MODE_AUTOSYNC) ? CR2_CLOCKMODE_AUTOSYNC : CR2_CLOCKMODE_MASTER;
488 data[2] |= (sw_settings->spdif_input_mode==FF_SWPARAM_SPDIF_INPUT_COAX) ? CR2_SPDIF_IN_COAX : CR2_SPDIF_IN_ADAT2;
489 data[2] |= (sw_settings->word_clock_single_speed=FF_SWPARAM_WORD_CLOCK_1x) ? CR2_WORD_CLOCK_1x : 0;
491 /* TMS / TCO toggle bits in CR2 are not set by other drivers */
493 /* Drive / fuzz in FF800. In FF400, the CR0 bit used by "Drive" controls
494 * the channel 3 "instrument" option.
496 if (m_rme_model == RME_MODEL_FIREFACE800) {
497 if (sw_settings->fuzz)
498 data[0] |= CR0_FF800_DRIVE_FPGA; // FPGA LED control
499 else
500 data[1] |= CR1_INSTR_DRIVE; // CPLD
501 } else
502 if (m_rme_model == RME_MODEL_FIREFACE400) {
503 data[0] |= (sw_settings->ff400_instr_input[0]) ? CR0_FF400_CH3_INSTR : 0;
506 /* Drop-and-stop is hardwired on in other drivers */
507 data[2] |= CR2_DROP_AND_STOP;
509 if (m_rme_model == RME_MODEL_FIREFACE400) {
510 data[2] |= CR2_FF400_BIT;
513 switch (sw_settings->sync_ref) {
514 case FF_SWPARAM_SYNCREF_WORDCLOCK:
515 data[2] |= CR2_SYNC_WORDCLOCK;
516 break;
517 case FF_SWPARAM_SYNCREF_ADAT1:
518 data[2] |= CR2_SYNC_ADAT1;
519 break;
520 case FF_SWPARAM_SYNCREF_ADAT2:
521 data[2] |= CR2_SYNC_ADAT2;
522 break;
523 case FF_SWPARAM_SYNCREF_SPDIF:
524 data[2] |= CR2_SYNC_SPDIF;
525 break;
526 case FF_SWPARAM_SYNCREC_TCO:
527 data[2] |= CR2_SYNC_TCO;
528 break;
531 // This is hardwired in other drivers
532 data[2] |= (CR2_FREQ0 + CR2_FREQ1 + CR2_DSPEED + CR2_QSSPEED);
534 // The FF800 limiter can only be disabled if the front panel instrument
535 // input is in use, so it only makes sense that it is disabled when that
536 // input is in use.
537 data[2] |= (sw_settings->limiter==0 &&
538 (sw_settings->input_opt[0]==FF_SWPARAM_FF800_INPUT_OPT_FRONT)) ?
539 CR2_DISABLE_LIMITER : 0;
541 //This is just for testing - it's a known consistent configuration
542 //data[0] = 0x00020810; // Phantom off
543 //data[0] = 0x00020811; // Phantom on
544 //data[1] = 0x0000031e;
545 //data[2] = 0xc400101f;
546 debugOutput(DEBUG_LEVEL_VERBOSE, "set hardware registers: 0x%08x 0x%08x 0x%08x\n",
547 data[0], data[1], data[2]);
549 switch (m_rme_model) {
550 case RME_MODEL_FIREFACE800: conf_reg = RME_FF800_CONF_REG; break;
551 case RME_MODEL_FIREFACE400: conf_reg = RME_FF400_CONF_REG; break;
552 default:
553 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
554 return -1;
556 if (writeBlock(conf_reg, data, 3) != 0) {
557 debugOutput(DEBUG_LEVEL_ERROR, "failed to write device settings\n");
558 return -1;
561 return 0;
564 signed int
565 Device::read_tco(quadlet_t *tco_data, signed int size)
567 // Read the TCO registers and return the respective values in *tco_data.
568 // Return value is 0 on success, or -1 if there is no TCO present.
569 // "size" is the size (in quadlets) of the array pointed to by tco_data.
570 // To obtain all TCO data "size" should be at least 4. If the caller
571 // doesn't care about the data returned by the TCO, tco_data can be
572 // NULL.
573 quadlet_t buf[4];
574 signed int i;
576 // Only the Fireface 800 can have the TCO fitted
577 if (m_rme_model != RME_MODEL_FIREFACE800)
578 return -1;
580 if (readBlock(RME_FF_TCO_READ_REG, buf, 4) != 0)
581 return -1;
583 if (tco_data != NULL) {
584 for (i=0; i<(size<4)?size:4; i++)
585 tco_data[i] = buf[i];
588 if ( (buf[0] & 0x80808080) == 0x80808080 &&
589 (buf[1] & 0x80808080) == 0x80808080 &&
590 (buf[2] & 0x80808080) == 0x80808080 &&
591 (buf[3] & 0x8000FFFF) == 0x80008000) {
592 // A TCO is present
593 return 0;
596 return -1;
599 signed int
600 Device::write_tco(quadlet_t *tco_data, signed int size)
602 // Writes data to the TCO. No check is made as to whether a TCO is
603 // present in the current device. Return value is 0 on success or -1 on
604 // error. "size" is the size (in quadlets) of the data pointed to by
605 // "tco_data". The first 4 quadlets of tco_data are significant; all
606 // others are ignored. If fewer than 4 quadlets are supplied (as
607 // indicated by the "size" parameter, -1 will be returned.
608 if (size < 4)
609 return -1;
611 // Don't bother trying to write if the device is not a FF800 since the
612 // TCO can only be fitted to a FF800.
613 if (m_rme_model != RME_MODEL_FIREFACE800)
614 return -1;
616 if (writeBlock(RME_FF_TCO_WRITE_REG, tco_data, 4) != 0)
617 return -1;
619 return 0;
622 signed int
623 Device::hardware_is_streaming(void)
625 // Return 1 if the hardware is streaming, 0 if not.
626 return dev_config->is_streaming;
629 signed int
630 Device::read_tco_state(FF_TCO_state_t *tco_state)
632 // Reads the current TCO state into the supplied state structure
634 quadlet_t tc[4];
635 unsigned int PLL_phase;
637 if (read_tco(tc, 4) != 0)
638 return -1;
640 // The timecode is stored in BCD (binary coded decimal) in register 0.
641 tco_state->frames = (tc[0] & 0xf) + ((tc[0] & 0x30) >> 4)*10;
642 tco_state->seconds = ((tc[0] & 0xf00) >> 8) + ((tc[0] & 0x7000) >> 12)*10;
643 tco_state->minutes = ((tc[0] & 0xf0000) >> 16) + ((tc[0] & 0x700000) >> 20)*10;
644 tco_state->hours = ((tc[0] & 0xf000000) >> 24) + ((tc[0] & 0x30000000) >> 28)*10;
646 tco_state->locked = (tc[1] & FF_TCO1_TCO_lock) != 0;
647 tco_state->ltc_valid = (tc[1] & FF_TCO1_LTC_INPUT_VALID) != 0;
649 switch (tc[1] & FF_TCO1_LTC_FORMAT_MASK) {
650 case FF_TC01_LTC_FORMAT_24fps:
651 tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_24fps; break;
652 case FF_TCO1_LTC_FORMAT_25fps:
653 tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_25fps; break;
654 case FF_TC01_LTC_FORMAT_29_97fps:
655 tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_29_97fps; break;
656 case FF_TCO1_LTC_FORMAT_30fps:
657 tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_30fps; break;
660 tco_state->drop_frame = (tc[1] & FF_TCO1_SET_DROPFRAME) != 0;
662 switch (tc[1] & FF_TCO1_VIDEO_INPUT_MASK) {
663 case FF_TCO1_VIDEO_INPUT_NTSC:
664 tco_state->video_input = FF_TCOSTATE_VIDEO_NTSC; break;
665 case FF_TCO1_VIDEO_INPUT_PAL:
666 tco_state->video_input = FF_TCOSTATE_VIDEO_PAL; break;
667 default:
668 tco_state->video_input = FF_TCOSTATE_VIDEO_NONE;
671 if ((tc[1] & FF_TCO1_WORD_CLOCK_INPUT_VALID) == 0) {
672 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_NONE;
673 } else {
674 switch (tc[1] & FF_TCO1_WORD_CLOCK_INPUT_MASK) {
675 case FF_TCO1_WORD_CLOCK_INPUT_1x:
676 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_1x; break;
677 case FF_TCO1_WORD_CLOCK_INPUT_2x:
678 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_2x; break;
679 case FF_TCO1_WORD_CLOCK_INPUT_4x:
680 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_4x; break;
684 PLL_phase = (tc[2] & 0x7f) + ((tc[2] & 0x7f00) >> 1);
685 tco_state->sample_rate = (25000000.0 * 16.0)/PLL_phase;
687 return 0;
690 signed int
691 Device::write_tco_settings(FF_TCO_settings_t *tco_settings)
693 // Writes the supplied application-level settings to the device's TCO
694 // (Time Code Option). Don't bother doing anything if the device doesn't
695 // have a TCO fitted. Returns 0 on success, -1 on error.
697 quadlet_t tc[4] = {0, 0, 0, 0};
699 if (!dev_config->tco_present) {
700 return -1;
703 if (tco_settings->MTC)
704 tc[0] |= FF_TCO0_MTC;
706 switch (tco_settings->input) {
707 case FF_TCOPARAM_INPUT_LTC:
708 tc[2] |= FF_TCO2_INPUT_LTC; break;
709 case FF_TCOPARAM_INPUT_VIDEO:
710 tc[2] |= FF_TCO2_INPUT_VIDEO; break;
711 case FF_TCOPARAM_INPUT_WCK:
712 tc[2] |= FF_TCO2_INPUT_WORD_CLOCK; break;
715 switch (tco_settings->frame_rate) {
716 case FF_TCOPARAM_FRAMERATE_24fps:
717 tc[1] |= FF_TC01_LTC_FORMAT_24fps; break;
718 case FF_TCOPARAM_FRAMERATE_25fps:
719 tc[1] |= FF_TCO1_LTC_FORMAT_25fps; break;
720 case FF_TCOPARAM_FRAMERATE_29_97fps:
721 tc[1] |= FF_TC01_LTC_FORMAT_29_97fps; break;
722 case FF_TCOPARAM_FRAMERATE_29_97dfps:
723 tc[1] |= FF_TCO1_LTC_FORMAT_29_97dpfs; break;
724 case FF_TCOPARAM_FRAMERATE_30fps:
725 tc[1] |= FF_TCO1_LTC_FORMAT_30fps; break;
726 case FF_TCOPARAM_FRAMERATE_30dfps:
727 tc[1] |= FF_TCO1_LTC_FORMAT_30dfps; break;
730 switch (tco_settings->word_clock) {
731 case FF_TCOPARAM_WORD_CLOCK_CONV_1_1:
732 tc[2] |= FF_TCO2_WORD_CLOCK_CONV_1_1; break;
733 case FF_TCOPARAM_WORD_CLOCK_CONV_44_48:
734 tc[2] |= FF_TCO2_WORD_CLOCK_CONV_44_48; break;
735 case FF_TCOPARAM_WORD_CLOCK_CONV_48_44:
736 tc[2] |= FF_TCO2_WORD_CLOCK_CONV_48_44; break;
739 switch (tco_settings->sample_rate) {
740 case FF_TCOPARAM_SRATE_44_1:
741 tc[2] |= FF_TCO2_SRATE_44_1; break;
742 case FF_TCOPARAM_SRATE_48:
743 tc[2] |= FF_TCO2_SRATE_48; break;
744 case FF_TCOPARAM_SRATE_FROM_APP:
745 tc[2] |= FF_TCO2_SRATE_FROM_APP; break;
748 switch (tco_settings->pull) {
749 case FF_TCPPARAM_PULL_NONE:
750 tc[2] |= FF_TCO2_PULL_0; break;
751 case FF_TCOPARAM_PULL_UP_01:
752 tc[2] |= FF_TCO2_PULL_UP_01; break;
753 case FF_TCOPARAM_PULL_DOWN_01:
754 tc[2] |= FF_TCO2_PULL_DOWN_01; break;
755 case FF_TCOPARAM_PULL_UP_40:
756 tc[2] |= FF_TCO2_PULL_UP_40; break;
757 case FF_TCOPARAM_PULL_DOWN_40:
758 tc[2] |= FF_TCO2_PULL_DOWN_40; break;
761 if (tco_settings->termination == FF_TCOPARAM_TERMINATION_ON)
762 tc[2] |= FF_TCO2_SET_TERMINATION;
764 return write_tco(tc, 4);
767 signed int
768 Device::set_hardware_dds_freq(signed int freq)
770 // Set the device's DDS to the given frequency (which in turn determines
771 // the sampling frequency). Returns 0 on success, -1 on error.
773 unsigned int ret = 0;
775 if (freq < MIN_SPEED || freq > MAX_SPEED)
776 return -1;
778 switch (m_rme_model) {
779 case RME_MODEL_FIREFACE400:
780 ret = writeRegister(RME_FF400_STREAM_SRATE, freq); break;
781 case RME_MODEL_FIREFACE800:
782 ret = writeRegister(RME_FF800_STREAM_SRATE, freq); break;
783 default:
784 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
785 ret = -1;
787 if (ret == 0)
788 dev_config->hardware_freq = freq;
789 else
790 debugOutput(DEBUG_LEVEL_ERROR, "failed to write DDS register\n");
792 return ret;
795 signed int
796 Device::hardware_init_streaming(unsigned int sample_rate,
797 unsigned int tx_channel)
799 // tx_channel is the ISO channel the PC will transmit on.
800 quadlet_t buf[5];
801 fb_nodeaddr_t addr;
802 unsigned int size;
803 signed int ret;
805 debugOutput(DEBUG_LEVEL_VERBOSE, "*** stream init: %d, %d, %d\n",
806 sample_rate, num_channels, tx_channel);
808 buf[0] = sample_rate;
809 buf[1] = (num_channels << 11) + tx_channel;
810 buf[2] = num_channels;
811 buf[3] = 0;
812 buf[4] = 0;
813 if (speed800) {
814 buf[2] |= RME_FF800_STREAMING_SPEED_800;
817 if (m_rme_model == RME_MODEL_FIREFACE400) {
818 addr = RME_FF400_STREAM_INIT_REG;
819 size = RME_FF400_STREAM_INIT_SIZE;
820 } else
821 if (m_rme_model == RME_MODEL_FIREFACE800) {
822 addr = RME_FF800_STREAM_INIT_REG;
823 size = RME_FF800_STREAM_INIT_SIZE;
824 } else {
825 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
826 return -1;
829 ret = writeBlock(addr, buf, size);
830 if (ret != 0)
831 debugOutput(DEBUG_LEVEL_ERROR, "failed to write streaming parameters\n");
832 return ret;
835 signed int
836 Device::hardware_start_streaming(unsigned int listen_channel)
838 signed int ret = 0;
839 // Listen_channel is the ISO channel the PC will listen on for data sent
840 // by the Fireface.
841 fb_nodeaddr_t addr;
842 quadlet_t data = num_channels;
844 config_lock();
845 if (not(hardware_is_streaming())) {
846 debugOutput(DEBUG_LEVEL_VERBOSE,"*** starting: listen=%d, num_ch=%d\n", listen_channel, num_channels);
847 if (m_rme_model == RME_MODEL_FIREFACE400) {
848 addr = RME_FF400_STREAM_START_REG;
849 data |= (listen_channel << 5);
850 } else
851 if (m_rme_model == RME_MODEL_FIREFACE800) {
852 addr = RME_FF800_STREAM_START_REG;
853 if (speed800)
854 data |= RME_FF800_STREAMING_SPEED_800; // Flag 800 Mbps speed
855 } else {
856 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
857 return -1;
860 debugOutput(DEBUG_LEVEL_VERBOSE, "start 0x%016"PRIx64" data: %08x\n", addr, data);
861 ret = writeRegister(addr, data);
862 debugOutput(DEBUG_LEVEL_VERBOSE, " ret=%d\n", ret);
863 if (ret == 0) {
864 dev_config->is_streaming = 1;
865 } else
866 debugOutput(DEBUG_LEVEL_ERROR, "failed to write for streaming start\n");
868 set_hardware_channel_mute(-1, 0);
870 } else
871 ret = 0;
872 config_unlock();
874 return ret;
877 signed int
878 Device::hardware_stop_streaming(void)
880 fb_nodeaddr_t addr;
881 quadlet_t buf[4] = {0, 0, 0, 1};
882 unsigned int size, ret = 0;
884 config_lock();
885 if (hardware_is_streaming()) {
886 if (m_rme_model == RME_MODEL_FIREFACE400) {
887 addr = RME_FF400_STREAM_END_REG;
888 size = RME_FF400_STREAM_END_SIZE;
889 } else
890 if (m_rme_model == RME_MODEL_FIREFACE800) {
891 addr = RME_FF800_STREAM_END_REG;
892 size = RME_FF800_STREAM_END_SIZE;
893 } else {
894 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
895 return -1;
898 ret = writeBlock(addr, buf, size);
899 if (ret == 0) {
900 dev_config->is_streaming = 0;
901 } else
902 debugOutput(DEBUG_LEVEL_ERROR, "failed to write for streaming stop\n");
904 set_hardware_channel_mute(-1, 1);
906 } else
907 ret = 0;
908 config_unlock();
910 return ret;
913 signed int
914 Device::set_hardware_ampgain(unsigned int index, signed int val) {
915 // "index" indicates the hardware amplifier gain to set. Values of 0-3
916 // correspond to input amplifier gains. Values from 4 on relate to output
917 // volume.
919 // "val" is in dB except for inputs 3/4 where it's in units of 0.5 dB. This
920 // function is responsible for converting to/from the scale used by the
921 // device.
923 // Only the FF400 has the hardware gain register which is controlled by this
924 // function.
925 quadlet_t regval = 0;
926 signed int devval = 0;
927 signed int ret;
928 if (val > 120)
929 val = 120;
930 if (val < -120)
931 val = -120;
932 if (index <= FF400_AMPGAIN_MIC2) {
933 if (val >= 10)
934 devval = val;
935 else
936 devval = 0;
937 } else
938 if (index <= FF400_AMPGAIN_INPUT4) {
939 devval = val;
940 } else {
941 devval = 6 - val;
942 if (devval > 53)
943 devval = 0x3f; // Mute
945 regval |= devval;
946 regval |= (index << 16);
947 ret = writeRegister(RME_FF400_GAIN_REG, regval);
948 if (ret != 0)
949 debugOutput(DEBUG_LEVEL_ERROR, "failed to write amp gains\n");
950 return ret;
953 signed int
954 Device::set_hardware_mixergain(unsigned int ctype, unsigned int src_channel,
955 unsigned int dest_channel, signed int val) {
956 // Set the value of a matrix mixer control. ctype is one of the RME_FF_MM_*
957 // defines:
958 // RME_FF_MM_INPUT: source is a physical input
959 // RME_FF_MM_PLAYBACK: source is playback from PC
960 // RME_FF_MM_OUTPUT: source is the physical output whose gain is to be
961 // changed, destination is ignored
962 // Val is the integer value sent to the device. The amount of gain (in dB)
963 // applied can be calculated using
964 // dB = 20.log10(val/32768)
965 // The maximum value of val is 0x10000, corresponding to +6dB of gain.
966 // The minimum is 0x00000 corresponding to mute.
968 unsigned int n_channels;
969 signed int ram_output_block_size;
970 unsigned int ram_addr;
972 if (m_rme_model == RME_MODEL_FIREFACE400) {
973 n_channels = RME_FF400_MAX_CHANNELS;
974 ram_output_block_size = 0x48;
975 } else
976 if (m_rme_model == RME_MODEL_FIREFACE800) {
977 n_channels = RME_FF800_MAX_CHANNELS;
978 ram_output_block_size = 0x80;
979 } else {
980 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
981 return -1;
984 if (src_channel>n_channels || dest_channel>n_channels)
985 return -1;
986 if (abs(val)>0x10000)
987 return -1;
989 ram_addr = RME_FF_MIXER_RAM;
990 switch (ctype) {
991 case RME_FF_MM_INPUT:
992 case RME_FF_MM_PLAYBACK:
993 ram_addr += (dest_channel*2*ram_output_block_size) + 4*src_channel;
994 if (ctype == RME_FF_MM_PLAYBACK)
995 ram_addr += ram_output_block_size;
996 break;
997 case RME_FF_MM_OUTPUT:
998 if (m_rme_model == RME_MODEL_FIREFACE400)
999 ram_addr += 0x0f80;
1000 else
1001 ram_addr += 0x1f80;
1002 ram_addr += 4*src_channel;
1003 break;
1006 if (writeRegister(ram_addr, val) != 0) {
1007 debugOutput(DEBUG_LEVEL_ERROR, "failed to write mixer gain element\n");
1010 // If setting the output volume and the device is the FF400, keep
1011 // the separate gain register in sync.
1012 if (ctype==RME_FF_MM_OUTPUT && m_rme_model==RME_MODEL_FIREFACE400) {
1013 signed int dB;
1014 if (val < 0)
1015 val = -val;
1016 if (val==0)
1017 dB = -90;
1018 else
1019 dB = roundl(20.0*log10(val/32768.0));
1020 set_hardware_ampgain(FF400_AMPGAIN_OUTPUT1+src_channel, dB);
1023 return 0;
1026 signed int
1027 Device::set_hardware_channel_mute(signed int chan, signed int mute) {
1029 // Mute hardware channels as instructed. This mute probably relates to the
1030 // sampled input channels as delivered to the PC. If "chan" is -1 the
1031 // supplied "mute" status is applied to all channels. This is the only
1032 // supported "chan" value for now. Down the track, if there's a need,
1033 // this could be extended to allow individual channel control.
1034 quadlet_t buf[28];
1035 signed int i;
1036 signed int n_channels;
1038 if (m_rme_model == RME_MODEL_FIREFACE400)
1039 n_channels = RME_FF400_MAX_CHANNELS;
1040 else
1041 if (m_rme_model == RME_MODEL_FIREFACE800)
1042 n_channels = RME_FF800_MAX_CHANNELS;
1043 else {
1044 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
1045 return -1;
1048 i = 0;
1049 if (chan < 0) {
1050 while (i<n_channels && i<28) {
1051 buf[i++] = (mute!=0);
1053 } else {
1054 return 0;
1057 while (i < 28) {
1058 buf[i++] = 0x00000001;
1061 // Write 28 quadlets even for FF400
1062 i = writeBlock(RME_FF_CHANNEL_MUTE_MASK, buf, 28);
1063 if (i != 0)
1064 debugOutput(DEBUG_LEVEL_ERROR, "failed to write channel mute\n");
1065 return i;
1068 signed int
1069 Device::set_hardware_output_rec(signed int rec) {
1070 // Explicitly record (mute!=1) outputs, or not.
1071 // TODO: fill the details in to allow individual outputs to be recorded as
1072 // required.
1073 quadlet_t buf[28];
1074 signed int i;
1076 for (i=0; i<28; i++)
1077 buf[i] = (rec!=0);
1079 // Write 28 quadlets even for FF400
1080 i = writeBlock(RME_FF_OUTPUT_REC_MASK, buf, 28);
1081 if (i != 0)
1082 debugOutput(DEBUG_LEVEL_ERROR, "failed to write output record flags\n");
1083 return i;