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

Revision 1622, 24.7 kB (checked in by jwoithe, 14 years ago)

RME: input, output and phones mixer/control elements now control respective device parameters

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