rme: add some debug output to get_hardware_state()\n
[ffado.git] / libffado / src / rme / fireface_hw.cpp
blob0a4150af58f0c7984d4ae9f6b7b280089fdb1c67
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 dev_config->dds_freq = settings->sample_rate;
92 set_hardware_params(settings);
96 // If no valid flash settings, configure with a static setup.
97 if (dev_config->settings_valid == 0) {
98 memset(settings, 0, sizeof(*settings));
99 settings->spdif_input_mode = FF_SWPARAM_SPDIF_INPUT_COAX;
100 settings->spdif_output_mode = FF_SWPARAM_SPDIF_OUTPUT_COAX;
101 settings->clock_mode = FF_SWPARAM_CLOCK_MODE_MASTER;
102 settings->sync_ref = FF_SWPARAM_SYNCREF_WORDCLOCK;
103 settings->input_level = FF_SWPARAM_ILEVEL_LOGAIN;
104 settings->output_level = FF_SWPARAM_OLEVEL_HIGAIN;
105 settings->phones_level = FF_SWPARAM_PHONESLEVEL_HIGAIN;
106 settings->limit_bandwidth = FF_SWPARAM_BWLIMIT_SEND_ALL_CHANNELS;
108 // A default sampling rate. An explicit DDS frequency is not enabled
109 // by default.
110 dev_config->software_freq = 44100;
111 dev_config->dds_freq = 0;
112 settings->sample_rate = dev_config->software_freq;
114 // TODO: set input amplifier gains to a value other than 0?
116 // TODO: store and manipulate channel mute/rec flags
118 // The FF800 needs the input source set via the input options.
119 // The device's default has the limiter enabled so we'll follow
120 // that convention.
121 if (m_rme_model == RME_MODEL_FIREFACE800) {
122 settings->input_opt[0] = settings->input_opt[1] =
123 settings->input_opt[2] = FF_SWPARAM_FF800_INPUT_OPT_FRONT;
124 settings->limiter = 1;
127 // Configure the hardware to match the current software status.
128 // This is only done if the settings valid flag is 0; if it is 1 it
129 // indicates that something has already set the device up to match
130 // the software settings so there's no need to do it again.
132 if (set_hardware_params(settings) != 0)
133 ret = -1;
135 if (ret==0) {
136 signed freq = dev_config->software_freq;
137 if (dev_config->dds_freq > 0)
138 freq = dev_config->dds_freq;
139 if (set_hardware_dds_freq(freq) != 0)
140 ret = -1;
143 if (m_rme_model == RME_MODEL_FIREFACE400) {
144 signed int i;
145 for (i=FF400_AMPGAIN_MIC1; i<=FF400_AMPGAIN_INPUT4; i++) {
146 set_hardware_ampgain(i, settings->amp_gains[i]);
150 dev_config->settings_valid = 1;
153 have_mixer_settings = read_device_mixer_settings(settings) == 0;
155 // Matrix mixer settings
156 for (dest=0; dest<n_channels; dest++) {
157 for (src=0; src<n_channels; src++) {
158 if (!have_mixer_settings)
159 settings->input_faders[getMixerGainIndex(src, dest)] = 0;
160 set_hardware_mixergain(RME_FF_MM_INPUT, src, dest, 0);
162 for (src=0; src<n_channels; src++) {
163 if (!have_mixer_settings)
164 settings->playback_faders[getMixerGainIndex(src, dest)] =
165 src==dest?0x8000:0;
166 set_hardware_mixergain(RME_FF_MM_PLAYBACK, src, dest,
167 src==dest?0x8000:0);
170 for (src=0; src<n_channels; src++) {
171 if (!have_mixer_settings)
172 settings->output_faders[src] = 0x8000;
173 set_hardware_mixergain(RME_FF_MM_OUTPUT, src, 0, 0x8000);
176 set_hardware_output_rec(0);
178 if (ret==0 && m_rme_model==RME_MODEL_FIREFACE400) {
179 unsigned int node_id = getConfigRom().getNodeId();
180 unsigned int midi_hi_addr;
181 // For now we'll fix this since that's what's done under other
182 // systems.
183 midi_hi_addr = 0x01;
184 if (writeRegister(RME_FF400_MIDI_HIGH_ADDR, (node_id<<16) | midi_hi_addr) != 0)
185 ret = -1;
188 // Also configure the TCO (Time Code Option) settings for those devices
189 // which have a TCO.
190 if (ret==0 && dev_config->tco_settings_valid==0) {
191 if (dev_config->tco_present) {
192 memset(tco_settings, 0, sizeof(*tco_settings));
193 write_tco_settings(tco_settings);
195 dev_config->tco_settings_valid = 1;
198 config_unlock();
200 return ret;
203 signed int
204 Device::get_hardware_status(unsigned int *stat0, unsigned int *stat1)
206 unsigned int buf[2];
207 if (readBlock(RME_FF_STATUS_REG0, buf, 2) != 0)
208 return -1;
209 *stat0 = buf[0];
210 *stat1 = buf[1];
211 return 0;
214 signed int
215 Device::get_hardware_streaming_status(unsigned int *stat, unsigned int n)
217 // Get the hardware status as it applies to the streaming system. This
218 // involves a request of 4 quadlets from the status register. It
219 // appears that the first register's definition is slightly different in
220 // this situation compared to when only 2 quadlets are requested as is
221 // done in get_hardware_status().
223 // "n" is the size of the passed-in stat array. It must be >= 4.
224 if (n < 4)
225 return -1;
226 if (readBlock(RME_FF_STATUS_REG0, stat, 4) != 0)
227 return -1;
228 return 0;
231 signed int
232 Device::get_hardware_state(FF_state_t *state)
234 // Retrieve the hardware status and deduce the device state. Return
235 // -1 on error, 0 on success. The given state structure will be
236 // cleared by this call.
237 unsigned int stat0, stat1;
238 memset(state, 0, sizeof(*state));
239 if (get_hardware_status(&stat0, &stat1) != 0)
240 return -1;
242 state->is_streaming = dev_config->is_streaming;
244 state->clock_mode = (settings->clock_mode == FF_SWPARAM_CLOCK_MODE_MASTER)?FF_STATE_CLOCKMODE_MASTER:FF_STATE_CLOCKMODE_AUTOSYNC;
246 switch (stat0 & SR0_AUTOSYNC_SRC_MASK) {
247 case SR0_AUTOSYNC_SRC_ADAT1:
248 state->autosync_source = FF_STATE_AUTOSYNC_SRC_ADAT1;
249 break;
250 case SR0_AUTOSYNC_SRC_ADAT2:
251 state->autosync_source = FF_STATE_AUTOSYNC_SRC_ADAT2;
252 break;
253 case SR0_AUTOSYNC_SRC_SPDIF:
254 state->autosync_source = FF_STATE_AUTOSYNC_SRC_SPDIF;
255 break;
256 case SR0_AUTOSYNC_SRC_WCLK:
257 state->autosync_source = FF_STATE_AUTOSYNC_SRC_WCLK;
258 break;
259 case SR0_AUTOSYNC_SRC_TCO:
260 state->autosync_source = FF_STATE_AUTOSYNC_SRC_TCO;
261 break;
262 default: state->autosync_source = FF_STATE_AUTOSYNC_SRC_NOLOCK;
265 switch (stat0 & SR0_AUTOSYNC_FREQ_MASK) {
266 case SR0_AUTOSYNC_FREQ_32k: state->autosync_freq = 32000; break;
267 case SR0_AUTOSYNC_FREQ_44k1: state->autosync_freq = 44100; break;
268 case SR0_AUTOSYNC_FREQ_48k: state->autosync_freq = 48000; break;
269 case SR0_AUTOSYNC_FREQ_64k: state->autosync_freq = 64000; break;
270 case SR0_AUTOSYNC_FREQ_88k2: state->autosync_freq = 88200; break;
271 case SR0_AUTOSYNC_FREQ_96k: state->autosync_freq = 96000; break;
272 case SR0_AUTOSYNC_FREQ_128k: state->autosync_freq = 128000; break;
273 case SR0_AUTOSYNC_FREQ_176k4:state->autosync_freq = 176400; break;
274 case SR0_AUTOSYNC_FREQ_192k: state->autosync_freq = 192000; break;
277 switch (stat0 & SR0_SPDIF_FREQ_MASK) {
278 case SR0_SPDIF_FREQ_32k: state->spdif_freq = 32000; break;
279 case SR0_SPDIF_FREQ_44k1: state->spdif_freq = 41000; break;
280 case SR0_SPDIF_FREQ_48k: state->spdif_freq = 48000; break;
281 case SR0_SPDIF_FREQ_64k: state->spdif_freq = 64000; break;
282 case SR0_SPDIF_FREQ_88k2: state->spdif_freq = 88200; break;
283 case SR0_SPDIF_FREQ_96k: state->spdif_freq = 96000; break;
284 case SR0_SPDIF_FREQ_128k: state->spdif_freq = 128000; break;
285 case SR0_SPDIF_FREQ_176k4:state->spdif_freq = 176400; break;
286 case SR0_SPDIF_FREQ_192k: state->spdif_freq = 192000; break;
289 switch (stat0 & SR0_ADAT1_STATUS_MASK) {
290 case SR0_ADAT1_STATUS_NOLOCK:
291 state->adat1_sync_status = FF_STATE_SYNC_NOLOCK; break;
292 case SR0_ADAT1_STATUS_LOCK:
293 state->adat1_sync_status = FF_STATE_SYNC_LOCKED; break;
294 case SR0_ADAT1_STATUS_SYNC:
295 state->adat1_sync_status = FF_STATE_SYNC_SYNCED; break;
297 switch (stat0 & SR0_ADAT2_STATUS_MASK) {
298 case SR0_ADAT2_STATUS_NOLOCK:
299 state->adat2_sync_status = FF_STATE_SYNC_NOLOCK; break;
300 case SR0_ADAT2_STATUS_LOCK:
301 state->adat2_sync_status = FF_STATE_SYNC_LOCKED; break;
302 case SR0_ADAT2_STATUS_SYNC:
303 state->adat2_sync_status = FF_STATE_SYNC_SYNCED; break;
305 switch (stat0 & SR0_SPDIF_STATUS_MASK) {
306 case SR0_SPDIF_STATUS_NOLOCK:
307 state->spdif_sync_status = FF_STATE_SYNC_NOLOCK; break;
308 case SR0_SPDIF_STATUS_LOCK:
309 state->spdif_sync_status = FF_STATE_SYNC_LOCKED; break;
310 case SR0_SPDIF_STATUS_SYNC:
311 state->spdif_sync_status = FF_STATE_SYNC_SYNCED; break;
313 switch (stat0 & SR0_WCLK_STATUS_MASK) {
314 case SR0_WCLK_STATUS_NOLOCK:
315 state->wclk_sync_status = FF_STATE_SYNC_NOLOCK; break;
316 case SR0_WCLK_STATUS_LOCK:
317 state->wclk_sync_status = FF_STATE_SYNC_LOCKED; break;
318 case SR0_WCLK_STATUS_SYNC:
319 state->wclk_sync_status = FF_STATE_SYNC_SYNCED; break;
321 switch (stat1 & SR1_TCO_STATUS_MASK) {
322 case SR1_TCO_STATUS_NOLOCK:
323 state->tco_sync_status = FF_STATE_SYNC_NOLOCK; break;
324 case SR1_TCO_STATUS_LOCK:
325 state->tco_sync_status = FF_STATE_SYNC_LOCKED; break;
326 case SR1_TCO_STATUS_SYNC:
327 state->tco_sync_status = FF_STATE_SYNC_SYNCED; break;
330 // Report the state reported by the hardware if debug output is active
331 debugOutput(DEBUG_LEVEL_VERBOSE, "State reported by hardware:\n");
332 debugOutput(DEBUG_LEVEL_VERBOSE, " is_streaming: %d\n", state->is_streaming);
333 debugOutput(DEBUG_LEVEL_VERBOSE, " clock_mode: %s\n", state->clock_mode==FF_STATE_CLOCKMODE_MASTER?"master":"autosync/slave");
334 debugOutput(DEBUG_LEVEL_VERBOSE, " autosync source: %d\n", state->autosync_source);
335 debugOutput(DEBUG_LEVEL_VERBOSE, " autosync freq: %d\n", state->autosync_freq);
336 debugOutput(DEBUG_LEVEL_VERBOSE, " spdif freq: %d\n", state->spdif_freq);
337 debugOutput(DEBUG_LEVEL_VERBOSE, " ADAT 1/2 status: %x, %x\n", state->adat1_sync_status, state->adat2_sync_status);
338 debugOutput(DEBUG_LEVEL_VERBOSE, " SDPIF status: %x\n", state->spdif_sync_status);
339 debugOutput(DEBUG_LEVEL_VERBOSE, " Wclk/tco status: %x, %x\n", state->wclk_sync_status, state->tco_sync_status);
341 return 0;
344 signed int
345 Device::set_hardware_params(FF_software_settings_t *use_settings)
347 // Initialises the hardware to the state defined by the supplied
348 // software settings structure (which will usually be the device's
349 // "settings" structure). This has the side effect of extinguishing the
350 // "Host" LED on the FF400 when done for the first time after the
351 // interface has been powered up.
353 // If use_settings is NULL, the device's current settings structure will
354 // be used to source the configuration information.
356 FF_software_settings_t *sw_settings;
357 quadlet_t data[3] = {0, 0, 0};
358 unsigned int conf_reg;
360 if (use_settings == NULL)
361 sw_settings = settings;
362 else
363 sw_settings = use_settings;
365 if (sw_settings->mic_phantom[0])
366 data[0] |= CR0_PHANTOM_MIC0;
367 if (sw_settings->mic_phantom[1])
368 data[0] |= CR0_PHANTOM_MIC1;
369 switch (m_rme_model) {
370 case RME_MODEL_FIREFACE800:
371 if (sw_settings->mic_phantom[2])
372 data[0] |= CR0_FF800_PHANTOM_MIC9;
373 if (sw_settings->mic_phantom[3])
374 data[0] |= CR0_FF800_PHANTOM_MIC10;
375 break;
376 case RME_MODEL_FIREFACE400:
377 if (sw_settings->ff400_input_pad[0])
378 data[0] |= CR0_FF400_CH3_PAD;
379 if (sw_settings->ff400_input_pad[1])
380 data[0] |= CR0_FF400_CH4_PAD;
381 break;
382 default:
383 break;
386 /* Phones level */
387 if (m_rme_model == RME_MODEL_FIREFACE400) {
388 switch (sw_settings->phones_level) {
389 case FF_SWPARAM_PHONESLEVEL_HIGAIN:
390 data[0] |= CRO_PHLEVEL_HIGAIN;
391 break;
392 case FF_SWPARAM_PHONESLEVEL_4dBU:
393 data[0] |= CR0_PHLEVEL_4dBU;
394 break;
395 case FF_SWPARAM_PHONESLEVEL_m10dBV:
396 data[0] |= CRO_PHLEVEL_m10dBV;
397 break;
401 /* Input level */
402 switch (sw_settings->input_level) {
403 case FF_SWPARAM_ILEVEL_LOGAIN: // Low gain
404 data[1] |= CR1_ILEVEL_CPLD_LOGAIN; // CPLD
405 data[0] |= CR0_ILEVEL_FPGA_LOGAIN; // LED control (used on FF800 only)
406 break;
407 case FF_SWPARAM_ILEVEL_4dBU: // +4 dBu
408 data[1] |= CR1_ILEVEL_CPLD_4dBU;
409 data[0] |= CR0_ILEVEL_FPGA_4dBU;
410 break;
411 case FF_SWPARAM_ILEVEL_m10dBV: // -10 dBV
412 data[1] |= CR1_ILEVEL_CPLD_m10dBV;
413 data[0] |= CR0_ILEVEL_FPGA_m10dBV;
414 break;
417 /* Output level */
418 switch (sw_settings->output_level) {
419 case FF_SWPARAM_OLEVEL_HIGAIN: // High gain
420 data[1] |= CR1_OLEVEL_CPLD_HIGAIN; // CPLD
421 data[0] |= CR0_OLEVEL_FPGA_HIGAIN; // LED control (used on FF800 only)
422 break;
423 case FF_SWPARAM_OLEVEL_4dBU: // +4 dBu
424 data[1] |= CR1_OLEVEL_CPLD_4dBU;
425 data[0] |= CR0_OLEVEL_FPGA_4dBU;
426 break;
427 case FF_SWPARAM_OLEVEL_m10dBV: // -10 dBV
428 data[1] |= CR1_OLEVEL_CPLD_m10dBV;
429 data[0] |= CR0_OLEVEL_FPGA_m10dBV;
430 break;
433 /* Set input options. The meaning of the options differs between
434 * devices, so we use the generic identifiers here.
436 data[1] |= (sw_settings->input_opt[1] & FF_SWPARAM_INPUT_OPT_A) ? CR1_INPUT_OPT1_A : 0;
437 data[1] |= (sw_settings->input_opt[1] & FF_SWPARAM_INPUT_OPT_B) ? CR1_INPUT_OPT1_B : 0;
438 data[1] |= (sw_settings->input_opt[2] & FF_SWPARAM_INPUT_OPT_A) ? CR1_INPUT_OPT2_A : 0;
439 data[1] |= (sw_settings->input_opt[2] & FF_SWPARAM_INPUT_OPT_B) ? CR1_INPUT_OPT2_B : 0;
441 // Drive the speaker emulation / filter LED via FPGA in FF800. In FF400
442 // the same bit controls the channel 4 "instrument" option.
443 if (m_rme_model == RME_MODEL_FIREFACE800) {
444 data[0] |= (sw_settings->filter) ? CR0_FF800_FILTER_FPGA : 0;
445 } else
446 if (m_rme_model == RME_MODEL_FIREFACE400) {
447 data[0] |= (sw_settings->ff400_instr_input[1]) ? CR0_FF400_CH4_INSTR : 0;
450 // Set the "rear" option for input 0 if selected
451 data[1] |= (sw_settings->input_opt[0] & FF_SWPARAM_FF800_INPUT_OPT_REAR) ? CR1_FF800_INPUT1_REAR : 0;
453 // The input 0 "front" option is activated using one of two bits
454 // depending on whether the filter (aka "speaker emulation") setting is
455 // active.
456 if (sw_settings->input_opt[0] & FF_SWPARAM_FF800_INPUT_OPT_FRONT) {
457 data[1] |= (sw_settings->filter) ? CR1_FF800_INPUT1_FRONT_WITH_FILTER : CR1_FF800_INPUT1_FRONT;
460 data[2] |= (sw_settings->spdif_output_emphasis==FF_SWPARAM_SPDIF_OUTPUT_EMPHASIS_ON) ? CR2_SPDIF_OUT_EMP : 0;
461 data[2] |= (sw_settings->spdif_output_pro==FF_SWPARAM_SPDIF_OUTPUT_PRO_ON) ? CR2_SPDIF_OUT_PRO : 0;
462 data[2] |= (sw_settings->spdif_output_nonaudio==FF_SWPARAM_SPDIF_OUTPUT_NONAUDIO_ON) ? CR2_SPDIF_OUT_NONAUDIO : 0;
463 data[2] |= (sw_settings->spdif_output_mode==FF_SWPARAM_SPDIF_OUTPUT_OPTICAL) ? CR2_SPDIF_OUT_ADAT2 : 0;
464 data[2] |= (sw_settings->clock_mode==FF_SWPARAM_CLOCK_MODE_AUTOSYNC) ? CR2_CLOCKMODE_AUTOSYNC : CR2_CLOCKMODE_MASTER;
465 data[2] |= (sw_settings->spdif_input_mode==FF_SWPARAM_SPDIF_INPUT_COAX) ? CR2_SPDIF_IN_COAX : CR2_SPDIF_IN_ADAT2;
466 data[2] |= (sw_settings->word_clock_single_speed=FF_SWPARAM_WORD_CLOCK_1x) ? CR2_WORD_CLOCK_1x : 0;
468 /* TMS / TCO toggle bits in CR2 are not set by other drivers */
470 /* Drive / fuzz in FF800. In FF400, the CR0 bit used by "Drive" controls
471 * the channel 3 "instrument" option.
473 if (m_rme_model == RME_MODEL_FIREFACE800) {
474 if (sw_settings->fuzz)
475 data[0] |= CR0_FF800_DRIVE_FPGA; // FPGA LED control
476 else
477 data[1] |= CR1_INSTR_DRIVE; // CPLD
478 } else
479 if (m_rme_model == RME_MODEL_FIREFACE400) {
480 data[0] |= (sw_settings->ff400_instr_input[0]) ? CR0_FF400_CH3_INSTR : 0;
483 /* Drop-and-stop is hardwired on in other drivers */
484 data[2] |= CR2_DROP_AND_STOP;
486 if (m_rme_model == RME_MODEL_FIREFACE400) {
487 data[2] |= CR2_FF400_BIT;
490 switch (sw_settings->sync_ref) {
491 case FF_SWPARAM_SYNCREF_WORDCLOCK:
492 data[2] |= CR2_SYNC_WORDCLOCK;
493 break;
494 case FF_SWPARAM_SYNCREF_ADAT1:
495 data[2] |= CR2_SYNC_ADAT1;
496 break;
497 case FF_SWPARAM_SYNCREF_ADAT2:
498 data[2] |= CR2_SYNC_ADAT2;
499 break;
500 case FF_SWPARAM_SYNCREF_SPDIF:
501 data[2] |= CR2_SYNC_SPDIF;
502 break;
503 case FF_SWPARAM_SYNCREC_TCO:
504 data[2] |= CR2_SYNC_TCO;
505 break;
508 // This is hardwired in other drivers
509 data[2] |= (CR2_FREQ0 + CR2_FREQ1 + CR2_DSPEED + CR2_QSSPEED);
511 // The FF800 limiter can only be disabled if the front panel instrument
512 // input is in use, so it only makes sense that it is disabled when that
513 // input is in use.
514 data[2] |= (sw_settings->limiter==0 &&
515 (sw_settings->input_opt[0]==FF_SWPARAM_FF800_INPUT_OPT_FRONT)) ?
516 CR2_DISABLE_LIMITER : 0;
518 //This is just for testing - it's a known consistent configuration
519 //data[0] = 0x00020810; // Phantom off
520 //data[0] = 0x00020811; // Phantom on
521 //data[1] = 0x0000031e;
522 //data[2] = 0xc400101f;
523 debugOutput(DEBUG_LEVEL_VERBOSE, "set hardware registers: 0x%08x 0x%08x 0x%08x\n",
524 data[0], data[1], data[2]);
526 switch (m_rme_model) {
527 case RME_MODEL_FIREFACE800: conf_reg = RME_FF800_CONF_REG; break;
528 case RME_MODEL_FIREFACE400: conf_reg = RME_FF400_CONF_REG; break;
529 default:
530 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
531 return -1;
533 if (writeBlock(conf_reg, data, 3) != 0)
534 return -1;
536 return 0;
539 signed int
540 Device::read_tco(quadlet_t *tco_data, signed int size)
542 // Read the TCO registers and return the respective values in *tco_data.
543 // Return value is 0 on success, or -1 if there is no TCO present.
544 // "size" is the size (in quadlets) of the array pointed to by tco_data.
545 // To obtain all TCO data "size" should be at least 4. If the caller
546 // doesn't care about the data returned by the TCO, tco_data can be
547 // NULL.
548 quadlet_t buf[4];
549 signed int i;
551 // Only the Fireface 800 can have the TCO fitted
552 if (m_rme_model != RME_MODEL_FIREFACE800)
553 return -1;
555 if (readBlock(RME_FF_TCO_READ_REG, buf, 4) != 0)
556 return -1;
558 if (tco_data != NULL) {
559 for (i=0; i<(size<4)?size:4; i++)
560 tco_data[i] = buf[i];
563 if ( (buf[0] & 0x80808080) == 0x80808080 &&
564 (buf[1] & 0x80808080) == 0x80808080 &&
565 (buf[2] & 0x80808080) == 0x80808080 &&
566 (buf[3] & 0x8000FFFF) == 0x80008000) {
567 // A TCO is present
568 return 0;
571 return -1;
574 signed int
575 Device::write_tco(quadlet_t *tco_data, signed int size)
577 // Writes data to the TCO. No check is made as to whether a TCO is
578 // present in the current device. Return value is 0 on success or -1 on
579 // error. "size" is the size (in quadlets) of the data pointed to by
580 // "tco_data". The first 4 quadlets of tco_data are significant; all
581 // others are ignored. If fewer than 4 quadlets are supplied (as
582 // indicated by the "size" parameter, -1 will be returned.
583 if (size < 4)
584 return -1;
586 // Don't bother trying to write if the device is not a FF800 since the
587 // TCO can only be fitted to a FF800.
588 if (m_rme_model != RME_MODEL_FIREFACE800)
589 return -1;
591 if (writeBlock(RME_FF_TCO_WRITE_REG, tco_data, 4) != 0)
592 return -1;
594 return 0;
597 signed int
598 Device::hardware_is_streaming(void)
600 // Return 1 if the hardware is streaming, 0 if not.
601 return dev_config->is_streaming;
604 signed int
605 Device::read_tco_state(FF_TCO_state_t *tco_state)
607 // Reads the current TCO state into the supplied state structure
609 quadlet_t tc[4];
610 unsigned int PLL_phase;
612 if (read_tco(tc, 4) != 0)
613 return -1;
615 // The timecode is stored in BCD (binary coded decimal) in register 0.
616 tco_state->frames = (tc[0] & 0xf) + ((tc[0] & 0x30) >> 4)*10;
617 tco_state->seconds = ((tc[0] & 0xf00) >> 8) + ((tc[0] & 0x7000) >> 12)*10;
618 tco_state->minutes = ((tc[0] & 0xf0000) >> 16) + ((tc[0] & 0x700000) >> 20)*10;
619 tco_state->hours = ((tc[0] & 0xf000000) >> 24) + ((tc[0] & 0x30000000) >> 28)*10;
621 tco_state->locked = (tc[1] & FF_TCO1_TCO_lock) != 0;
622 tco_state->ltc_valid = (tc[1] & FF_TCO1_LTC_INPUT_VALID) != 0;
624 switch (tc[1] & FF_TCO1_LTC_FORMAT_MASK) {
625 case FF_TC01_LTC_FORMAT_24fps:
626 tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_24fps; break;
627 case FF_TCO1_LTC_FORMAT_25fps:
628 tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_25fps; break;
629 case FF_TC01_LTC_FORMAT_29_97fps:
630 tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_29_97fps; break;
631 case FF_TCO1_LTC_FORMAT_30fps:
632 tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_30fps; break;
635 tco_state->drop_frame = (tc[1] & FF_TCO1_SET_DROPFRAME) != 0;
637 switch (tc[1] & FF_TCO1_VIDEO_INPUT_MASK) {
638 case FF_TCO1_VIDEO_INPUT_NTSC:
639 tco_state->video_input = FF_TCOSTATE_VIDEO_NTSC; break;
640 case FF_TCO1_VIDEO_INPUT_PAL:
641 tco_state->video_input = FF_TCOSTATE_VIDEO_PAL; break;
642 default:
643 tco_state->video_input = FF_TCOSTATE_VIDEO_NONE;
646 if ((tc[1] & FF_TCO1_WORD_CLOCK_INPUT_VALID) == 0) {
647 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_NONE;
648 } else {
649 switch (tc[1] & FF_TCO1_WORD_CLOCK_INPUT_MASK) {
650 case FF_TCO1_WORD_CLOCK_INPUT_1x:
651 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_1x; break;
652 case FF_TCO1_WORD_CLOCK_INPUT_2x:
653 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_2x; break;
654 case FF_TCO1_WORD_CLOCK_INPUT_4x:
655 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_4x; break;
659 PLL_phase = (tc[2] & 0x7f) + ((tc[2] & 0x7f00) >> 1);
660 tco_state->sample_rate = (25000000.0 * 16.0)/PLL_phase;
662 return 0;
665 signed int
666 Device::write_tco_settings(FF_TCO_settings_t *tco_settings)
668 // Writes the supplied application-level settings to the device's TCO
669 // (Time Code Option). Don't bother doing anything if the device doesn't
670 // have a TCO fitted. Returns 0 on success, -1 on error.
672 quadlet_t tc[4] = {0, 0, 0, 0};
674 if (!dev_config->tco_present) {
675 return -1;
678 if (tco_settings->MTC)
679 tc[0] |= FF_TCO0_MTC;
681 switch (tco_settings->input) {
682 case FF_TCOPARAM_INPUT_LTC:
683 tc[2] |= FF_TCO2_INPUT_LTC; break;
684 case FF_TCOPARAM_INPUT_VIDEO:
685 tc[2] |= FF_TCO2_INPUT_VIDEO; break;
686 case FF_TCOPARAM_INPUT_WCK:
687 tc[2] |= FF_TCO2_INPUT_WORD_CLOCK; break;
690 switch (tco_settings->frame_rate) {
691 case FF_TCOPARAM_FRAMERATE_24fps:
692 tc[1] |= FF_TC01_LTC_FORMAT_24fps; break;
693 case FF_TCOPARAM_FRAMERATE_25fps:
694 tc[1] |= FF_TCO1_LTC_FORMAT_25fps; break;
695 case FF_TCOPARAM_FRAMERATE_29_97fps:
696 tc[1] |= FF_TC01_LTC_FORMAT_29_97fps; break;
697 case FF_TCOPARAM_FRAMERATE_29_97dfps:
698 tc[1] |= FF_TCO1_LTC_FORMAT_29_97dpfs; break;
699 case FF_TCOPARAM_FRAMERATE_30fps:
700 tc[1] |= FF_TCO1_LTC_FORMAT_30fps; break;
701 case FF_TCOPARAM_FRAMERATE_30dfps:
702 tc[1] |= FF_TCO1_LTC_FORMAT_30dfps; break;
705 switch (tco_settings->word_clock) {
706 case FF_TCOPARAM_WORD_CLOCK_CONV_1_1:
707 tc[2] |= FF_TCO2_WORD_CLOCK_CONV_1_1; break;
708 case FF_TCOPARAM_WORD_CLOCK_CONV_44_48:
709 tc[2] |= FF_TCO2_WORD_CLOCK_CONV_44_48; break;
710 case FF_TCOPARAM_WORD_CLOCK_CONV_48_44:
711 tc[2] |= FF_TCO2_WORD_CLOCK_CONV_48_44; break;
714 switch (tco_settings->sample_rate) {
715 case FF_TCOPARAM_SRATE_44_1:
716 tc[2] |= FF_TCO2_SRATE_44_1; break;
717 case FF_TCOPARAM_SRATE_48:
718 tc[2] |= FF_TCO2_SRATE_48; break;
719 case FF_TCOPARAM_SRATE_FROM_APP:
720 tc[2] |= FF_TCO2_SRATE_FROM_APP; break;
723 switch (tco_settings->pull) {
724 case FF_TCPPARAM_PULL_NONE:
725 tc[2] |= FF_TCO2_PULL_0; break;
726 case FF_TCOPARAM_PULL_UP_01:
727 tc[2] |= FF_TCO2_PULL_UP_01; break;
728 case FF_TCOPARAM_PULL_DOWN_01:
729 tc[2] |= FF_TCO2_PULL_DOWN_01; break;
730 case FF_TCOPARAM_PULL_UP_40:
731 tc[2] |= FF_TCO2_PULL_UP_40; break;
732 case FF_TCOPARAM_PULL_DOWN_40:
733 tc[2] |= FF_TCO2_PULL_DOWN_40; break;
736 if (tco_settings->termination == FF_TCOPARAM_TERMINATION_ON)
737 tc[2] |= FF_TCO2_SET_TERMINATION;
739 return write_tco(tc, 4);
742 signed int
743 Device::set_hardware_dds_freq(signed int freq)
745 // Set the device's DDS to the given frequency (which in turn determines
746 // the sampling frequency). Returns 0 on success, -1 on error.
748 unsigned int ret = 0;
750 if (freq < MIN_SPEED || freq > MAX_SPEED)
751 return -1;
753 switch (m_rme_model) {
754 case RME_MODEL_FIREFACE400:
755 ret = writeRegister(RME_FF400_STREAM_SRATE, freq); break;
756 case RME_MODEL_FIREFACE800:
757 ret = writeRegister(RME_FF800_STREAM_SRATE, freq); break;
758 default:
759 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
760 ret = -1;
762 if (ret == 0)
763 dev_config->hardware_freq = freq;
765 return ret;
768 signed int
769 Device::hardware_init_streaming(unsigned int sample_rate,
770 unsigned int tx_channel)
772 // tx_channel is the ISO channel the PC will transmit on.
773 quadlet_t buf[5];
774 fb_nodeaddr_t addr;
775 unsigned int size;
777 debugOutput(DEBUG_LEVEL_VERBOSE, "*** stream init: %d, %d, %d\n",
778 sample_rate, num_channels, tx_channel);
780 buf[0] = sample_rate;
781 buf[1] = (num_channels << 11) + tx_channel;
782 buf[2] = num_channels;
783 buf[3] = 0;
784 buf[4] = 0;
785 if (speed800) {
786 buf[2] |= RME_FF800_STREAMING_SPEED_800;
789 if (m_rme_model == RME_MODEL_FIREFACE400) {
790 addr = RME_FF400_STREAM_INIT_REG;
791 size = RME_FF400_STREAM_INIT_SIZE;
792 } else
793 if (m_rme_model == RME_MODEL_FIREFACE800) {
794 addr = RME_FF800_STREAM_INIT_REG;
795 size = RME_FF800_STREAM_INIT_SIZE;
796 } else {
797 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
798 return -1;
801 return writeBlock(addr, buf, size);
804 signed int
805 Device::hardware_start_streaming(unsigned int listen_channel)
807 signed int ret = 0;
808 // Listen_channel is the ISO channel the PC will listen on for data sent
809 // by the Fireface.
810 fb_nodeaddr_t addr;
811 quadlet_t data = num_channels;
813 config_lock();
814 if (not(hardware_is_streaming())) {
815 debugOutput(DEBUG_LEVEL_VERBOSE,"*** starting: listen=%d, num_ch=%d\n", listen_channel, num_channels);
816 if (m_rme_model == RME_MODEL_FIREFACE400) {
817 addr = RME_FF400_STREAM_START_REG;
818 data |= (listen_channel << 5);
819 } else
820 if (m_rme_model == RME_MODEL_FIREFACE800) {
821 addr = RME_FF800_STREAM_START_REG;
822 if (speed800)
823 data |= RME_FF800_STREAMING_SPEED_800; // Flag 800 Mbps speed
824 } else {
825 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
826 return -1;
829 debugOutput(DEBUG_LEVEL_VERBOSE, "start 0x%016llx data: %08x\n", addr, data);
830 ret = writeRegister(addr, data);
831 debugOutput(DEBUG_LEVEL_VERBOSE, " ret=%d\n", ret);
832 if (ret == 0) {
833 dev_config->is_streaming = 1;
836 set_hardware_channel_mute(-1, 0);
838 } else
839 ret = 0;
840 config_unlock();
842 return ret;
845 signed int
846 Device::hardware_stop_streaming(void)
848 fb_nodeaddr_t addr;
849 quadlet_t buf[4] = {0, 0, 0, 1};
850 unsigned int size, ret = 0;
852 config_lock();
853 if (hardware_is_streaming()) {
854 if (m_rme_model == RME_MODEL_FIREFACE400) {
855 addr = RME_FF400_STREAM_END_REG;
856 size = RME_FF400_STREAM_END_SIZE;
857 } else
858 if (m_rme_model == RME_MODEL_FIREFACE800) {
859 addr = RME_FF800_STREAM_END_REG;
860 size = RME_FF800_STREAM_END_SIZE;
861 } else {
862 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
863 return -1;
866 ret = writeBlock(addr, buf, size);
867 if (ret == 0) {
868 dev_config->is_streaming = 0;
871 set_hardware_channel_mute(-1, 1);
873 } else
874 ret = 0;
875 config_unlock();
877 return ret;
880 signed int
881 Device::set_hardware_ampgain(unsigned int index, signed int val) {
882 // "index" indicates the hardware amplifier gain to set. Values of 0-3
883 // correspond to input amplifier gains. Values from 4 on relate to output
884 // volume.
886 // "val" is in dB except for inputs 3/4 where it's in units of 0.5 dB. This
887 // function is responsible for converting to/from the scale used by the
888 // device.
890 // Only the FF400 has the hardware gain register which is controlled by this
891 // function.
892 quadlet_t regval = 0;
893 signed int devval = 0;
894 if (val > 120)
895 val = 120;
896 if (val < -120)
897 val = -120;
898 if (index <= FF400_AMPGAIN_MIC2) {
899 if (val >= 10)
900 devval = val;
901 else
902 devval = 0;
903 } else
904 if (index <= FF400_AMPGAIN_INPUT4) {
905 devval = val;
906 } else {
907 devval = 6 - val;
908 if (devval > 53)
909 devval = 0x3f; // Mute
911 regval |= devval;
912 regval |= (index << 16);
913 return writeRegister(RME_FF400_GAIN_REG, regval);
916 signed int
917 Device::set_hardware_mixergain(unsigned int ctype, unsigned int src_channel,
918 unsigned int dest_channel, signed int val) {
919 // Set the value of a matrix mixer control. ctype is one of the RME_FF_MM_*
920 // defines:
921 // RME_FF_MM_INPUT: source is a physical input
922 // RME_FF_MM_PLAYBACK: source is playback from PC
923 // RME_FF_MM_OUTPUT: source is the physical output whose gain is to be
924 // changed, destination is ignored
925 // Val is the integer value sent to the device. The amount of gain (in dB)
926 // applied can be calculated using
927 // dB = 20.log10(val/32768)
928 // The maximum value of val is 0x10000, corresponding to +6dB of gain.
929 // The minimum is 0x00000 corresponding to mute.
931 unsigned int n_channels;
932 signed int ram_output_block_size;
933 unsigned int ram_addr;
935 if (m_rme_model == RME_MODEL_FIREFACE400) {
936 n_channels = RME_FF400_MAX_CHANNELS;
937 ram_output_block_size = 0x48;
938 } else
939 if (m_rme_model == RME_MODEL_FIREFACE800) {
940 n_channels = RME_FF800_MAX_CHANNELS;
941 ram_output_block_size = 0x80;
942 } else {
943 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
944 return -1;
947 if (src_channel>n_channels || dest_channel>n_channels)
948 return -1;
949 if (abs(val)>0x10000)
950 return -1;
952 ram_addr = RME_FF_MIXER_RAM;
953 switch (ctype) {
954 case RME_FF_MM_INPUT:
955 case RME_FF_MM_PLAYBACK:
956 ram_addr += (dest_channel*2*ram_output_block_size) + 4*src_channel;
957 if (ctype == RME_FF_MM_PLAYBACK)
958 ram_addr += ram_output_block_size;
959 break;
960 case RME_FF_MM_OUTPUT:
961 if (m_rme_model == RME_MODEL_FIREFACE400)
962 ram_addr += 0x0f80;
963 else
964 ram_addr += 0x1f80;
965 ram_addr += 4*src_channel;
966 break;
969 writeRegister(ram_addr, val);
971 // If setting the output volume and the device is the FF400, keep
972 // the separate gain register in sync.
973 if (ctype==RME_FF_MM_OUTPUT && m_rme_model==RME_MODEL_FIREFACE400) {
974 signed int dB;
975 if (val < 0)
976 val = -val;
977 if (val==0)
978 dB = -90;
979 else
980 dB = roundl(20.0*log10(val/32768.0));
981 set_hardware_ampgain(FF400_AMPGAIN_OUTPUT1+src_channel, dB);
984 return 0;
987 signed int
988 Device::set_hardware_channel_mute(signed int chan, signed int mute) {
990 // Mute hardware channels as instructed. This mute probably relates to the
991 // sampled input channels as delivered to the PC. If "chan" is -1 the
992 // supplied "mute" status is applied to all channels. This is the only
993 // supported "chan" value for now. Down the track, if there's a need,
994 // this could be extended to allow individual channel control.
995 quadlet_t buf[28];
996 signed int i;
997 signed int n_channels;
999 if (m_rme_model == RME_MODEL_FIREFACE400)
1000 n_channels = RME_FF400_MAX_CHANNELS;
1001 else
1002 if (m_rme_model == RME_MODEL_FIREFACE800)
1003 n_channels = RME_FF800_MAX_CHANNELS;
1004 else {
1005 debugOutput(DEBUG_LEVEL_ERROR, "unimplemented model %d\n", m_rme_model);
1006 return -1;
1009 i = 0;
1010 if (chan < 0) {
1011 while (i<n_channels && i<28) {
1012 buf[i++] = (mute!=0);
1014 } else {
1015 return 0;
1018 while (i < 28) {
1019 buf[i++] = 0x00000001;
1022 // Write 28 quadlets even for FF400
1023 return writeBlock(RME_FF_CHANNEL_MUTE_MASK, buf, 28);
1026 signed int
1027 Device::set_hardware_output_rec(signed int rec) {
1028 // Explicitly record (mute!=1) outputs, or not.
1029 // TODO: fill the details in to allow individual outputs to be recorded as
1030 // required.
1031 quadlet_t buf[28];
1032 signed int i;
1034 for (i=0; i<28; i++)
1035 buf[i] = (rec!=0);
1037 // Write 28 quadlets even for FF400
1038 return writeBlock(RME_FF_OUTPUT_REC_MASK, buf, 28);