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

Revision 1696, 26.9 kB (checked in by jwoithe, 14 years ago)

RME: add some device initialisation details.

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