blob: b28b8e4703cf184d2bcbf40d9a6445a293748988 [file] [log] [blame]
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001/****************************************************************************
2
3 Copyright Echo Digital Audio Corporation (c) 1998 - 2004
4 All rights reserved
5 www.echoaudio.com
6
7 This file is part of Echo Digital Audio's generic driver library.
8
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
12 Foundation.
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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 MA 02111-1307, USA.
23
24 *************************************************************************
25
26 Translation from C++ and adaptation for use in ALSA-Driver
27 were made by Giuliano Pochini <pochini@shiny.it>
28
29****************************************************************************/
30
31
32static int write_control_reg(struct echoaudio *chip, u32 value, char force);
33static int set_input_clock(struct echoaudio *chip, u16 clock);
34static int set_professional_spdif(struct echoaudio *chip, char prof);
35static int set_digital_mode(struct echoaudio *chip, u8 mode);
Giuliano Pochini19b50062010-02-14 18:15:34 +010036static int load_asic_generic(struct echoaudio *chip, u32 cmd, short asic);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020037static int check_asic_status(struct echoaudio *chip);
38
39
40static int init_hw(struct echoaudio *chip, u16 device_id, u16 subdevice_id)
41{
42 int err;
43
44 DE_INIT(("init_hw() - Mona\n"));
Takashi Iwaida3cec32008-08-08 17:12:14 +020045 if (snd_BUG_ON((subdevice_id & 0xfff0) != MONA))
46 return -ENODEV;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020047
48 if ((err = init_dsp_comm_page(chip))) {
49 DE_INIT(("init_hw - could not initialize DSP comm page\n"));
50 return err;
51 }
52
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;
59 chip->digital_modes =
60 ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_RCA |
61 ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_OPTICAL |
62 ECHOCAPS_HAS_DIGITAL_MODE_ADAT;
63
64 /* Mona comes in both '301 and '361 flavors */
65 if (chip->device_id == DEVICE_ID_56361)
Giuliano Pochini19b50062010-02-14 18:15:34 +010066 chip->dsp_code_to_load = FW_MONA_361_DSP;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020067 else
Giuliano Pochini19b50062010-02-14 18:15:34 +010068 chip->dsp_code_to_load = FW_MONA_301_DSP;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020069
70 chip->digital_mode = DIGITAL_MODE_SPDIF_RCA;
71 chip->professional_spdif = FALSE;
72 chip->digital_in_automute = TRUE;
73
74 if ((err = load_firmware(chip)) < 0)
75 return err;
76 chip->bad_board = FALSE;
77
78 if ((err = init_line_levels(chip)) < 0)
79 return err;
80
81 err = set_digital_mode(chip, DIGITAL_MODE_SPDIF_RCA);
Takashi Iwaida3cec32008-08-08 17:12:14 +020082 if (err < 0)
83 return err;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020084 err = set_professional_spdif(chip, TRUE);
85
86 DE_INIT(("init_hw done\n"));
87 return err;
88}
89
90
91
92static u32 detect_input_clocks(const struct echoaudio *chip)
93{
94 u32 clocks_from_dsp, clock_bits;
95
96 /* Map the DSP clock detect bits to the generic driver clock
97 detect bits */
98 clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
99
100 clock_bits = ECHO_CLOCK_BIT_INTERNAL;
101
102 if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_SPDIF)
103 clock_bits |= ECHO_CLOCK_BIT_SPDIF;
104
105 if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_ADAT)
106 clock_bits |= ECHO_CLOCK_BIT_ADAT;
107
108 if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_WORD)
109 clock_bits |= ECHO_CLOCK_BIT_WORD;
110
111 return clock_bits;
112}
113
114
115
116/* Mona has an ASIC on the PCI card and another ASIC in the external box;
117both need to be loaded. */
118static int load_asic(struct echoaudio *chip)
119{
120 u32 control_reg;
121 int err;
Giuliano Pochini19b50062010-02-14 18:15:34 +0100122 short asic;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200123
124 if (chip->asic_loaded)
125 return 0;
126
127 mdelay(10);
128
129 if (chip->device_id == DEVICE_ID_56361)
Giuliano Pochini19b50062010-02-14 18:15:34 +0100130 asic = FW_MONA_361_1_ASIC48;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200131 else
Giuliano Pochini19b50062010-02-14 18:15:34 +0100132 asic = FW_MONA_301_1_ASIC48;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200133
134 err = load_asic_generic(chip, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC, asic);
135 if (err < 0)
136 return err;
137
138 chip->asic_code = asic;
139 mdelay(10);
140
141 /* Do the external one */
142 err = load_asic_generic(chip, DSP_FNC_LOAD_MONA_EXTERNAL_ASIC,
Giuliano Pochini19b50062010-02-14 18:15:34 +0100143 FW_MONA_2_ASIC);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200144 if (err < 0)
145 return err;
146
147 mdelay(10);
148 err = check_asic_status(chip);
149
150 /* Set up the control register if the load succeeded -
151 48 kHz, internal clock, S/PDIF RCA mode */
152 if (!err) {
153 control_reg = GML_CONVERTER_ENABLE | GML_48KHZ;
154 err = write_control_reg(chip, control_reg, TRUE);
155 }
156
157 return err;
158}
159
160
161
162/* Depending on what digital mode you want, Mona needs different ASICs
163loaded. This function checks the ASIC needed for the new mode and sees
164if it matches the one already loaded. */
165static int switch_asic(struct echoaudio *chip, char double_speed)
166{
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200167 int err;
Giuliano Pochini19b50062010-02-14 18:15:34 +0100168 short asic;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200169
170 /* Check the clock detect bits to see if this is
171 a single-speed clock or a double-speed clock; load
172 a new ASIC if necessary. */
173 if (chip->device_id == DEVICE_ID_56361) {
174 if (double_speed)
Giuliano Pochini19b50062010-02-14 18:15:34 +0100175 asic = FW_MONA_361_1_ASIC96;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200176 else
Giuliano Pochini19b50062010-02-14 18:15:34 +0100177 asic = FW_MONA_361_1_ASIC48;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200178 } else {
179 if (double_speed)
Giuliano Pochini19b50062010-02-14 18:15:34 +0100180 asic = FW_MONA_301_1_ASIC96;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200181 else
Giuliano Pochini19b50062010-02-14 18:15:34 +0100182 asic = FW_MONA_301_1_ASIC48;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200183 }
184
185 if (asic != chip->asic_code) {
186 /* Load the desired ASIC */
187 err = load_asic_generic(chip, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC,
188 asic);
189 if (err < 0)
190 return err;
191 chip->asic_code = asic;
192 }
193
194 return 0;
195}
196
197
198
199static int set_sample_rate(struct echoaudio *chip, u32 rate)
200{
201 u32 control_reg, clock;
Giuliano Pochini19b50062010-02-14 18:15:34 +0100202 short asic;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200203 char force_write;
204
205 /* Only set the clock for internal mode. */
206 if (chip->input_clock != ECHO_CLOCK_INTERNAL) {
207 DE_ACT(("set_sample_rate: Cannot set sample rate - "
208 "clock not set to CLK_CLOCKININTERNAL\n"));
209 /* Save the rate anyhow */
210 chip->comm_page->sample_rate = cpu_to_le32(rate);
211 chip->sample_rate = rate;
212 return 0;
213 }
214
215 /* Now, check to see if the required ASIC is loaded */
216 if (rate >= 88200) {
217 if (chip->digital_mode == DIGITAL_MODE_ADAT)
218 return -EINVAL;
219 if (chip->device_id == DEVICE_ID_56361)
Giuliano Pochini19b50062010-02-14 18:15:34 +0100220 asic = FW_MONA_361_1_ASIC96;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200221 else
Giuliano Pochini19b50062010-02-14 18:15:34 +0100222 asic = FW_MONA_301_1_ASIC96;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200223 } else {
224 if (chip->device_id == DEVICE_ID_56361)
Giuliano Pochini19b50062010-02-14 18:15:34 +0100225 asic = FW_MONA_361_1_ASIC48;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200226 else
Giuliano Pochini19b50062010-02-14 18:15:34 +0100227 asic = FW_MONA_301_1_ASIC48;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200228 }
229
230 force_write = 0;
231 if (asic != chip->asic_code) {
232 int err;
233 /* Load the desired ASIC (load_asic_generic() can sleep) */
234 spin_unlock_irq(&chip->lock);
235 err = load_asic_generic(chip, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC,
236 asic);
237 spin_lock_irq(&chip->lock);
238
239 if (err < 0)
240 return err;
241 chip->asic_code = asic;
242 force_write = 1;
243 }
244
245 /* Compute the new control register value */
246 clock = 0;
247 control_reg = le32_to_cpu(chip->comm_page->control_register);
248 control_reg &= GML_CLOCK_CLEAR_MASK;
249 control_reg &= GML_SPDIF_RATE_CLEAR_MASK;
250
251 switch (rate) {
252 case 96000:
253 clock = GML_96KHZ;
254 break;
255 case 88200:
256 clock = GML_88KHZ;
257 break;
258 case 48000:
259 clock = GML_48KHZ | GML_SPDIF_SAMPLE_RATE1;
260 break;
261 case 44100:
262 clock = GML_44KHZ;
263 /* Professional mode */
264 if (control_reg & GML_SPDIF_PRO_MODE)
265 clock |= GML_SPDIF_SAMPLE_RATE0;
266 break;
267 case 32000:
268 clock = GML_32KHZ | GML_SPDIF_SAMPLE_RATE0 |
269 GML_SPDIF_SAMPLE_RATE1;
270 break;
271 case 22050:
272 clock = GML_22KHZ;
273 break;
274 case 16000:
275 clock = GML_16KHZ;
276 break;
277 case 11025:
278 clock = GML_11KHZ;
279 break;
280 case 8000:
281 clock = GML_8KHZ;
282 break;
283 default:
284 DE_ACT(("set_sample_rate: %d invalid!\n", rate));
285 return -EINVAL;
286 }
287
288 control_reg |= clock;
289
290 chip->comm_page->sample_rate = cpu_to_le32(rate); /* ignored by the DSP */
291 chip->sample_rate = rate;
292 DE_ACT(("set_sample_rate: %d clock %d\n", rate, clock));
293
294 return write_control_reg(chip, control_reg, force_write);
295}
296
297
298
299static int set_input_clock(struct echoaudio *chip, u16 clock)
300{
301 u32 control_reg, clocks_from_dsp;
302 int err;
303
304 DE_ACT(("set_input_clock:\n"));
305
306 /* Prevent two simultaneous calls to switch_asic() */
307 if (atomic_read(&chip->opencount))
308 return -EAGAIN;
309
310 /* Mask off the clock select bits */
311 control_reg = le32_to_cpu(chip->comm_page->control_register) &
312 GML_CLOCK_CLEAR_MASK;
313 clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
314
315 switch (clock) {
316 case ECHO_CLOCK_INTERNAL:
317 DE_ACT(("Set Mona clock to INTERNAL\n"));
318 chip->input_clock = ECHO_CLOCK_INTERNAL;
319 return set_sample_rate(chip, chip->sample_rate);
320 case ECHO_CLOCK_SPDIF:
321 if (chip->digital_mode == DIGITAL_MODE_ADAT)
322 return -EAGAIN;
323 spin_unlock_irq(&chip->lock);
324 err = switch_asic(chip, clocks_from_dsp &
325 GML_CLOCK_DETECT_BIT_SPDIF96);
326 spin_lock_irq(&chip->lock);
327 if (err < 0)
328 return err;
329 DE_ACT(("Set Mona clock to SPDIF\n"));
330 control_reg |= GML_SPDIF_CLOCK;
331 if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_SPDIF96)
332 control_reg |= GML_DOUBLE_SPEED_MODE;
333 else
334 control_reg &= ~GML_DOUBLE_SPEED_MODE;
335 break;
336 case ECHO_CLOCK_WORD:
337 DE_ACT(("Set Mona clock to WORD\n"));
338 spin_unlock_irq(&chip->lock);
339 err = switch_asic(chip, clocks_from_dsp &
340 GML_CLOCK_DETECT_BIT_WORD96);
341 spin_lock_irq(&chip->lock);
342 if (err < 0)
343 return err;
344 control_reg |= GML_WORD_CLOCK;
345 if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_WORD96)
346 control_reg |= GML_DOUBLE_SPEED_MODE;
347 else
348 control_reg &= ~GML_DOUBLE_SPEED_MODE;
349 break;
350 case ECHO_CLOCK_ADAT:
351 DE_ACT(("Set Mona clock to ADAT\n"));
352 if (chip->digital_mode != DIGITAL_MODE_ADAT)
353 return -EAGAIN;
354 control_reg |= GML_ADAT_CLOCK;
355 control_reg &= ~GML_DOUBLE_SPEED_MODE;
356 break;
357 default:
358 DE_ACT(("Input clock 0x%x not supported for Mona\n", clock));
359 return -EINVAL;
360 }
361
362 chip->input_clock = clock;
363 return write_control_reg(chip, control_reg, TRUE);
364}
365
366
367
368static int dsp_set_digital_mode(struct echoaudio *chip, u8 mode)
369{
370 u32 control_reg;
371 int err, incompatible_clock;
372
373 /* Set clock to "internal" if it's not compatible with the new mode */
374 incompatible_clock = FALSE;
375 switch (mode) {
376 case DIGITAL_MODE_SPDIF_OPTICAL:
377 case DIGITAL_MODE_SPDIF_RCA:
378 if (chip->input_clock == ECHO_CLOCK_ADAT)
379 incompatible_clock = TRUE;
380 break;
381 case DIGITAL_MODE_ADAT:
382 if (chip->input_clock == ECHO_CLOCK_SPDIF)
383 incompatible_clock = TRUE;
384 break;
385 default:
386 DE_ACT(("Digital mode not supported: %d\n", mode));
387 return -EINVAL;
388 }
389
390 spin_lock_irq(&chip->lock);
391
392 if (incompatible_clock) { /* Switch to 48KHz, internal */
393 chip->sample_rate = 48000;
394 set_input_clock(chip, ECHO_CLOCK_INTERNAL);
395 }
396
397 /* Clear the current digital mode */
398 control_reg = le32_to_cpu(chip->comm_page->control_register);
399 control_reg &= GML_DIGITAL_MODE_CLEAR_MASK;
400
401 /* Tweak the control reg */
402 switch (mode) {
403 case DIGITAL_MODE_SPDIF_OPTICAL:
404 control_reg |= GML_SPDIF_OPTICAL_MODE;
405 break;
406 case DIGITAL_MODE_SPDIF_RCA:
407 /* GML_SPDIF_OPTICAL_MODE bit cleared */
408 break;
409 case DIGITAL_MODE_ADAT:
410 /* If the current ASIC is the 96KHz ASIC, switch the ASIC
411 and set to 48 KHz */
Giuliano Pochini19b50062010-02-14 18:15:34 +0100412 if (chip->asic_code == FW_MONA_361_1_ASIC96 ||
413 chip->asic_code == FW_MONA_301_1_ASIC96) {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200414 set_sample_rate(chip, 48000);
415 }
416 control_reg |= GML_ADAT_MODE;
417 control_reg &= ~GML_DOUBLE_SPEED_MODE;
418 break;
419 }
420
421 err = write_control_reg(chip, control_reg, FALSE);
422 spin_unlock_irq(&chip->lock);
423 if (err < 0)
424 return err;
425 chip->digital_mode = mode;
426
427 DE_ACT(("set_digital_mode to %d\n", mode));
428 return incompatible_clock;
429}