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 ****************************************************************************/
32 static int write_control_reg(struct echoaudio
*chip
, u32 value
, char force
);
33 static int set_input_clock(struct echoaudio
*chip
, u16 clock
);
34 static int set_professional_spdif(struct echoaudio
*chip
, char prof
);
35 static int set_digital_mode(struct echoaudio
*chip
, u8 mode
);
36 static int load_asic_generic(struct echoaudio
*chip
, u32 cmd
, short asic
);
37 static int check_asic_status(struct echoaudio
*chip
);
40 static int init_hw(struct echoaudio
*chip
, u16 device_id
, u16 subdevice_id
)
44 DE_INIT(("init_hw() - Mona\n"));
45 if (snd_BUG_ON((subdevice_id
& 0xfff0) != MONA
))
48 if ((err
= init_dsp_comm_page(chip
))) {
49 DE_INIT(("init_hw - could not initialize DSP comm page\n"));
53 chip
->device_id
= device_id
;
54 chip
->subdevice_id
= subdevice_id
;
55 chip
->bad_board
= TRUE
;
56 chip
->input_clock_types
=
57 ECHO_CLOCK_BIT_INTERNAL
| ECHO_CLOCK_BIT_SPDIF
|
58 ECHO_CLOCK_BIT_WORD
| ECHO_CLOCK_BIT_ADAT
;
60 ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_RCA
|
61 ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_OPTICAL
|
62 ECHOCAPS_HAS_DIGITAL_MODE_ADAT
;
64 /* Mona comes in both '301 and '361 flavors */
65 if (chip
->device_id
== DEVICE_ID_56361
)
66 chip
->dsp_code_to_load
= FW_MONA_361_DSP
;
68 chip
->dsp_code_to_load
= FW_MONA_301_DSP
;
70 if ((err
= load_firmware(chip
)) < 0)
72 chip
->bad_board
= FALSE
;
74 DE_INIT(("init_hw done\n"));
80 static int set_mixer_defaults(struct echoaudio
*chip
)
82 chip
->digital_mode
= DIGITAL_MODE_SPDIF_RCA
;
83 chip
->professional_spdif
= FALSE
;
84 chip
->digital_in_automute
= TRUE
;
85 return init_line_levels(chip
);
90 static u32
detect_input_clocks(const struct echoaudio
*chip
)
92 u32 clocks_from_dsp
, clock_bits
;
94 /* Map the DSP clock detect bits to the generic driver clock
96 clocks_from_dsp
= le32_to_cpu(chip
->comm_page
->status_clocks
);
98 clock_bits
= ECHO_CLOCK_BIT_INTERNAL
;
100 if (clocks_from_dsp
& GML_CLOCK_DETECT_BIT_SPDIF
)
101 clock_bits
|= ECHO_CLOCK_BIT_SPDIF
;
103 if (clocks_from_dsp
& GML_CLOCK_DETECT_BIT_ADAT
)
104 clock_bits
|= ECHO_CLOCK_BIT_ADAT
;
106 if (clocks_from_dsp
& GML_CLOCK_DETECT_BIT_WORD
)
107 clock_bits
|= ECHO_CLOCK_BIT_WORD
;
114 /* Mona has an ASIC on the PCI card and another ASIC in the external box;
115 both need to be loaded. */
116 static int load_asic(struct echoaudio
*chip
)
122 if (chip
->asic_loaded
)
127 if (chip
->device_id
== DEVICE_ID_56361
)
128 asic
= FW_MONA_361_1_ASIC48
;
130 asic
= FW_MONA_301_1_ASIC48
;
132 err
= load_asic_generic(chip
, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC
, asic
);
136 chip
->asic_code
= asic
;
139 /* Do the external one */
140 err
= load_asic_generic(chip
, DSP_FNC_LOAD_MONA_EXTERNAL_ASIC
,
146 err
= check_asic_status(chip
);
148 /* Set up the control register if the load succeeded -
149 48 kHz, internal clock, S/PDIF RCA mode */
151 control_reg
= GML_CONVERTER_ENABLE
| GML_48KHZ
;
152 err
= write_control_reg(chip
, control_reg
, TRUE
);
160 /* Depending on what digital mode you want, Mona needs different ASICs
161 loaded. This function checks the ASIC needed for the new mode and sees
162 if it matches the one already loaded. */
163 static int switch_asic(struct echoaudio
*chip
, char double_speed
)
168 /* Check the clock detect bits to see if this is
169 a single-speed clock or a double-speed clock; load
170 a new ASIC if necessary. */
171 if (chip
->device_id
== DEVICE_ID_56361
) {
173 asic
= FW_MONA_361_1_ASIC96
;
175 asic
= FW_MONA_361_1_ASIC48
;
178 asic
= FW_MONA_301_1_ASIC96
;
180 asic
= FW_MONA_301_1_ASIC48
;
183 if (asic
!= chip
->asic_code
) {
184 /* Load the desired ASIC */
185 err
= load_asic_generic(chip
, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC
,
189 chip
->asic_code
= asic
;
197 static int set_sample_rate(struct echoaudio
*chip
, u32 rate
)
199 u32 control_reg
, clock
;
203 /* Only set the clock for internal mode. */
204 if (chip
->input_clock
!= ECHO_CLOCK_INTERNAL
) {
205 DE_ACT(("set_sample_rate: Cannot set sample rate - "
206 "clock not set to CLK_CLOCKININTERNAL\n"));
207 /* Save the rate anyhow */
208 chip
->comm_page
->sample_rate
= cpu_to_le32(rate
);
209 chip
->sample_rate
= rate
;
213 /* Now, check to see if the required ASIC is loaded */
215 if (chip
->digital_mode
== DIGITAL_MODE_ADAT
)
217 if (chip
->device_id
== DEVICE_ID_56361
)
218 asic
= FW_MONA_361_1_ASIC96
;
220 asic
= FW_MONA_301_1_ASIC96
;
222 if (chip
->device_id
== DEVICE_ID_56361
)
223 asic
= FW_MONA_361_1_ASIC48
;
225 asic
= FW_MONA_301_1_ASIC48
;
229 if (asic
!= chip
->asic_code
) {
231 /* Load the desired ASIC (load_asic_generic() can sleep) */
232 spin_unlock_irq(&chip
->lock
);
233 err
= load_asic_generic(chip
, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC
,
235 spin_lock_irq(&chip
->lock
);
239 chip
->asic_code
= asic
;
243 /* Compute the new control register value */
245 control_reg
= le32_to_cpu(chip
->comm_page
->control_register
);
246 control_reg
&= GML_CLOCK_CLEAR_MASK
;
247 control_reg
&= GML_SPDIF_RATE_CLEAR_MASK
;
257 clock
= GML_48KHZ
| GML_SPDIF_SAMPLE_RATE1
;
261 /* Professional mode */
262 if (control_reg
& GML_SPDIF_PRO_MODE
)
263 clock
|= GML_SPDIF_SAMPLE_RATE0
;
266 clock
= GML_32KHZ
| GML_SPDIF_SAMPLE_RATE0
|
267 GML_SPDIF_SAMPLE_RATE1
;
282 DE_ACT(("set_sample_rate: %d invalid!\n", rate
));
286 control_reg
|= clock
;
288 chip
->comm_page
->sample_rate
= cpu_to_le32(rate
); /* ignored by the DSP */
289 chip
->sample_rate
= rate
;
290 DE_ACT(("set_sample_rate: %d clock %d\n", rate
, clock
));
292 return write_control_reg(chip
, control_reg
, force_write
);
297 static int set_input_clock(struct echoaudio
*chip
, u16 clock
)
299 u32 control_reg
, clocks_from_dsp
;
302 DE_ACT(("set_input_clock:\n"));
304 /* Prevent two simultaneous calls to switch_asic() */
305 if (atomic_read(&chip
->opencount
))
308 /* Mask off the clock select bits */
309 control_reg
= le32_to_cpu(chip
->comm_page
->control_register
) &
310 GML_CLOCK_CLEAR_MASK
;
311 clocks_from_dsp
= le32_to_cpu(chip
->comm_page
->status_clocks
);
314 case ECHO_CLOCK_INTERNAL
:
315 DE_ACT(("Set Mona clock to INTERNAL\n"));
316 chip
->input_clock
= ECHO_CLOCK_INTERNAL
;
317 return set_sample_rate(chip
, chip
->sample_rate
);
318 case ECHO_CLOCK_SPDIF
:
319 if (chip
->digital_mode
== DIGITAL_MODE_ADAT
)
321 spin_unlock_irq(&chip
->lock
);
322 err
= switch_asic(chip
, clocks_from_dsp
&
323 GML_CLOCK_DETECT_BIT_SPDIF96
);
324 spin_lock_irq(&chip
->lock
);
327 DE_ACT(("Set Mona clock to SPDIF\n"));
328 control_reg
|= GML_SPDIF_CLOCK
;
329 if (clocks_from_dsp
& GML_CLOCK_DETECT_BIT_SPDIF96
)
330 control_reg
|= GML_DOUBLE_SPEED_MODE
;
332 control_reg
&= ~GML_DOUBLE_SPEED_MODE
;
334 case ECHO_CLOCK_WORD
:
335 DE_ACT(("Set Mona clock to WORD\n"));
336 spin_unlock_irq(&chip
->lock
);
337 err
= switch_asic(chip
, clocks_from_dsp
&
338 GML_CLOCK_DETECT_BIT_WORD96
);
339 spin_lock_irq(&chip
->lock
);
342 control_reg
|= GML_WORD_CLOCK
;
343 if (clocks_from_dsp
& GML_CLOCK_DETECT_BIT_WORD96
)
344 control_reg
|= GML_DOUBLE_SPEED_MODE
;
346 control_reg
&= ~GML_DOUBLE_SPEED_MODE
;
348 case ECHO_CLOCK_ADAT
:
349 DE_ACT(("Set Mona clock to ADAT\n"));
350 if (chip
->digital_mode
!= DIGITAL_MODE_ADAT
)
352 control_reg
|= GML_ADAT_CLOCK
;
353 control_reg
&= ~GML_DOUBLE_SPEED_MODE
;
356 DE_ACT(("Input clock 0x%x not supported for Mona\n", clock
));
360 chip
->input_clock
= clock
;
361 return write_control_reg(chip
, control_reg
, TRUE
);
366 static int dsp_set_digital_mode(struct echoaudio
*chip
, u8 mode
)
369 int err
, incompatible_clock
;
371 /* Set clock to "internal" if it's not compatible with the new mode */
372 incompatible_clock
= FALSE
;
374 case DIGITAL_MODE_SPDIF_OPTICAL
:
375 case DIGITAL_MODE_SPDIF_RCA
:
376 if (chip
->input_clock
== ECHO_CLOCK_ADAT
)
377 incompatible_clock
= TRUE
;
379 case DIGITAL_MODE_ADAT
:
380 if (chip
->input_clock
== ECHO_CLOCK_SPDIF
)
381 incompatible_clock
= TRUE
;
384 DE_ACT(("Digital mode not supported: %d\n", mode
));
388 spin_lock_irq(&chip
->lock
);
390 if (incompatible_clock
) { /* Switch to 48KHz, internal */
391 chip
->sample_rate
= 48000;
392 set_input_clock(chip
, ECHO_CLOCK_INTERNAL
);
395 /* Clear the current digital mode */
396 control_reg
= le32_to_cpu(chip
->comm_page
->control_register
);
397 control_reg
&= GML_DIGITAL_MODE_CLEAR_MASK
;
399 /* Tweak the control reg */
401 case DIGITAL_MODE_SPDIF_OPTICAL
:
402 control_reg
|= GML_SPDIF_OPTICAL_MODE
;
404 case DIGITAL_MODE_SPDIF_RCA
:
405 /* GML_SPDIF_OPTICAL_MODE bit cleared */
407 case DIGITAL_MODE_ADAT
:
408 /* If the current ASIC is the 96KHz ASIC, switch the ASIC
410 if (chip
->asic_code
== FW_MONA_361_1_ASIC96
||
411 chip
->asic_code
== FW_MONA_301_1_ASIC96
) {
412 set_sample_rate(chip
, 48000);
414 control_reg
|= GML_ADAT_MODE
;
415 control_reg
&= ~GML_DOUBLE_SPEED_MODE
;
419 err
= write_control_reg(chip
, control_reg
, FALSE
);
420 spin_unlock_irq(&chip
->lock
);
423 chip
->digital_mode
= mode
;
425 DE_ACT(("set_digital_mode to %d\n", mode
));
426 return incompatible_clock
;