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,
37 const struct firmware *asic);
38 static int check_asic_status(struct echoaudio *chip);
41 static int init_hw(struct echoaudio *chip, u16 device_id, u16 subdevice_id)
45 DE_INIT(("init_hw() - Mona\n"));
46 snd_assert((subdevice_id & 0xfff0) == MONA, return -ENODEV);
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 = &card_fw[FW_MONA_361_DSP];
68 chip->dsp_code_to_load = &card_fw[FW_MONA_301_DSP];
70 chip->digital_mode = DIGITAL_MODE_SPDIF_RCA;
71 chip->professional_spdif = FALSE;
72 chip->digital_in_automute = TRUE;
74 if ((err = load_firmware(chip)) < 0)
76 chip->bad_board = FALSE;
78 if ((err = init_line_levels(chip)) < 0)
81 err = set_digital_mode(chip, DIGITAL_MODE_SPDIF_RCA);
82 snd_assert(err >= 0, return err);
83 err = set_professional_spdif(chip, TRUE);
85 DE_INIT(("init_hw done\n"));
91 static u32 detect_input_clocks(const struct echoaudio *chip)
93 u32 clocks_from_dsp, clock_bits;
95 /* Map the DSP clock detect bits to the generic driver clock
97 clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
99 clock_bits = ECHO_CLOCK_BIT_INTERNAL;
101 if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_SPDIF)
102 clock_bits |= ECHO_CLOCK_BIT_SPDIF;
104 if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_ADAT)
105 clock_bits |= ECHO_CLOCK_BIT_ADAT;
107 if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_WORD)
108 clock_bits |= ECHO_CLOCK_BIT_WORD;
115 /* Mona has an ASIC on the PCI card and another ASIC in the external box;
116 both need to be loaded. */
117 static int load_asic(struct echoaudio *chip)
121 const struct firmware *asic;
123 if (chip->asic_loaded)
128 if (chip->device_id == DEVICE_ID_56361)
129 asic = &card_fw[FW_MONA_361_1_ASIC48];
131 asic = &card_fw[FW_MONA_301_1_ASIC48];
133 err = load_asic_generic(chip, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC, asic);
137 chip->asic_code = asic;
140 /* Do the external one */
141 err = load_asic_generic(chip, DSP_FNC_LOAD_MONA_EXTERNAL_ASIC,
142 &card_fw[FW_MONA_2_ASIC]);
147 err = check_asic_status(chip);
149 /* Set up the control register if the load succeeded -
150 48 kHz, internal clock, S/PDIF RCA mode */
152 control_reg = GML_CONVERTER_ENABLE | GML_48KHZ;
153 err = write_control_reg(chip, control_reg, TRUE);
161 /* Depending on what digital mode you want, Mona needs different ASICs
162 loaded. This function checks the ASIC needed for the new mode and sees
163 if it matches the one already loaded. */
164 static int switch_asic(struct echoaudio *chip, char double_speed)
166 const struct firmware *asic;
169 /* Check the clock detect bits to see if this is
170 a single-speed clock or a double-speed clock; load
171 a new ASIC if necessary. */
172 if (chip->device_id == DEVICE_ID_56361) {
174 asic = &card_fw[FW_MONA_361_1_ASIC96];
176 asic = &card_fw[FW_MONA_361_1_ASIC48];
179 asic = &card_fw[FW_MONA_301_1_ASIC96];
181 asic = &card_fw[FW_MONA_301_1_ASIC48];
184 if (asic != chip->asic_code) {
185 /* Load the desired ASIC */
186 err = load_asic_generic(chip, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC,
190 chip->asic_code = asic;
198 static int set_sample_rate(struct echoaudio *chip, u32 rate)
200 u32 control_reg, clock;
201 const struct firmware *asic;
204 /* Only set the clock for internal mode. */
205 if (chip->input_clock != ECHO_CLOCK_INTERNAL) {
206 DE_ACT(("set_sample_rate: Cannot set sample rate - "
207 "clock not set to CLK_CLOCKININTERNAL\n"));
208 /* Save the rate anyhow */
209 chip->comm_page->sample_rate = cpu_to_le32(rate);
210 chip->sample_rate = rate;
214 /* Now, check to see if the required ASIC is loaded */
216 if (chip->digital_mode == DIGITAL_MODE_ADAT)
218 if (chip->device_id == DEVICE_ID_56361)
219 asic = &card_fw[FW_MONA_361_1_ASIC96];
221 asic = &card_fw[FW_MONA_301_1_ASIC96];
223 if (chip->device_id == DEVICE_ID_56361)
224 asic = &card_fw[FW_MONA_361_1_ASIC48];
226 asic = &card_fw[FW_MONA_301_1_ASIC48];
230 if (asic != chip->asic_code) {
232 /* Load the desired ASIC (load_asic_generic() can sleep) */
233 spin_unlock_irq(&chip->lock);
234 err = load_asic_generic(chip, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC,
236 spin_lock_irq(&chip->lock);
240 chip->asic_code = asic;
244 /* Compute the new control register value */
246 control_reg = le32_to_cpu(chip->comm_page->control_register);
247 control_reg &= GML_CLOCK_CLEAR_MASK;
248 control_reg &= GML_SPDIF_RATE_CLEAR_MASK;
258 clock = GML_48KHZ | GML_SPDIF_SAMPLE_RATE1;
262 /* Professional mode */
263 if (control_reg & GML_SPDIF_PRO_MODE)
264 clock |= GML_SPDIF_SAMPLE_RATE0;
267 clock = GML_32KHZ | GML_SPDIF_SAMPLE_RATE0 |
268 GML_SPDIF_SAMPLE_RATE1;
283 DE_ACT(("set_sample_rate: %d invalid!\n", rate));
287 control_reg |= clock;
289 chip->comm_page->sample_rate = cpu_to_le32(rate); /* ignored by the DSP */
290 chip->sample_rate = rate;
291 DE_ACT(("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;
303 DE_ACT(("set_input_clock:\n"));
305 /* Prevent two simultaneous calls to switch_asic() */
306 if (atomic_read(&chip->opencount))
309 /* Mask off the clock select bits */
310 control_reg = le32_to_cpu(chip->comm_page->control_register) &
311 GML_CLOCK_CLEAR_MASK;
312 clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
315 case ECHO_CLOCK_INTERNAL:
316 DE_ACT(("Set Mona clock to INTERNAL\n"));
317 chip->input_clock = ECHO_CLOCK_INTERNAL;
318 return set_sample_rate(chip, chip->sample_rate);
319 case ECHO_CLOCK_SPDIF:
320 if (chip->digital_mode == DIGITAL_MODE_ADAT)
322 spin_unlock_irq(&chip->lock);
323 err = switch_asic(chip, clocks_from_dsp &
324 GML_CLOCK_DETECT_BIT_SPDIF96);
325 spin_lock_irq(&chip->lock);
328 DE_ACT(("Set Mona clock to SPDIF\n"));
329 control_reg |= GML_SPDIF_CLOCK;
330 if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_SPDIF96)
331 control_reg |= GML_DOUBLE_SPEED_MODE;
333 control_reg &= ~GML_DOUBLE_SPEED_MODE;
335 case ECHO_CLOCK_WORD:
336 DE_ACT(("Set Mona clock to WORD\n"));
337 spin_unlock_irq(&chip->lock);
338 err = switch_asic(chip, clocks_from_dsp &
339 GML_CLOCK_DETECT_BIT_WORD96);
340 spin_lock_irq(&chip->lock);
343 control_reg |= GML_WORD_CLOCK;
344 if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_WORD96)
345 control_reg |= GML_DOUBLE_SPEED_MODE;
347 control_reg &= ~GML_DOUBLE_SPEED_MODE;
349 case ECHO_CLOCK_ADAT:
350 DE_ACT(("Set Mona clock to ADAT\n"));
351 if (chip->digital_mode != DIGITAL_MODE_ADAT)
353 control_reg |= GML_ADAT_CLOCK;
354 control_reg &= ~GML_DOUBLE_SPEED_MODE;
357 DE_ACT(("Input clock 0x%x not supported for Mona\n", clock));
361 chip->input_clock = clock;
362 return write_control_reg(chip, control_reg, TRUE);
367 static int dsp_set_digital_mode(struct echoaudio *chip, u8 mode)
370 int err, incompatible_clock;
372 /* Set clock to "internal" if it's not compatible with the new mode */
373 incompatible_clock = FALSE;
375 case DIGITAL_MODE_SPDIF_OPTICAL:
376 case DIGITAL_MODE_SPDIF_RCA:
377 if (chip->input_clock == ECHO_CLOCK_ADAT)
378 incompatible_clock = TRUE;
380 case DIGITAL_MODE_ADAT:
381 if (chip->input_clock == ECHO_CLOCK_SPDIF)
382 incompatible_clock = TRUE;
385 DE_ACT(("Digital mode not supported: %d\n", mode));
389 spin_lock_irq(&chip->lock);
391 if (incompatible_clock) { /* Switch to 48KHz, internal */
392 chip->sample_rate = 48000;
393 set_input_clock(chip, ECHO_CLOCK_INTERNAL);
396 /* Clear the current digital mode */
397 control_reg = le32_to_cpu(chip->comm_page->control_register);
398 control_reg &= GML_DIGITAL_MODE_CLEAR_MASK;
400 /* Tweak the control reg */
402 case DIGITAL_MODE_SPDIF_OPTICAL:
403 control_reg |= GML_SPDIF_OPTICAL_MODE;
405 case DIGITAL_MODE_SPDIF_RCA:
406 /* GML_SPDIF_OPTICAL_MODE bit cleared */
408 case DIGITAL_MODE_ADAT:
409 /* If the current ASIC is the 96KHz ASIC, switch the ASIC
411 if (chip->asic_code == &card_fw[FW_MONA_361_1_ASIC96] ||
412 chip->asic_code == &card_fw[FW_MONA_301_1_ASIC96]) {
413 set_sample_rate(chip, 48000);
415 control_reg |= GML_ADAT_MODE;
416 control_reg &= ~GML_DOUBLE_SPEED_MODE;
420 err = write_control_reg(chip, control_reg, FALSE);
421 spin_unlock_irq(&chip->lock);
424 chip->digital_mode = mode;
426 DE_ACT(("set_digital_mode to %d\n", mode));
427 return incompatible_clock;