1 /****************************************************************************
3 Copyright Echo Digital Audio Corporation (c) 1998 - 2004
7 This file is part of Echo Digital Audio's generic driver library.
9 Echo Digital Audio's generic driver library is free software;
10 you can redistribute it and/or modify it under the terms of
11 the GNU General Public License as published by the Free Software
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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 *************************************************************************
26 Translation from C++ and adaptation for use in ALSA-Driver
27 were made by Giuliano Pochini <pochini@shiny.it>
29 ****************************************************************************/
33 /* These functions are common for all "3G" cards */
36 static int check_asic_status(struct echoaudio
*chip
)
40 if (wait_handshake(chip
))
43 chip
->comm_page
->ext_box_status
= cpu_to_le32(E3G_ASIC_NOT_LOADED
);
44 chip
->asic_loaded
= false;
45 clear_handshake(chip
);
46 send_vector(chip
, DSP_VC_TEST_ASIC
);
48 if (wait_handshake(chip
)) {
49 chip
->dsp_code
= NULL
;
53 box_status
= le32_to_cpu(chip
->comm_page
->ext_box_status
);
54 dev_dbg(chip
->card
->dev
, "box_status=%x\n", box_status
);
55 if (box_status
== E3G_ASIC_NOT_LOADED
)
58 chip
->asic_loaded
= true;
59 return box_status
& E3G_BOX_TYPE_MASK
;
64 static inline u32
get_frq_reg(struct echoaudio
*chip
)
66 return le32_to_cpu(chip
->comm_page
->e3g_frq_register
);
71 /* Most configuration of 3G cards is accomplished by writing the control
72 register. write_control_reg sends the new control register value to the DSP. */
73 static int write_control_reg(struct echoaudio
*chip
, u32 ctl
, u32 frq
,
76 __le32 ctl_reg
, frq_reg
;
78 if (wait_handshake(chip
))
81 dev_dbg(chip
->card
->dev
,
82 "WriteControlReg: Setting 0x%x, 0x%x\n", ctl
, frq
);
84 ctl_reg
= cpu_to_le32(ctl
);
85 frq_reg
= cpu_to_le32(frq
);
87 if (ctl_reg
!= chip
->comm_page
->control_register
||
88 frq_reg
!= chip
->comm_page
->e3g_frq_register
|| force
) {
89 chip
->comm_page
->e3g_frq_register
= frq_reg
;
90 chip
->comm_page
->control_register
= ctl_reg
;
91 clear_handshake(chip
);
92 return send_vector(chip
, DSP_VC_WRITE_CONTROL_REG
);
95 dev_dbg(chip
->card
->dev
, "WriteControlReg: not written, no change\n");
101 /* Set the digital mode - currently for Gina24, Layla24, Mona, 3G */
102 static int set_digital_mode(struct echoaudio
*chip
, u8 mode
)
107 /* All audio channels must be closed before changing the digital mode */
108 if (snd_BUG_ON(chip
->pipe_alloc_mask
))
111 if (snd_BUG_ON(!(chip
->digital_modes
& (1 << mode
))))
114 previous_mode
= chip
->digital_mode
;
115 err
= dsp_set_digital_mode(chip
, mode
);
117 /* If we successfully changed the digital mode from or to ADAT,
118 * then make sure all output, input and monitor levels are
119 * updated by the DSP comm object. */
120 if (err
>= 0 && previous_mode
!= mode
&&
121 (previous_mode
== DIGITAL_MODE_ADAT
|| mode
== DIGITAL_MODE_ADAT
)) {
122 spin_lock_irq(&chip
->lock
);
123 for (o
= 0; o
< num_busses_out(chip
); o
++)
124 for (i
= 0; i
< num_busses_in(chip
); i
++)
125 set_monitor_gain(chip
, o
, i
,
126 chip
->monitor_gain
[o
][i
]);
128 #ifdef ECHOCARD_HAS_INPUT_GAIN
129 for (i
= 0; i
< num_busses_in(chip
); i
++)
130 set_input_gain(chip
, i
, chip
->input_gain
[i
]);
131 update_input_line_level(chip
);
134 for (o
= 0; o
< num_busses_out(chip
); o
++)
135 set_output_gain(chip
, o
, chip
->output_gain
[o
]);
136 update_output_line_level(chip
);
137 spin_unlock_irq(&chip
->lock
);
145 static u32
set_spdif_bits(struct echoaudio
*chip
, u32 control_reg
, u32 rate
)
147 control_reg
&= E3G_SPDIF_FORMAT_CLEAR_MASK
;
151 control_reg
|= E3G_SPDIF_SAMPLE_RATE0
| E3G_SPDIF_SAMPLE_RATE1
;
154 if (chip
->professional_spdif
)
155 control_reg
|= E3G_SPDIF_SAMPLE_RATE0
;
158 control_reg
|= E3G_SPDIF_SAMPLE_RATE1
;
162 if (chip
->professional_spdif
)
163 control_reg
|= E3G_SPDIF_PRO_MODE
;
165 if (chip
->non_audio_spdif
)
166 control_reg
|= E3G_SPDIF_NOT_AUDIO
;
168 control_reg
|= E3G_SPDIF_24_BIT
| E3G_SPDIF_TWO_CHANNEL
|
169 E3G_SPDIF_COPY_PERMIT
;
176 /* Set the S/PDIF output format */
177 static int set_professional_spdif(struct echoaudio
*chip
, char prof
)
181 control_reg
= le32_to_cpu(chip
->comm_page
->control_register
);
182 chip
->professional_spdif
= prof
;
183 control_reg
= set_spdif_bits(chip
, control_reg
, chip
->sample_rate
);
184 return write_control_reg(chip
, control_reg
, get_frq_reg(chip
), 0);
189 /* detect_input_clocks() returns a bitmask consisting of all the input clocks
190 currently connected to the hardware; this changes as the user connects and
191 disconnects clock inputs. You should use this information to determine which
192 clocks the user is allowed to select. */
193 static u32
detect_input_clocks(const struct echoaudio
*chip
)
195 u32 clocks_from_dsp
, clock_bits
;
197 /* Map the DSP clock detect bits to the generic driver clock
199 clocks_from_dsp
= le32_to_cpu(chip
->comm_page
->status_clocks
);
201 clock_bits
= ECHO_CLOCK_BIT_INTERNAL
;
203 if (clocks_from_dsp
& E3G_CLOCK_DETECT_BIT_WORD
)
204 clock_bits
|= ECHO_CLOCK_BIT_WORD
;
206 switch(chip
->digital_mode
) {
207 case DIGITAL_MODE_SPDIF_RCA
:
208 case DIGITAL_MODE_SPDIF_OPTICAL
:
209 if (clocks_from_dsp
& E3G_CLOCK_DETECT_BIT_SPDIF
)
210 clock_bits
|= ECHO_CLOCK_BIT_SPDIF
;
212 case DIGITAL_MODE_ADAT
:
213 if (clocks_from_dsp
& E3G_CLOCK_DETECT_BIT_ADAT
)
214 clock_bits
|= ECHO_CLOCK_BIT_ADAT
;
223 static int load_asic(struct echoaudio
*chip
)
227 if (chip
->asic_loaded
)
230 /* Give the DSP a few milliseconds to settle down */
233 err
= load_asic_generic(chip
, DSP_FNC_LOAD_3G_ASIC
, FW_3G_ASIC
);
237 chip
->asic_code
= FW_3G_ASIC
;
239 /* Now give the new ASIC some time to set up */
241 /* See if it worked */
242 box_type
= check_asic_status(chip
);
244 /* Set up the control register if the load succeeded -
245 * 48 kHz, internal clock, S/PDIF RCA mode */
247 err
= write_control_reg(chip
, E3G_48KHZ
,
248 E3G_FREQ_REG_DEFAULT
, true);
258 static int set_sample_rate(struct echoaudio
*chip
, u32 rate
)
260 u32 control_reg
, clock
, base_rate
, frq_reg
;
262 /* Only set the clock for internal mode. */
263 if (chip
->input_clock
!= ECHO_CLOCK_INTERNAL
) {
264 dev_warn(chip
->card
->dev
,
265 "Cannot set sample rate - clock not set to CLK_CLOCKININTERNAL\n");
266 /* Save the rate anyhow */
267 chip
->comm_page
->sample_rate
= cpu_to_le32(rate
);
268 chip
->sample_rate
= rate
;
269 set_input_clock(chip
, chip
->input_clock
);
273 if (snd_BUG_ON(rate
>= 50000 &&
274 chip
->digital_mode
== DIGITAL_MODE_ADAT
))
278 control_reg
= le32_to_cpu(chip
->comm_page
->control_register
);
279 control_reg
&= E3G_CLOCK_CLEAR_MASK
;
298 clock
= E3G_CONTINUOUS_CLOCK
;
300 clock
|= E3G_DOUBLE_SPEED_MODE
;
304 control_reg
|= clock
;
305 control_reg
= set_spdif_bits(chip
, control_reg
, rate
);
308 if (base_rate
> 50000)
310 if (base_rate
< 32000)
313 frq_reg
= E3G_MAGIC_NUMBER
/ base_rate
- 2;
314 if (frq_reg
> E3G_FREQ_REG_MAX
)
315 frq_reg
= E3G_FREQ_REG_MAX
;
317 chip
->comm_page
->sample_rate
= cpu_to_le32(rate
); /* ignored by the DSP */
318 chip
->sample_rate
= rate
;
319 dev_dbg(chip
->card
->dev
,
320 "SetSampleRate: %d clock %x\n", rate
, control_reg
);
322 /* Tell the DSP about it - DSP reads both control reg & freq reg */
323 return write_control_reg(chip
, control_reg
, frq_reg
, 0);
328 /* Set the sample clock source to internal, S/PDIF, ADAT */
329 static int set_input_clock(struct echoaudio
*chip
, u16 clock
)
331 u32 control_reg
, clocks_from_dsp
;
334 /* Mask off the clock select bits */
335 control_reg
= le32_to_cpu(chip
->comm_page
->control_register
) &
336 E3G_CLOCK_CLEAR_MASK
;
337 clocks_from_dsp
= le32_to_cpu(chip
->comm_page
->status_clocks
);
340 case ECHO_CLOCK_INTERNAL
:
341 chip
->input_clock
= ECHO_CLOCK_INTERNAL
;
342 return set_sample_rate(chip
, chip
->sample_rate
);
343 case ECHO_CLOCK_SPDIF
:
344 if (chip
->digital_mode
== DIGITAL_MODE_ADAT
)
346 control_reg
|= E3G_SPDIF_CLOCK
;
347 if (clocks_from_dsp
& E3G_CLOCK_DETECT_BIT_SPDIF96
)
348 control_reg
|= E3G_DOUBLE_SPEED_MODE
;
350 control_reg
&= ~E3G_DOUBLE_SPEED_MODE
;
352 case ECHO_CLOCK_ADAT
:
353 if (chip
->digital_mode
!= DIGITAL_MODE_ADAT
)
355 control_reg
|= E3G_ADAT_CLOCK
;
356 control_reg
&= ~E3G_DOUBLE_SPEED_MODE
;
358 case ECHO_CLOCK_WORD
:
359 control_reg
|= E3G_WORD_CLOCK
;
360 if (clocks_from_dsp
& E3G_CLOCK_DETECT_BIT_WORD96
)
361 control_reg
|= E3G_DOUBLE_SPEED_MODE
;
363 control_reg
&= ~E3G_DOUBLE_SPEED_MODE
;
366 dev_err(chip
->card
->dev
,
367 "Input clock 0x%x not supported for Echo3G\n", clock
);
371 chip
->input_clock
= clock
;
372 return write_control_reg(chip
, control_reg
, get_frq_reg(chip
), 1);
377 static int dsp_set_digital_mode(struct echoaudio
*chip
, u8 mode
)
380 int err
, incompatible_clock
;
382 /* Set clock to "internal" if it's not compatible with the new mode */
383 incompatible_clock
= false;
385 case DIGITAL_MODE_SPDIF_OPTICAL
:
386 case DIGITAL_MODE_SPDIF_RCA
:
387 if (chip
->input_clock
== ECHO_CLOCK_ADAT
)
388 incompatible_clock
= true;
390 case DIGITAL_MODE_ADAT
:
391 if (chip
->input_clock
== ECHO_CLOCK_SPDIF
)
392 incompatible_clock
= true;
395 dev_err(chip
->card
->dev
,
396 "Digital mode not supported: %d\n", mode
);
400 spin_lock_irq(&chip
->lock
);
402 if (incompatible_clock
) {
403 chip
->sample_rate
= 48000;
404 set_input_clock(chip
, ECHO_CLOCK_INTERNAL
);
407 /* Clear the current digital mode */
408 control_reg
= le32_to_cpu(chip
->comm_page
->control_register
);
409 control_reg
&= E3G_DIGITAL_MODE_CLEAR_MASK
;
411 /* Tweak the control reg */
413 case DIGITAL_MODE_SPDIF_OPTICAL
:
414 control_reg
|= E3G_SPDIF_OPTICAL_MODE
;
416 case DIGITAL_MODE_SPDIF_RCA
:
417 /* E3G_SPDIF_OPTICAL_MODE bit cleared */
419 case DIGITAL_MODE_ADAT
:
420 control_reg
|= E3G_ADAT_MODE
;
421 control_reg
&= ~E3G_DOUBLE_SPEED_MODE
; /* @@ useless */
425 err
= write_control_reg(chip
, control_reg
, get_frq_reg(chip
), 1);
426 spin_unlock_irq(&chip
->lock
);
429 chip
->digital_mode
= mode
;
431 dev_dbg(chip
->card
->dev
, "set_digital_mode(%d)\n", chip
->digital_mode
);
432 return incompatible_clock
;