root/trunk/libffado/src/rme/fireface_hw.cpp

Revision 1616, 24.6 kB (checked in by jwoithe, 14 years ago)

RME: minor documentation corrections
RME: the Fireface-400 input gain mixer controls now drive the hardware. Control of the output gains is also included in this infrastructure but is not connected to any mixer controls yet - they will be controlled by the mixer proper once that's implemented.

Line 
1 /*
2  * Copyright (C) 2009 by Jonathan Woithe
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB.
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  */
23
24 /* This file implements miscellaneous lower-level hardware functions for the Fireface */
25
26 #include "rme/rme_avdevice.h"
27 #include "rme/fireface_def.h"
28
29 #include "debugmodule/debugmodule.h"
30
31 namespace Rme {
32
33 unsigned int
34 Device::multiplier_of_freq(unsigned int freq)
35 {
36   if (freq > MIN_QUAD_SPEED)
37     return 4;
38   if (freq > MIN_DOUBLE_SPEED)
39     return 2;
40   return 1;
41 }
42
43 signed int
44 Device::init_hardware(void)
45 {
46     // Initialises the device's settings structure to a known state and then
47     // sets the hardware to reflect this state.
48     //
49     // In time this function may read a cached device setup and initialise
50     // based on that.  It may also read the device configuration from the
51     // device flash and adopt that.  For now (for initial testing purposes)
52     // we'll go with a static state.
53     memset(&settings, 0, sizeof(settings));
54     settings.spdif_input_mode = FF_SWPARAM_SPDIF_INPUT_COAX;
55     settings.spdif_output_mode = FF_SWPARAM_SPDIF_OUTPUT_COAX;
56     settings.clock_mode = FF_SWPARAM_CLOCK_MODE_MASTER;
57     settings.sync_ref = FF_SWPARAM_SYNCREF_WORDCLOCK;
58     settings.input_level = FF_SWPARAM_ILEVEL_LOGAIN;
59     settings.output_level = FF_SWPARAM_OLEVEL_HIGAIN;
60
61     // Set amplifier gains
62     if (m_rme_model == RME_MODEL_FIREFACE400) {
63         signed int i;
64         for (i=0; i<FF400_AMPGAIN_NUM; i++) {
65             set_hardware_ampgain(i, settings.amp_gains[i]);
66         }
67     }
68
69     // A default sampling rate.  An explicit DDS frequency is not enabled
70     // by default.
71     m_software_freq = 44100;
72     m_dds_freq = 0;
73
74     if (set_hardware_params(&settings) != 0)
75         return -1;
76
77     // Also configure the TCO (Time Code Option) settings for those devices
78     // which have a TCO.
79     if (tco_present) {
80         memset(&tco_settings, 0, sizeof(tco_settings));
81         return write_tco_settings(&tco_settings);
82     }
83
84     return 0;
85 }
86
87 signed int
88 Device::get_hardware_status(unsigned int *stat0, unsigned int *stat1)
89 {
90     unsigned int buf[2];
91     if (readBlock(RME_FF_STATUS_REG0, buf, 2) != 0)
92         return -1;
93     *stat0 = buf[0];
94     *stat1 = buf[1];
95     return 0;
96 }
97
98 signed int
99 Device::get_hardware_streaming_status(unsigned int *stat, unsigned int n)
100 {
101     // Get the hardware status as it applies to the streaming system.  This
102     // involves a request of 4 quadlets from the status register.  It
103     // appears that the first register's definition is slightly different in
104     // this situation compared to when only 2 quadlets are requested as is
105     // done in get_hardware_status().
106     //
107     // "n" is the size of the passed-in stat array.  It must be >= 4.
108     if (n < 4)
109         return -1;
110     if (readBlock(RME_FF_STATUS_REG0, stat, 4) != 0)
111         return -1;
112     return 0;
113 }
114
115 signed int
116 Device::get_hardware_state(FF_state_t *state)
117 {
118     // Retrieve the hardware status and deduce the device state.  Return
119     // -1 on error, 0 on success.  The given state structure will be
120     // cleared by this call.
121     unsigned int stat0, stat1;
122     memset(state, 0, sizeof(*state));
123     if (get_hardware_status(&stat0, &stat1) != 0)
124         return -1;
125
126     state->is_streaming = is_streaming;
127
128     state->clock_mode = (settings.clock_mode == FF_SWPARAM_CLOCK_MODE_MASTER)?FF_STATE_CLOCKMODE_MASTER:FF_STATE_CLOCKMODE_AUTOSYNC;
129
130     switch (stat0 & SR0_AUTOSYNC_SRC_MASK) {
131         case SR0_AUTOSYNC_SRC_ADAT1:
132             state->autosync_source = FF_STATE_AUTOSYNC_SRC_ADAT1;
133             break;
134         case SR0_AUTOSYNC_SRC_ADAT2:
135             state->autosync_source = FF_STATE_AUTOSYNC_SRC_ADAT2;
136             break;
137         case SR0_AUTOSYNC_SRC_SPDIF:
138             state->autosync_source = FF_STATE_AUTOSYNC_SRC_SPDIF;
139             break;
140         case SR0_AUTOSYNC_SRC_WCLK:
141             state->autosync_source = FF_STATE_AUTOSYNC_SRC_WCLK;
142             break;
143         case SR0_AUTOSYNC_SRC_TCO:
144             state->autosync_source = FF_STATE_AUTOSYNC_SRC_TCO;
145             break;
146         default: state->autosync_source = FF_STATE_AUTOSYNC_SRC_NOLOCK;
147     }
148
149     switch (stat0 & SR0_AUTOSYNC_FREQ_MASK) {
150         case SR0_AUTOSYNC_FREQ_32k:  state->autosync_freq = 32000; break;
151         case SR0_AUTOSYNC_FREQ_44k1: state->autosync_freq = 44100; break;
152         case SR0_AUTOSYNC_FREQ_48k:  state->autosync_freq = 48000; break;
153         case SR0_AUTOSYNC_FREQ_64k:  state->autosync_freq = 64000; break;
154         case SR0_AUTOSYNC_FREQ_88k2: state->autosync_freq = 88200; break;
155         case SR0_AUTOSYNC_FREQ_96k:  state->autosync_freq = 96000; break;
156         case SR0_AUTOSYNC_FREQ_128k: state->autosync_freq = 128000; break;
157         case SR0_AUTOSYNC_FREQ_176k4:state->autosync_freq = 176400; break;
158         case SR0_AUTOSYNC_FREQ_192k: state->autosync_freq = 192000; break;
159     }
160
161     switch (stat0 & SR0_SPDIF_FREQ_MASK) {
162         case SR0_SPDIF_FREQ_32k:  state->spdif_freq = 32000; break;
163         case SR0_SPDIF_FREQ_44k1: state->spdif_freq = 41000; break;
164         case SR0_SPDIF_FREQ_48k:  state->spdif_freq = 48000; break;
165         case SR0_SPDIF_FREQ_64k:  state->spdif_freq = 64000; break;
166         case SR0_SPDIF_FREQ_88k2: state->spdif_freq = 88200; break;
167         case SR0_SPDIF_FREQ_96k:  state->spdif_freq = 96000; break;
168         case SR0_SPDIF_FREQ_128k: state->spdif_freq = 128000; break;
169         case SR0_SPDIF_FREQ_176k4:state->spdif_freq = 176400; break;
170         case SR0_SPDIF_FREQ_192k: state->spdif_freq = 192000; break;
171     }
172
173     switch (stat0 & SR0_ADAT1_STATUS_MASK) {
174         case SR0_ADAT1_STATUS_NOLOCK:
175             state->adat1_sync_status = FF_STATE_SYNC_NOLOCK; break;
176         case SR0_ADAT1_STATUS_LOCK:
177             state->adat1_sync_status = FF_STATE_SYNC_LOCKED; break;
178         case SR0_ADAT1_STATUS_SYNC:
179             state->adat1_sync_status = FF_STATE_SYNC_SYNCED; break;
180     }
181     switch (stat0 & SR0_ADAT2_STATUS_MASK) {
182         case SR0_ADAT2_STATUS_NOLOCK:
183             state->adat2_sync_status = FF_STATE_SYNC_NOLOCK; break;
184         case SR0_ADAT2_STATUS_LOCK:
185             state->adat2_sync_status = FF_STATE_SYNC_LOCKED; break;
186         case SR0_ADAT2_STATUS_SYNC:
187             state->adat2_sync_status = FF_STATE_SYNC_SYNCED; break;
188     }
189     switch (stat0 & SR0_SPDIF_STATUS_MASK) {
190         case SR0_SPDIF_STATUS_NOLOCK:
191             state->spdif_sync_status = FF_STATE_SYNC_NOLOCK; break;
192         case SR0_SPDIF_STATUS_LOCK:
193             state->spdif_sync_status = FF_STATE_SYNC_LOCKED; break;
194         case SR0_SPDIF_STATUS_SYNC:
195             state->spdif_sync_status = FF_STATE_SYNC_SYNCED; break;
196     }
197     switch (stat0 & SR0_WCLK_STATUS_MASK) {
198         case SR0_WCLK_STATUS_NOLOCK:
199             state->wclk_sync_status = FF_STATE_SYNC_NOLOCK; break;
200         case SR0_WCLK_STATUS_LOCK:
201             state->wclk_sync_status = FF_STATE_SYNC_LOCKED; break;
202         case SR0_WCLK_STATUS_SYNC:
203             state->wclk_sync_status = FF_STATE_SYNC_SYNCED; break;
204     }
205     switch (stat1 & SR1_TCO_STATUS_MASK) {
206        case SR1_TCO_STATUS_NOLOCK:
207            state->tco_sync_status = FF_STATE_SYNC_NOLOCK; break;
208        case SR1_TCO_STATUS_LOCK:
209            state->tco_sync_status = FF_STATE_SYNC_LOCKED; break;
210        case SR1_TCO_STATUS_SYNC:
211            state->tco_sync_status = FF_STATE_SYNC_SYNCED; break;
212     }
213
214     return 0;
215 }
216
217 signed int
218 Device::set_hardware_params(FF_software_settings_t *use_settings)
219 {
220     // Initialises the hardware to the state defined by the supplied
221     // software settings structure (which will usually be the device's
222     // "settings" structure).  This has the side effect of extinguishing the
223     // "Host" LED on the FF400 when done for the first time after the
224     // interface has been powered up.
225     //
226     // If use_settings is NULL, the device's current settings structure will
227     // be used to source the configuration information.
228
229     FF_software_settings_t *sw_settings;
230     quadlet_t data[3] = {0, 0, 0};
231     unsigned int conf_reg;
232
233     if (use_settings == NULL)
234       sw_settings = &settings;
235     else
236       sw_settings = use_settings;
237
238     if (sw_settings->mic_phantom[0])
239       data[0] |= CR0_PHANTOM_MIC0;
240     if (sw_settings->mic_phantom[1])
241       data[0] |= CR0_PHANTOM_MIC1;
242     if (m_rme_model == RME_MODEL_FIREFACE800) {
243         if (sw_settings->mic_phantom[2])
244             data[0] |= CR0_FF800_PHANTOM_MIC9;
245         if (sw_settings->mic_phantom[3])
246             data[0] |= CR0_FF800_PHANTOM_MIC10;
247     } else {
248         if (sw_settings->ff400_input_pad[0])
249             data[0] |= CR0_FF400_CH3_PAD;
250         if (sw_settings->ff400_input_pad[1])
251             data[0] |= CR0_FF400_CH4_PAD;
252     }
253
254     /* Phones level */
255     switch (sw_settings->phones_level) {
256         case FF_SWPARAM_PHONESLEVEL_HIGAIN:
257             data[0] |= CRO_PHLEVEL_HIGAIN;
258             break;
259         case FF_SWPARAM_PHONESLEVEL_4dBU:
260             data[0] |= CR0_PHLEVEL_4dBU;
261             break;
262         case FF_SWPARAM_PHONESLEVEL_m10dBV:
263             data[0] |= CRO_PHLEVEL_m10dBV;
264             break;
265     }
266
267     /* Input level */
268     switch (sw_settings->input_level) {
269         case FF_SWPARAM_ILEVEL_LOGAIN: // Low gain
270             data[1] |= CR1_ILEVEL_CPLD_LOGAIN;    // CPLD
271             data[0] |= CR0_ILEVEL_FPGA_LOGAIN;    // LED control (used on FF800 only)
272             break;
273         case FF_SWPARAM_ILEVEL_4dBU:   // +4 dBu
274             data[1] |= CR1_ILEVEL_CPLD_4dBU;
275             data[0] |= CR0_ILEVEL_FPGA_4dBU;
276             break;
277         case FF_SWPARAM_ILEVEL_m10dBV: // -10 dBV
278             data[1] |= CR1_ILEVEL_CPLD_m10dBV;
279             data[0] |= CR0_ILEVEL_FPGA_m10dBV;
280             break;
281     }
282
283     /* Output level */
284     switch (sw_settings->output_level) {
285         case FF_SWPARAM_OLEVEL_HIGAIN: // High gain
286             data[1] |= CR1_OLEVEL_CPLD_HIGAIN;   // CPLD
287             data[0] |= CR0_OLEVEL_FPGA_HIGAIN;   // LED control (used on FF800 only)
288             break;
289         case FF_SWPARAM_OLEVEL_4dBU:   // +4 dBu
290             data[1] |= CR1_OLEVEL_CPLD_4dBU;
291             data[0] |= CR0_OLEVEL_FPGA_4dBU;
292             break;
293         case FF_SWPARAM_OLEVEL_m10dBV: // -10 dBV
294             data[1] |= CR1_OLEVEL_CPLD_m10dBV;
295             data[0] |= CR0_OLEVEL_FPGA_m10dBV;
296             break;
297     }
298
299     /* Set input options.  The meaning of the options differs between
300      * devices, so we use the generic identifiers here.
301      */
302     data[1] |= (sw_settings->input_opt[1] & FF_SWPARAM_INPUT_OPT_A) ? CR1_INPUT_OPT1_A : 0;
303     data[1] |= (sw_settings->input_opt[1] & FF_SWPARAM_INPUT_OPT_B) ? CR1_INPUT_OPT1_B : 0;
304     data[1] |= (sw_settings->input_opt[2] & FF_SWPARAM_INPUT_OPT_A) ? CR1_INPUT_OPT2_A : 0;
305     data[1] |= (sw_settings->input_opt[2] & FF_SWPARAM_INPUT_OPT_B) ? CR1_INPUT_OPT2_B : 0;
306
307     // Drive the speaker emulation / filter LED via FPGA in FF800.  In FF400
308     // the same bit controls the channel 4 "instrument" option.
309     if (m_rme_model == RME_MODEL_FIREFACE800) {
310         data[0] |= (sw_settings->filter) ? CR0_FF800_FILTER_FPGA : 0;
311     } else {
312         data[0] |= (sw_settings->ff400_instr_input[1]) ? CR0_FF400_CH4_INSTR : 0;
313     }
314
315     // Set the "rear" option for input 0 if selected
316     data[1] |= (sw_settings->input_opt[0] & FF_SWPARAM_FF800_INPUT_OPT_REAR) ? CR1_FF800_INPUT1_REAR : 0;
317
318     // The input 0 "front" option is activated using one of two bits
319     // depending on whether the filter (aka "speaker emulation") setting is
320     // active.
321     if (sw_settings->input_opt[0] & FF_SWPARAM_FF800_INPUT_OPT_FRONT) {
322         data[1] |= (sw_settings->filter) ? CR1_FF800_INPUT1_FRONT_WITH_FILTER : CR1_FF800_INPUT1_FRONT;
323     }
324
325     data[2] |= (sw_settings->spdif_output_emphasis==FF_SWPARAM_SPDIF_OUTPUT_EMPHASIS_ON) ? CR2_SPDIF_OUT_EMP : 0;
326     data[2] |= (sw_settings->spdif_output_pro==FF_SWPARAM_SPDIF_OUTPUT_PRO_ON) ? CR2_SPDIF_OUT_PRO : 0;
327     data[2] |= (sw_settings->spdif_output_nonaudio==FF_SWPARAM_SPDIF_OUTPUT_NONAUDIO_ON) ? CR2_SPDIF_OUT_NONAUDIO : 0;
328     data[2] |= (sw_settings->spdif_output_mode==FF_SWPARAM_SPDIF_OUTPUT_OPTICAL) ? CR2_SPDIF_OUT_ADAT2 : 0;
329     data[2] |= (sw_settings->clock_mode==FF_SWPARAM_CLOCK_MODE_AUTOSYNC) ? CR2_CLOCKMODE_AUTOSYNC : CR2_CLOCKMODE_MASTER;
330     data[2] |= (sw_settings->spdif_input_mode==FF_SWPARAM_SPDIF_INPUT_COAX) ? CR2_SPDIF_IN_COAX : CR2_SPDIF_IN_ADAT2;
331     data[2] |= (sw_settings->word_clock_single_speed=FF_SWPARAM_WORD_CLOCK_1x) ? CR2_WORD_CLOCK_1x : 0;
332
333     /* TMS / TCO toggle bits in CR2 are not set by other drivers */
334
335     /* Drive / fuzz in FF800.  In FF400, the CR0 bit used by "Drive" controls
336      * the channel 3 "instrument" option.
337      */
338     if (m_rme_model == RME_MODEL_FIREFACE800) {
339         if (sw_settings->fuzz)
340             data[0] |= CR0_FF800_DRIVE_FPGA; // FPGA LED control
341         else
342             data[1] |= CR1_INSTR_DRIVE;      // CPLD
343     } else {
344         data[0] |= (sw_settings->ff400_instr_input[0]) ? CR0_FF400_CH3_INSTR : 0;
345     }
346
347     /* Drop-and-stop is hardwired on in other drivers */
348     data[2] |= CR2_DROP_AND_STOP;
349
350     if (m_rme_model == RME_MODEL_FIREFACE400) {
351         data[2] |= CR2_FF400_BIT;
352     }
353
354     switch (sw_settings->sync_ref) {
355         case FF_SWPARAM_SYNCREF_WORDCLOCK:
356             data[2] |= CR2_SYNC_WORDCLOCK;
357             break;
358         case FF_SWPARAM_SYNCREF_ADAT1:
359             data[2] |= CR2_SYNC_ADAT1;
360             break;
361         case FF_SWPARAM_SYNCREF_ADAT2:
362             data[2] |= CR2_SYNC_ADAT2;
363             break;
364         case FF_SWPARAM_SYNCREF_SPDIF:
365             data[2] |= CR2_SYNC_SPDIF;
366             break;
367         case FF_SWPARAM_SYNCREC_TCO:
368             data[2] |= CR2_SYNC_TCO;
369             break;
370     }
371
372     // This is hardwired in other drivers
373     data[2] |= (CR2_FREQ0 + CR2_FREQ1 + CR2_DSPEED + CR2_QSSPEED);
374
375     // The FF800 limiter applies to the front panel instrument input, so it
376     // only makes sense that it is disabled when that input is in use.
377     data[2] |= (sw_settings->limiter_disable &&
378                 (sw_settings->input_opt[0] & FF_SWPARAM_FF800_INPUT_OPT_FRONT)) ?
379                 CR2_DISABLE_LIMITER : 0;
380
381 //This is just for testing - it's a known consistent configuration
382 //data[0] = 0x00020811;      // Phantom off
383 //data[0] = 0x00020811;      // Phantom on
384 //data[1] = 0x0000031e;
385 //data[2] = 0xc400101f;
386     debugOutput(DEBUG_LEVEL_VERBOSE, "set hardware registers: 0x%08x 0x%08x 0x%08x\n",
387       data[0], data[1], data[2]);
388
389     conf_reg = (m_rme_model==RME_MODEL_FIREFACE800)?RME_FF800_CONF_REG:RME_FF400_CONF_REG;
390     if (writeBlock(conf_reg, data, 3) != 0)
391         return -1;
392
393     return -0;
394 }
395
396 signed int
397 Device::read_tco(quadlet_t *tco_data, signed int size)
398 {
399     // Read the TCO registers and return the respective values in *tco_data.
400     // Return value is 0 on success, or -1 if there is no TCO present.
401     // "size" is the size (in quadlets) of the array pointed to by tco_data.
402     // To obtain all TCO data "size" should be at least 4.  If the caller
403     // doesn't care about the data returned by the TCO, tco_data can be
404     // NULL.
405     quadlet_t buf[4];
406     signed int i;
407
408     // The Fireface 400 can't have the TCO fitted
409     if (m_rme_model==RME_MODEL_FIREFACE400)
410         return -1;
411
412     if (readBlock(RME_FF_TCO_READ_REG, buf, 4) != 0)
413         return -1;
414
415     if (tco_data != NULL) {
416         for (i=0; i<(size<4)?size:4; i++)
417             tco_data[i] = buf[i];
418     }
419
420     if ( (buf[0] & 0x80808080) == 0x80808080 &&
421          (buf[1] & 0x80808080) == 0x80808080 &&
422          (buf[2] & 0x80808080) == 0x80808080 &&
423          (buf[3] & 0x8000FFFF) == 0x80008000) {
424         // A TCO is present
425         return 0;
426     }
427
428     return -1;
429 }
430
431 signed int
432 Device::write_tco(quadlet_t *tco_data, signed int size)
433 {
434     // Writes data to the TCO.  No check is made as to whether a TCO is
435     // present in the current device.  Return value is 0 on success or -1 on
436     // error.  "size" is the size (in quadlets) of the data pointed to by
437     // "tco_data".  The first 4 quadlets of tco_data are significant; all
438     // others are ignored.  If fewer than 4 quadlets are supplied (as
439     // indicated by the "size" parameter, -1 will be returned.
440     if (size < 4)
441         return -1;
442
443     // Don't bother trying to write if the device is a FF400 since the TCO
444     // can't be fitted to this device.
445     if (m_rme_model==RME_MODEL_FIREFACE400)
446         return -1;
447
448     if (writeBlock(RME_FF_TCO_WRITE_REG, tco_data, 4) != 0)
449         return -1;
450
451     return 0;
452 }
453
454 signed int
455 Device::hardware_is_streaming(void)
456 {
457     // Return 1 if the hardware is streaming, 0 if not.
458     return is_streaming;
459 }
460
461 signed int
462 Device::read_tco_state(FF_TCO_state_t *tco_state)
463 {
464     // Reads the current TCO state into the supplied state structure
465
466     quadlet_t tc[4];
467     unsigned int PLL_phase;
468
469     if (read_tco(tc, 4) != 0)
470       return -1;
471
472     // The timecode is stored in BCD (binary coded decimal) in register 0.
473     tco_state->frames = (tc[0] & 0xf) + ((tc[0] & 0x30) >> 4)*10;
474     tco_state->seconds = ((tc[0] & 0xf00) >> 8) + ((tc[0] & 0x7000) >> 12)*10;
475     tco_state->minutes = ((tc[0] & 0xf0000) >> 16) + ((tc[0] & 0x700000) >> 20)*10;
476     tco_state->hours = ((tc[0] & 0xf000000) >> 24) + ((tc[0] & 0x30000000) >> 28)*10;
477
478     tco_state->locked = (tc[1] & FF_TCO1_TCO_lock) != 0;
479     tco_state->ltc_valid = (tc[1] & FF_TCO1_LTC_INPUT_VALID) != 0;
480
481     switch (tc[1] & FF_TCO1_LTC_FORMAT_MASK) {
482         case FF_TC01_LTC_FORMAT_24fps:
483           tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_24fps; break;
484         case FF_TCO1_LTC_FORMAT_25fps:
485           tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_25fps; break;
486         case FF_TC01_LTC_FORMAT_29_97fps:
487           tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_29_97fps; break;
488         case FF_TCO1_LTC_FORMAT_30fps:
489           tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_30fps; break;
490     }
491
492     tco_state->drop_frame = (tc[1] & FF_TCO1_SET_DROPFRAME) != 0;
493
494     switch (tc[1] & FF_TCO1_VIDEO_INPUT_MASK) {
495         case FF_TCO1_VIDEO_INPUT_NTSC:
496             tco_state->video_input = FF_TCOSTATE_VIDEO_NTSC; break;
497         case FF_TCO1_VIDEO_INPUT_PAL:
498             tco_state->video_input = FF_TCOSTATE_VIDEO_PAL; break;
499         default:
500             tco_state->video_input = FF_TCOSTATE_VIDEO_NONE;
501     }
502
503     if ((tc[1] & FF_TCO1_WORD_CLOCK_INPUT_VALID) == 0) {
504         tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_NONE;
505     } else {
506         switch (tc[1] & FF_TCO1_WORD_CLOCK_INPUT_MASK) {
507             case FF_TCO1_WORD_CLOCK_INPUT_1x:
508                 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_1x; break;
509             case FF_TCO1_WORD_CLOCK_INPUT_2x:
510                 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_2x; break;
511             case FF_TCO1_WORD_CLOCK_INPUT_4x:
512                 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_4x; break;
513         }
514     }
515
516     PLL_phase = (tc[2] & 0x7f) + ((tc[2] & 0x7f00) >> 1);
517     tco_state->sample_rate = (25000000.0 * 16.0)/PLL_phase;
518
519     return 0;
520 }
521
522 signed int
523 Device::write_tco_settings(FF_TCO_settings_t *tco_settings)
524 {
525     // Writes the supplied application-level settings to the device's TCO
526     // (Time Code Option).  Don't bother doing anything if the device doesn't
527     // have a TCO fitted.  Returns 0 on success, -1 on error.
528
529     quadlet_t tc[4] = {0, 0, 0, 0};
530
531     if (!tco_present) {
532         return -1;
533     }
534
535     if (tco_settings->MTC)
536         tc[0] |= FF_TCO0_MTC;
537
538     switch (tco_settings->input) {
539         case FF_TCOPARAM_INPUT_LTC:
540             tc[2] |= FF_TCO2_INPUT_LTC; break;
541         case FF_TCOPARAM_INPUT_VIDEO:
542             tc[2] |= FF_TCO2_INPUT_VIDEO; break;
543         case FF_TCOPARAM_INPUT_WCK:
544             tc[2] |= FF_TCO2_INPUT_WORD_CLOCK; break;
545     }
546
547     switch (tco_settings->frame_rate) {
548         case FF_TCOPARAM_FRAMERATE_24fps:
549             tc[1] |= FF_TC01_LTC_FORMAT_24fps; break;
550         case FF_TCOPARAM_FRAMERATE_25fps:
551             tc[1] |= FF_TCO1_LTC_FORMAT_25fps; break;
552         case FF_TCOPARAM_FRAMERATE_29_97fps:
553             tc[1] |= FF_TC01_LTC_FORMAT_29_97fps; break;
554         case FF_TCOPARAM_FRAMERATE_29_97dfps:
555             tc[1] |= FF_TCO1_LTC_FORMAT_29_97dpfs; break;
556         case FF_TCOPARAM_FRAMERATE_30fps:
557             tc[1] |= FF_TCO1_LTC_FORMAT_30fps; break;
558         case FF_TCOPARAM_FRAMERATE_30dfps:
559             tc[1] |= FF_TCO1_LTC_FORMAT_30dfps; break;
560     }
561
562     switch (tco_settings->word_clock) {
563         case FF_TCOPARAM_WORD_CLOCK_CONV_1_1:
564             tc[2] |= FF_TCO2_WORD_CLOCK_CONV_1_1; break;
565         case FF_TCOPARAM_WORD_CLOCK_CONV_44_48:
566             tc[2] |= FF_TCO2_WORD_CLOCK_CONV_44_48; break;
567         case FF_TCOPARAM_WORD_CLOCK_CONV_48_44:
568             tc[2] |= FF_TCO2_WORD_CLOCK_CONV_48_44; break;
569     }
570
571     switch (tco_settings->sample_rate) {
572         case FF_TCOPARAM_SRATE_44_1:
573             tc[2] |= FF_TCO2_SRATE_44_1; break;
574         case FF_TCOPARAM_SRATE_48:
575             tc[2] |= FF_TCO2_SRATE_48; break;
576         case FF_TCOPARAM_SRATE_FROM_APP:
577             tc[2] |= FF_TCO2_SRATE_FROM_APP; break;
578     }
579
580     switch (tco_settings->pull) {
581         case FF_TCPPARAM_PULL_NONE:
582             tc[2] |= FF_TCO2_PULL_0; break;
583         case FF_TCOPARAM_PULL_UP_01:
584             tc[2] |= FF_TCO2_PULL_UP_01; break;
585         case FF_TCOPARAM_PULL_DOWN_01:
586             tc[2] |= FF_TCO2_PULL_DOWN_01; break;
587         case FF_TCOPARAM_PULL_UP_40:
588             tc[2] |= FF_TCO2_PULL_UP_40; break;
589         case FF_TCOPARAM_PULL_DOWN_40:
590             tc[2] |= FF_TCO2_PULL_DOWN_40; break;
591     }
592
593     if (tco_settings->termination == FF_TCOPARAM_TERMINATION_ON)
594         tc[2] |= FF_TCO2_SET_TERMINATION;
595
596     return write_tco(tc, 4);
597
598     return 0;
599 }
600
601 signed int
602 Device::set_hardware_dds_freq(signed int freq)
603 {
604     // Set the device's DDS to the given frequency (which in turn determines
605     // the sampling frequency).  Returns 0 on success, -1 on error.
606
607     unsigned int ret = 0;
608
609     if (freq < MIN_SPEED || freq > MAX_SPEED)
610         return -1;
611
612     if (m_rme_model == RME_MODEL_FIREFACE400)
613         ret = writeRegister(RME_FF400_STREAM_SRATE, freq);
614     else
615         ret = writeRegister(RME_FF800_STREAM_SRATE, freq);
616
617     return ret;
618 }
619
620 signed int
621 Device::hardware_init_streaming(unsigned int sample_rate,
622     unsigned int tx_channel)
623 {
624     // tx_channel is the ISO channel the PC will transmit on.
625     quadlet_t buf[4];
626     fb_nodeaddr_t addr;
627     unsigned int size;
628
629     buf[0] = sample_rate;
630     buf[1] = (num_channels << 11) + tx_channel;
631     buf[2] = num_channels;
632     buf[3] = 0;
633     buf[4] = 0;
634     if (speed800) {
635         buf[2] |= RME_FF800_STREAMING_SPEED_800;
636     }
637
638     if (m_rme_model == RME_MODEL_FIREFACE400) {
639         addr = RME_FF400_STREAM_INIT_REG;
640         size = RME_FF400_STREAM_INIT_SIZE;
641     } else {
642         addr = RME_FF800_STREAM_INIT_REG;
643         size = RME_FF800_STREAM_INIT_SIZE;
644     }
645
646     return writeBlock(addr, buf, size);
647 }
648
649 signed int
650 Device::hardware_start_streaming(unsigned int listen_channel)
651 {
652     // Listen_channel is the ISO channel the PC will listen on for data sent
653     // by the Fireface.
654     fb_nodeaddr_t addr;
655     quadlet_t data = num_channels;
656
657     if (m_rme_model == RME_MODEL_FIREFACE400) {
658         addr = RME_FF400_STREAM_START_REG;
659         data |= (listen_channel << 5);
660     } else {
661         addr = RME_FF800_STREAM_START_REG;
662         if (speed800)
663             data |= RME_FF800_STREAMING_SPEED_800; // Flag 800 Mbps speed
664     }
665
666     return writeRegister(addr, data);
667 }
668
669 signed int
670 Device::hardware_stop_streaming(void)
671 {
672     fb_nodeaddr_t addr;
673     quadlet_t buf[4] = {0, 0, 0, 1};
674     unsigned int size;
675
676     if (m_rme_model == RME_MODEL_FIREFACE400) {
677       addr = RME_FF400_STREAM_END_REG;
678       size = RME_FF400_STREAM_END_SIZE;
679     } else {
680       addr = RME_FF800_STREAM_END_REG;
681       size = RME_FF800_STREAM_END_SIZE;
682     }
683
684     return writeBlock(addr, buf, size);
685 }
686
687 signed int
688 Device::set_hardware_ampgain(unsigned int index, signed int val) {
689 // "val" is in dB except for inputs 3/4 where it's in units of 0.5 dB. This
690 // function is responsible for converting to/from the scale used by the
691 // device.
692     quadlet_t regval = 0;
693     signed int devval = 0;
694     if (index <= FF400_AMPGAIN_MIC2) {
695         if (val >= 10)
696             devval = val;
697         else
698             devval = 0;
699     } else
700     if (index <= FF400_AMPGAIN_INPUT4) {
701         devval = val;
702     } else {
703         devval = 6 - val;
704         if (devval > 53)
705             devval = 0x3f;  // Mute
706     }
707     regval |= devval;
708     regval |= (index << 16);
709     return writeRegister(RME_FF400_GAIN_REG, regval);
710 }
711
712 }
Note: See TracBrowser for help on using the browser.