blob: 7f0f6ea08ca7334dea07089b41bb64b98cb4263f [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 read_dsp(struct echoaudio *chip, u32 *data);
33static int set_professional_spdif(struct echoaudio *chip, char prof);
Giuliano Pochini19b50062010-02-14 18:15:34 +010034static int load_asic_generic(struct echoaudio *chip, u32 cmd, short asic);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020035static int check_asic_status(struct echoaudio *chip);
36static int update_flags(struct echoaudio *chip);
37
38
39static int init_hw(struct echoaudio *chip, u16 device_id, u16 subdevice_id)
40{
41 int err;
42
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +053043 dev_dbg(chip->card->dev, "init_hw() - Layla20\n");
Takashi Iwaida3cec32008-08-08 17:12:14 +020044 if (snd_BUG_ON((subdevice_id & 0xfff0) != LAYLA20))
45 return -ENODEV;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020046
47 if ((err = init_dsp_comm_page(chip))) {
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +053048 dev_err(chip->card->dev,
49 "init_hw - could not initialize DSP comm page\n");
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020050 return err;
51 }
52
53 chip->device_id = device_id;
54 chip->subdevice_id = subdevice_id;
55 chip->bad_board = TRUE;
56 chip->has_midi = TRUE;
Giuliano Pochini19b50062010-02-14 18:15:34 +010057 chip->dsp_code_to_load = FW_LAYLA20_DSP;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020058 chip->input_clock_types =
59 ECHO_CLOCK_BIT_INTERNAL | ECHO_CLOCK_BIT_SPDIF |
60 ECHO_CLOCK_BIT_WORD | ECHO_CLOCK_BIT_SUPER;
61 chip->output_clock_types =
62 ECHO_CLOCK_BIT_WORD | ECHO_CLOCK_BIT_SUPER;
63
64 if ((err = load_firmware(chip)) < 0)
65 return err;
66 chip->bad_board = FALSE;
67
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +053068 dev_dbg(chip->card->dev, "init_hw done\n");
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020069 return err;
70}
71
72
73
Giuliano Pochiniad3499f2010-02-14 18:15:59 +010074static int set_mixer_defaults(struct echoaudio *chip)
75{
76 chip->professional_spdif = FALSE;
77 return init_line_levels(chip);
78}
79
80
81
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020082static u32 detect_input_clocks(const struct echoaudio *chip)
83{
84 u32 clocks_from_dsp, clock_bits;
85
86 /* Map the DSP clock detect bits to the generic driver clock detect bits */
87 clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
88
89 clock_bits = ECHO_CLOCK_BIT_INTERNAL;
90
91 if (clocks_from_dsp & GLDM_CLOCK_DETECT_BIT_SPDIF)
92 clock_bits |= ECHO_CLOCK_BIT_SPDIF;
93
94 if (clocks_from_dsp & GLDM_CLOCK_DETECT_BIT_WORD) {
95 if (clocks_from_dsp & GLDM_CLOCK_DETECT_BIT_SUPER)
96 clock_bits |= ECHO_CLOCK_BIT_SUPER;
97 else
98 clock_bits |= ECHO_CLOCK_BIT_WORD;
99 }
100
101 return clock_bits;
102}
103
104
105
106/* ASIC status check - some cards have one or two ASICs that need to be
107loaded. Once that load is complete, this function is called to see if
108the load was successful.
109If this load fails, it does not necessarily mean that the hardware is
110defective - the external box may be disconnected or turned off.
111This routine sometimes fails for Layla20; for Layla20, the loop runs
1125 times and succeeds if it wins on three of the loops. */
113static int check_asic_status(struct echoaudio *chip)
114{
115 u32 asic_status;
116 int goodcnt, i;
117
118 chip->asic_loaded = FALSE;
119 for (i = goodcnt = 0; i < 5; i++) {
120 send_vector(chip, DSP_VC_TEST_ASIC);
121
122 /* The DSP will return a value to indicate whether or not
123 the ASIC is currently loaded */
124 if (read_dsp(chip, &asic_status) < 0) {
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530125 dev_err(chip->card->dev,
126 "check_asic_status: failed on read_dsp\n");
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200127 return -EIO;
128 }
129
130 if (asic_status == ASIC_ALREADY_LOADED) {
131 if (++goodcnt == 3) {
132 chip->asic_loaded = TRUE;
133 return 0;
134 }
135 }
136 }
137 return -EIO;
138}
139
140
141
142/* Layla20 has an ASIC in the external box */
143static int load_asic(struct echoaudio *chip)
144{
145 int err;
146
147 if (chip->asic_loaded)
148 return 0;
149
150 err = load_asic_generic(chip, DSP_FNC_LOAD_LAYLA_ASIC,
Giuliano Pochini19b50062010-02-14 18:15:34 +0100151 FW_LAYLA20_ASIC);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200152 if (err < 0)
153 return err;
154
155 /* Check if ASIC is alive and well. */
156 return check_asic_status(chip);
157}
158
159
160
161static int set_sample_rate(struct echoaudio *chip, u32 rate)
162{
Takashi Iwaida3cec32008-08-08 17:12:14 +0200163 if (snd_BUG_ON(rate < 8000 || rate > 50000))
164 return -EINVAL;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200165
166 /* Only set the clock for internal mode. Do not return failure,
167 simply treat it as a non-event. */
168 if (chip->input_clock != ECHO_CLOCK_INTERNAL) {
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530169 dev_warn(chip->card->dev,
170 "Cannot set sample rate - clock not set to CLK_CLOCKININTERNAL\n");
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200171 chip->comm_page->sample_rate = cpu_to_le32(rate);
172 chip->sample_rate = rate;
173 return 0;
174 }
175
176 if (wait_handshake(chip))
177 return -EIO;
178
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530179 dev_dbg(chip->card->dev, "set_sample_rate(%d)\n", rate);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200180 chip->sample_rate = rate;
181 chip->comm_page->sample_rate = cpu_to_le32(rate);
182 clear_handshake(chip);
183 return send_vector(chip, DSP_VC_SET_LAYLA_SAMPLE_RATE);
184}
185
186
187
188static int set_input_clock(struct echoaudio *chip, u16 clock_source)
189{
190 u16 clock;
191 u32 rate;
192
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530193 dev_dbg(chip->card->dev, "set_input_clock:\n");
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200194 rate = 0;
195 switch (clock_source) {
196 case ECHO_CLOCK_INTERNAL:
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530197 dev_dbg(chip->card->dev, "Set Layla20 clock to INTERNAL\n");
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200198 rate = chip->sample_rate;
199 clock = LAYLA20_CLOCK_INTERNAL;
200 break;
201 case ECHO_CLOCK_SPDIF:
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530202 dev_dbg(chip->card->dev, "Set Layla20 clock to SPDIF\n");
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200203 clock = LAYLA20_CLOCK_SPDIF;
204 break;
205 case ECHO_CLOCK_WORD:
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530206 dev_dbg(chip->card->dev, "Set Layla20 clock to WORD\n");
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200207 clock = LAYLA20_CLOCK_WORD;
208 break;
209 case ECHO_CLOCK_SUPER:
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530210 dev_dbg(chip->card->dev, "Set Layla20 clock to SUPER\n");
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200211 clock = LAYLA20_CLOCK_SUPER;
212 break;
213 default:
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530214 dev_err(chip->card->dev,
215 "Input clock 0x%x not supported for Layla24\n",
216 clock_source);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200217 return -EINVAL;
218 }
219 chip->input_clock = clock_source;
220
221 chip->comm_page->input_clock = cpu_to_le16(clock);
222 clear_handshake(chip);
223 send_vector(chip, DSP_VC_UPDATE_CLOCKS);
224
225 if (rate)
226 set_sample_rate(chip, rate);
227
228 return 0;
229}
230
231
232
233static int set_output_clock(struct echoaudio *chip, u16 clock)
234{
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530235 dev_dbg(chip->card->dev, "set_output_clock: %d\n", clock);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200236 switch (clock) {
237 case ECHO_CLOCK_SUPER:
238 clock = LAYLA20_OUTPUT_CLOCK_SUPER;
239 break;
240 case ECHO_CLOCK_WORD:
241 clock = LAYLA20_OUTPUT_CLOCK_WORD;
242 break;
243 default:
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530244 dev_err(chip->card->dev, "set_output_clock wrong clock\n");
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200245 return -EINVAL;
246 }
247
248 if (wait_handshake(chip))
249 return -EIO;
250
251 chip->comm_page->output_clock = cpu_to_le16(clock);
252 chip->output_clock = clock;
253 clear_handshake(chip);
254 return send_vector(chip, DSP_VC_UPDATE_CLOCKS);
255}
256
257
258
259/* Set input bus gain (one unit is 0.5dB !) */
260static int set_input_gain(struct echoaudio *chip, u16 input, int gain)
261{
Takashi Iwaida3cec32008-08-08 17:12:14 +0200262 if (snd_BUG_ON(input >= num_busses_in(chip)))
263 return -EINVAL;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200264
265 if (wait_handshake(chip))
266 return -EIO;
267
268 chip->input_gain[input] = gain;
269 gain += GL20_INPUT_GAIN_MAGIC_NUMBER;
270 chip->comm_page->line_in_level[input] = gain;
271 return 0;
272}
273
274
275
276/* Tell the DSP to reread the flags from the comm page */
277static int update_flags(struct echoaudio *chip)
278{
279 if (wait_handshake(chip))
280 return -EIO;
281 clear_handshake(chip);
282 return send_vector(chip, DSP_VC_UPDATE_FLAGS);
283}
284
285
286
287static int set_professional_spdif(struct echoaudio *chip, char prof)
288{
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530289 dev_dbg(chip->card->dev, "set_professional_spdif %d\n", prof);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200290 if (prof)
291 chip->comm_page->flags |=
Harvey Harrisone930e992009-02-11 14:49:30 -0800292 cpu_to_le32(DSP_FLAG_PROFESSIONAL_SPDIF);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200293 else
294 chip->comm_page->flags &=
Harvey Harrisone930e992009-02-11 14:49:30 -0800295 ~cpu_to_le32(DSP_FLAG_PROFESSIONAL_SPDIF);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200296 chip->professional_spdif = prof;
297 return update_flags(chip);
298}