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 if (snd_BUG_ON((subdevice_id
& 0xfff0) != MONA
))
47 if ((err
= init_dsp_comm_page(chip
))) {
48 dev_err(chip
->card
->dev
,
49 "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;
79 static int set_mixer_defaults(struct echoaudio
*chip
)
81 chip
->digital_mode
= DIGITAL_MODE_SPDIF_RCA
;
82 chip
->professional_spdif
= false;
83 chip
->digital_in_automute
= true;
84 return init_line_levels(chip
);
89 static u32
detect_input_clocks(const struct echoaudio
*chip
)
91 u32 clocks_from_dsp
, clock_bits
;
93 /* Map the DSP clock detect bits to the generic driver clock
95 clocks_from_dsp
= le32_to_cpu(chip
->comm_page
->status_clocks
);
97 clock_bits
= ECHO_CLOCK_BIT_INTERNAL
;
99 if (clocks_from_dsp
& GML_CLOCK_DETECT_BIT_SPDIF
)
100 clock_bits
|= ECHO_CLOCK_BIT_SPDIF
;
102 if (clocks_from_dsp
& GML_CLOCK_DETECT_BIT_ADAT
)
103 clock_bits
|= ECHO_CLOCK_BIT_ADAT
;
105 if (clocks_from_dsp
& GML_CLOCK_DETECT_BIT_WORD
)
106 clock_bits
|= ECHO_CLOCK_BIT_WORD
;
113 /* Mona has an ASIC on the PCI card and another ASIC in the external box;
114 both need to be loaded. */
115 static int load_asic(struct echoaudio
*chip
)
121 if (chip
->asic_loaded
)
126 if (chip
->device_id
== DEVICE_ID_56361
)
127 asic
= FW_MONA_361_1_ASIC48
;
129 asic
= FW_MONA_301_1_ASIC48
;
131 err
= load_asic_generic(chip
, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC
, asic
);
135 chip
->asic_code
= asic
;
138 /* Do the external one */
139 err
= load_asic_generic(chip
, DSP_FNC_LOAD_MONA_EXTERNAL_ASIC
,
145 err
= check_asic_status(chip
);
147 /* Set up the control register if the load succeeded -
148 48 kHz, internal clock, S/PDIF RCA mode */
150 control_reg
= GML_CONVERTER_ENABLE
| GML_48KHZ
;
151 err
= write_control_reg(chip
, control_reg
, true);
159 /* Depending on what digital mode you want, Mona needs different ASICs
160 loaded. This function checks the ASIC needed for the new mode and sees
161 if it matches the one already loaded. */
162 static int switch_asic(struct echoaudio
*chip
, char double_speed
)
167 /* Check the clock detect bits to see if this is
168 a single-speed clock or a double-speed clock; load
169 a new ASIC if necessary. */
170 if (chip
->device_id
== DEVICE_ID_56361
) {
172 asic
= FW_MONA_361_1_ASIC96
;
174 asic
= FW_MONA_361_1_ASIC48
;
177 asic
= FW_MONA_301_1_ASIC96
;
179 asic
= FW_MONA_301_1_ASIC48
;
182 if (asic
!= chip
->asic_code
) {
183 /* Load the desired ASIC */
184 err
= load_asic_generic(chip
, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC
,
188 chip
->asic_code
= asic
;
196 static int set_sample_rate(struct echoaudio
*chip
, u32 rate
)
198 u32 control_reg
, clock
;
202 /* Only set the clock for internal mode. */
203 if (chip
->input_clock
!= ECHO_CLOCK_INTERNAL
) {
204 dev_dbg(chip
->card
->dev
,
205 "Cannot set sample rate - clock not set to CLK_CLOCKININTERNAL\n");
206 /* Save the rate anyhow */
207 chip
->comm_page
->sample_rate
= cpu_to_le32(rate
);
208 chip
->sample_rate
= rate
;
212 /* Now, check to see if the required ASIC is loaded */
214 if (chip
->digital_mode
== DIGITAL_MODE_ADAT
)
216 if (chip
->device_id
== DEVICE_ID_56361
)
217 asic
= FW_MONA_361_1_ASIC96
;
219 asic
= FW_MONA_301_1_ASIC96
;
221 if (chip
->device_id
== DEVICE_ID_56361
)
222 asic
= FW_MONA_361_1_ASIC48
;
224 asic
= FW_MONA_301_1_ASIC48
;
228 if (asic
!= chip
->asic_code
) {
230 /* Load the desired ASIC (load_asic_generic() can sleep) */
231 spin_unlock_irq(&chip
->lock
);
232 err
= load_asic_generic(chip
, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC
,
234 spin_lock_irq(&chip
->lock
);
238 chip
->asic_code
= asic
;
242 /* Compute the new control register value */
244 control_reg
= le32_to_cpu(chip
->comm_page
->control_register
);
245 control_reg
&= GML_CLOCK_CLEAR_MASK
;
246 control_reg
&= GML_SPDIF_RATE_CLEAR_MASK
;
256 clock
= GML_48KHZ
| GML_SPDIF_SAMPLE_RATE1
;
260 /* Professional mode */
261 if (control_reg
& GML_SPDIF_PRO_MODE
)
262 clock
|= GML_SPDIF_SAMPLE_RATE0
;
265 clock
= GML_32KHZ
| GML_SPDIF_SAMPLE_RATE0
|
266 GML_SPDIF_SAMPLE_RATE1
;
281 dev_err(chip
->card
->dev
,
282 "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 dev_dbg(chip
->card
->dev
,
291 "set_sample_rate: %d clock %d\n", rate
, clock
);
293 return write_control_reg(chip
, control_reg
, force_write
);
298 static int set_input_clock(struct echoaudio
*chip
, u16 clock
)
300 u32 control_reg
, clocks_from_dsp
;
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 chip
->input_clock
= ECHO_CLOCK_INTERNAL
;
316 return set_sample_rate(chip
, chip
->sample_rate
);
317 case ECHO_CLOCK_SPDIF
:
318 if (chip
->digital_mode
== DIGITAL_MODE_ADAT
)
320 spin_unlock_irq(&chip
->lock
);
321 err
= switch_asic(chip
, clocks_from_dsp
&
322 GML_CLOCK_DETECT_BIT_SPDIF96
);
323 spin_lock_irq(&chip
->lock
);
326 control_reg
|= GML_SPDIF_CLOCK
;
327 if (clocks_from_dsp
& GML_CLOCK_DETECT_BIT_SPDIF96
)
328 control_reg
|= GML_DOUBLE_SPEED_MODE
;
330 control_reg
&= ~GML_DOUBLE_SPEED_MODE
;
332 case ECHO_CLOCK_WORD
:
333 spin_unlock_irq(&chip
->lock
);
334 err
= switch_asic(chip
, clocks_from_dsp
&
335 GML_CLOCK_DETECT_BIT_WORD96
);
336 spin_lock_irq(&chip
->lock
);
339 control_reg
|= GML_WORD_CLOCK
;
340 if (clocks_from_dsp
& GML_CLOCK_DETECT_BIT_WORD96
)
341 control_reg
|= GML_DOUBLE_SPEED_MODE
;
343 control_reg
&= ~GML_DOUBLE_SPEED_MODE
;
345 case ECHO_CLOCK_ADAT
:
346 dev_dbg(chip
->card
->dev
, "Set Mona clock to ADAT\n");
347 if (chip
->digital_mode
!= DIGITAL_MODE_ADAT
)
349 control_reg
|= GML_ADAT_CLOCK
;
350 control_reg
&= ~GML_DOUBLE_SPEED_MODE
;
353 dev_err(chip
->card
->dev
,
354 "Input clock 0x%x not supported for Mona\n", clock
);
358 chip
->input_clock
= clock
;
359 return write_control_reg(chip
, control_reg
, true);
364 static int dsp_set_digital_mode(struct echoaudio
*chip
, u8 mode
)
367 int err
, incompatible_clock
;
369 /* Set clock to "internal" if it's not compatible with the new mode */
370 incompatible_clock
= false;
372 case DIGITAL_MODE_SPDIF_OPTICAL
:
373 case DIGITAL_MODE_SPDIF_RCA
:
374 if (chip
->input_clock
== ECHO_CLOCK_ADAT
)
375 incompatible_clock
= true;
377 case DIGITAL_MODE_ADAT
:
378 if (chip
->input_clock
== ECHO_CLOCK_SPDIF
)
379 incompatible_clock
= true;
382 dev_err(chip
->card
->dev
,
383 "Digital mode not supported: %d\n", mode
);
387 spin_lock_irq(&chip
->lock
);
389 if (incompatible_clock
) { /* Switch to 48KHz, internal */
390 chip
->sample_rate
= 48000;
391 set_input_clock(chip
, ECHO_CLOCK_INTERNAL
);
394 /* Clear the current digital mode */
395 control_reg
= le32_to_cpu(chip
->comm_page
->control_register
);
396 control_reg
&= GML_DIGITAL_MODE_CLEAR_MASK
;
398 /* Tweak the control reg */
400 case DIGITAL_MODE_SPDIF_OPTICAL
:
401 control_reg
|= GML_SPDIF_OPTICAL_MODE
;
403 case DIGITAL_MODE_SPDIF_RCA
:
404 /* GML_SPDIF_OPTICAL_MODE bit cleared */
406 case DIGITAL_MODE_ADAT
:
407 /* If the current ASIC is the 96KHz ASIC, switch the ASIC
409 if (chip
->asic_code
== FW_MONA_361_1_ASIC96
||
410 chip
->asic_code
== FW_MONA_301_1_ASIC96
) {
411 set_sample_rate(chip
, 48000);
413 control_reg
|= GML_ADAT_MODE
;
414 control_reg
&= ~GML_DOUBLE_SPEED_MODE
;
418 err
= write_control_reg(chip
, control_reg
, false);
419 spin_unlock_irq(&chip
->lock
);
422 chip
->digital_mode
= mode
;
424 dev_dbg(chip
->card
->dev
, "set_digital_mode to %d\n", mode
);
425 return incompatible_clock
;