[ALSA] Add echoaudio sound drivers
[linux-2.6.git] / sound / pci / echoaudio / gina20_dsp.c
1 /****************************************************************************
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
32 static int set_professional_spdif(struct echoaudio *chip, char prof);
33 static int update_flags(struct echoaudio *chip);
34
35
36 static int init_hw(struct echoaudio *chip, u16 device_id, u16 subdevice_id)
37 {
38         int err;
39
40         DE_INIT(("init_hw() - Gina20\n"));
41         snd_assert((subdevice_id & 0xfff0) == GINA20, return -ENODEV);
42
43         if ((err = init_dsp_comm_page(chip))) {
44                 DE_INIT(("init_hw - could not initialize DSP comm page\n"));
45                 return err;
46         }
47
48         chip->device_id = device_id;
49         chip->subdevice_id = subdevice_id;
50         chip->bad_board = TRUE;
51         chip->dsp_code_to_load = &card_fw[FW_GINA20_DSP];
52         chip->spdif_status = GD_SPDIF_STATUS_UNDEF;
53         chip->clock_state = GD_CLOCK_UNDEF;
54         /* Since this card has no ASIC, mark it as loaded so everything
55            works OK */
56         chip->asic_loaded = TRUE;
57         chip->input_clock_types = ECHO_CLOCK_BIT_INTERNAL |
58                 ECHO_CLOCK_BIT_SPDIF;
59
60         if ((err = load_firmware(chip)) < 0)
61                 return err;
62         chip->bad_board = FALSE;
63
64         if ((err = init_line_levels(chip)) < 0)
65                 return err;
66
67         err = set_professional_spdif(chip, TRUE);
68
69         DE_INIT(("init_hw done\n"));
70         return err;
71 }
72
73
74
75 static u32 detect_input_clocks(const struct echoaudio *chip)
76 {
77         u32 clocks_from_dsp, clock_bits;
78
79         /* Map the DSP clock detect bits to the generic driver clock
80            detect bits */
81         clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
82
83         clock_bits = ECHO_CLOCK_BIT_INTERNAL;
84
85         if (clocks_from_dsp & GLDM_CLOCK_DETECT_BIT_SPDIF)
86                 clock_bits |= ECHO_CLOCK_BIT_SPDIF;
87
88         return clock_bits;
89 }
90
91
92
93 /* The Gina20 has no ASIC. Just do nothing */
94 static int load_asic(struct echoaudio *chip)
95 {
96         return 0;
97 }
98
99
100
101 static int set_sample_rate(struct echoaudio *chip, u32 rate)
102 {
103         u8 clock_state, spdif_status;
104
105         if (wait_handshake(chip))
106                 return -EIO;
107
108         switch (rate) {
109         case 44100:
110                 clock_state = GD_CLOCK_44;
111                 spdif_status = GD_SPDIF_STATUS_44;
112                 break;
113         case 48000:
114                 clock_state = GD_CLOCK_48;
115                 spdif_status = GD_SPDIF_STATUS_48;
116                 break;
117         default:
118                 clock_state = GD_CLOCK_NOCHANGE;
119                 spdif_status = GD_SPDIF_STATUS_NOCHANGE;
120                 break;
121         }
122
123         if (chip->clock_state == clock_state)
124                 clock_state = GD_CLOCK_NOCHANGE;
125         if (spdif_status == chip->spdif_status)
126                 spdif_status = GD_SPDIF_STATUS_NOCHANGE;
127
128         chip->comm_page->sample_rate = cpu_to_le32(rate);
129         chip->comm_page->gd_clock_state = clock_state;
130         chip->comm_page->gd_spdif_status = spdif_status;
131         chip->comm_page->gd_resampler_state = 3;        /* magic number - should always be 3 */
132
133         /* Save the new audio state if it changed */
134         if (clock_state != GD_CLOCK_NOCHANGE)
135                 chip->clock_state = clock_state;
136         if (spdif_status != GD_SPDIF_STATUS_NOCHANGE)
137                 chip->spdif_status = spdif_status;
138         chip->sample_rate = rate;
139
140         clear_handshake(chip);
141         return send_vector(chip, DSP_VC_SET_GD_AUDIO_STATE);
142 }
143
144
145
146 static int set_input_clock(struct echoaudio *chip, u16 clock)
147 {
148         DE_ACT(("set_input_clock:\n"));
149
150         switch (clock) {
151         case ECHO_CLOCK_INTERNAL:
152                 /* Reset the audio state to unknown (just in case) */
153                 chip->clock_state = GD_CLOCK_UNDEF;
154                 chip->spdif_status = GD_SPDIF_STATUS_UNDEF;
155                 set_sample_rate(chip, chip->sample_rate);
156                 chip->input_clock = clock;
157                 DE_ACT(("Set Gina clock to INTERNAL\n"));
158                 break;
159         case ECHO_CLOCK_SPDIF:
160                 chip->comm_page->gd_clock_state = GD_CLOCK_SPDIFIN;
161                 chip->comm_page->gd_spdif_status = GD_SPDIF_STATUS_NOCHANGE;
162                 clear_handshake(chip);
163                 send_vector(chip, DSP_VC_SET_GD_AUDIO_STATE);
164                 chip->clock_state = GD_CLOCK_SPDIFIN;
165                 DE_ACT(("Set Gina20 clock to SPDIF\n"));
166                 chip->input_clock = clock;
167                 break;
168         default:
169                 return -EINVAL;
170         }
171
172         return 0;
173 }
174
175
176
177 /* Set input bus gain (one unit is 0.5dB !) */
178 static int set_input_gain(struct echoaudio *chip, u16 input, int gain)
179 {
180         snd_assert(input < num_busses_in(chip), return -EINVAL);
181
182         if (wait_handshake(chip))
183                 return -EIO;
184
185         chip->input_gain[input] = gain;
186         gain += GL20_INPUT_GAIN_MAGIC_NUMBER;
187         chip->comm_page->line_in_level[input] = gain;
188         return 0;
189 }
190
191
192
193 /* Tell the DSP to reread the flags from the comm page */
194 static int update_flags(struct echoaudio *chip)
195 {
196         if (wait_handshake(chip))
197                 return -EIO;
198         clear_handshake(chip);
199         return send_vector(chip, DSP_VC_UPDATE_FLAGS);
200 }
201
202
203
204 static int set_professional_spdif(struct echoaudio *chip, char prof)
205 {
206         DE_ACT(("set_professional_spdif %d\n", prof));
207         if (prof)
208                 chip->comm_page->flags |=
209                         __constant_cpu_to_le32(DSP_FLAG_PROFESSIONAL_SPDIF);
210         else
211                 chip->comm_page->flags &=
212                         ~__constant_cpu_to_le32(DSP_FLAG_PROFESSIONAL_SPDIF);
213         chip->professional_spdif = prof;
214         return update_flags(chip);
215 }