Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / sound / drivers / vx / vx_uer.c
blobfb8932af888d6a523fc5c33f489acb61e5b2d82f
1 /*
2 * Driver for Digigram VX soundcards
4 * IEC958 stuff
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/delay.h>
24 #include <sound/core.h>
25 #include <sound/vx_core.h>
26 #include "vx_cmd.h"
30 * vx_modify_board_clock - tell the board that its clock has been modified
31 * @sync: DSP needs to resynchronize its FIFO
33 static int vx_modify_board_clock(struct vx_core *chip, int sync)
35 struct vx_rmh rmh;
37 vx_init_rmh(&rmh, CMD_MODIFY_CLOCK);
38 /* Ask the DSP to resynchronize its FIFO. */
39 if (sync)
40 rmh.Cmd[0] |= CMD_MODIFY_CLOCK_S_BIT;
41 return vx_send_msg(chip, &rmh);
45 * vx_modify_board_inputs - resync audio inputs
47 static int vx_modify_board_inputs(struct vx_core *chip)
49 struct vx_rmh rmh;
51 vx_init_rmh(&rmh, CMD_RESYNC_AUDIO_INPUTS);
52 rmh.Cmd[0] |= 1 << 0; /* reference: AUDIO 0 */
53 return vx_send_msg(chip, &rmh);
57 * vx_read_one_cbit - read one bit from UER config
58 * @index: the bit index
59 * returns 0 or 1.
61 static int vx_read_one_cbit(struct vx_core *chip, int index)
63 unsigned long flags;
64 int val;
65 spin_lock_irqsave(&chip->lock, flags);
66 if (chip->type >= VX_TYPE_VXPOCKET) {
67 vx_outb(chip, CSUER, 1); /* read */
68 vx_outb(chip, RUER, index & XX_UER_CBITS_OFFSET_MASK);
69 val = (vx_inb(chip, RUER) >> 7) & 0x01;
70 } else {
71 vx_outl(chip, CSUER, 1); /* read */
72 vx_outl(chip, RUER, index & XX_UER_CBITS_OFFSET_MASK);
73 val = (vx_inl(chip, RUER) >> 7) & 0x01;
75 spin_unlock_irqrestore(&chip->lock, flags);
76 return val;
80 * vx_write_one_cbit - write one bit to UER config
81 * @index: the bit index
82 * @val: bit value, 0 or 1
84 static void vx_write_one_cbit(struct vx_core *chip, int index, int val)
86 unsigned long flags;
87 val = !!val; /* 0 or 1 */
88 spin_lock_irqsave(&chip->lock, flags);
89 if (vx_is_pcmcia(chip)) {
90 vx_outb(chip, CSUER, 0); /* write */
91 vx_outb(chip, RUER, (val << 7) | (index & XX_UER_CBITS_OFFSET_MASK));
92 } else {
93 vx_outl(chip, CSUER, 0); /* write */
94 vx_outl(chip, RUER, (val << 7) | (index & XX_UER_CBITS_OFFSET_MASK));
96 spin_unlock_irqrestore(&chip->lock, flags);
100 * vx_read_uer_status - read the current UER status
101 * @mode: pointer to store the UER mode, VX_UER_MODE_XXX
103 * returns the frequency of UER, or 0 if not sync,
104 * or a negative error code.
106 static int vx_read_uer_status(struct vx_core *chip, int *mode)
108 int val, freq;
110 /* Default values */
111 freq = 0;
113 /* Read UER status */
114 if (vx_is_pcmcia(chip))
115 val = vx_inb(chip, CSUER);
116 else
117 val = vx_inl(chip, CSUER);
118 if (val < 0)
119 return val;
120 /* If clock is present, read frequency */
121 if (val & VX_SUER_CLOCK_PRESENT_MASK) {
122 switch (val & VX_SUER_FREQ_MASK) {
123 case VX_SUER_FREQ_32KHz_MASK:
124 freq = 32000;
125 break;
126 case VX_SUER_FREQ_44KHz_MASK:
127 freq = 44100;
128 break;
129 case VX_SUER_FREQ_48KHz_MASK:
130 freq = 48000;
131 break;
134 if (val & VX_SUER_DATA_PRESENT_MASK)
135 /* bit 0 corresponds to consumer/professional bit */
136 *mode = vx_read_one_cbit(chip, 0) ?
137 VX_UER_MODE_PROFESSIONAL : VX_UER_MODE_CONSUMER;
138 else
139 *mode = VX_UER_MODE_NOT_PRESENT;
141 return freq;
146 * compute the sample clock value from frequency
148 * The formula is as follows:
150 * HexFreq = (dword) ((double) ((double) 28224000 / (double) Frequency))
151 * switch ( HexFreq & 0x00000F00 )
152 * case 0x00000100: ;
153 * case 0x00000200:
154 * case 0x00000300: HexFreq -= 0x00000201 ;
155 * case 0x00000400:
156 * case 0x00000500:
157 * case 0x00000600:
158 * case 0x00000700: HexFreq = (dword) (((double) 28224000 / (double) (Frequency*2)) - 1)
159 * default : HexFreq = (dword) ((double) 28224000 / (double) (Frequency*4)) - 0x000001FF
162 static int vx_calc_clock_from_freq(struct vx_core *chip, int freq)
164 int hexfreq;
166 snd_assert(freq > 0, return 0);
168 hexfreq = (28224000 * 10) / freq;
169 hexfreq = (hexfreq + 5) / 10;
171 /* max freq = 55125 Hz */
172 snd_assert(hexfreq > 0x00000200, return 0);
174 if (hexfreq <= 0x03ff)
175 return hexfreq - 0x00000201;
176 if (hexfreq <= 0x07ff)
177 return (hexfreq / 2) - 1;
178 if (hexfreq <= 0x0fff)
179 return (hexfreq / 4) + 0x000001ff;
181 return 0x5fe; /* min freq = 6893 Hz */
186 * vx_change_clock_source - change the clock source
187 * @source: the new source
189 static void vx_change_clock_source(struct vx_core *chip, int source)
191 unsigned long flags;
193 /* we mute DAC to prevent clicks */
194 vx_toggle_dac_mute(chip, 1);
195 spin_lock_irqsave(&chip->lock, flags);
196 chip->ops->set_clock_source(chip, source);
197 chip->clock_source = source;
198 spin_unlock_irqrestore(&chip->lock, flags);
199 /* unmute */
200 vx_toggle_dac_mute(chip, 0);
205 * set the internal clock
207 void vx_set_internal_clock(struct vx_core *chip, unsigned int freq)
209 int clock;
210 unsigned long flags;
211 /* Get real clock value */
212 clock = vx_calc_clock_from_freq(chip, freq);
213 snd_printdd(KERN_DEBUG "set internal clock to 0x%x from freq %d\n", clock, freq);
214 spin_lock_irqsave(&chip->lock, flags);
215 if (vx_is_pcmcia(chip)) {
216 vx_outb(chip, HIFREQ, (clock >> 8) & 0x0f);
217 vx_outb(chip, LOFREQ, clock & 0xff);
218 } else {
219 vx_outl(chip, HIFREQ, (clock >> 8) & 0x0f);
220 vx_outl(chip, LOFREQ, clock & 0xff);
222 spin_unlock_irqrestore(&chip->lock, flags);
227 * set the iec958 status bits
228 * @bits: 32-bit status bits
230 void vx_set_iec958_status(struct vx_core *chip, unsigned int bits)
232 int i;
234 if (chip->chip_status & VX_STAT_IS_STALE)
235 return;
237 for (i = 0; i < 32; i++)
238 vx_write_one_cbit(chip, i, bits & (1 << i));
243 * vx_set_clock - change the clock and audio source if necessary
245 int vx_set_clock(struct vx_core *chip, unsigned int freq)
247 int src_changed = 0;
249 if (chip->chip_status & VX_STAT_IS_STALE)
250 return 0;
252 /* change the audio source if possible */
253 vx_sync_audio_source(chip);
255 if (chip->clock_mode == VX_CLOCK_MODE_EXTERNAL ||
256 (chip->clock_mode == VX_CLOCK_MODE_AUTO &&
257 chip->audio_source == VX_AUDIO_SRC_DIGITAL)) {
258 if (chip->clock_source != UER_SYNC) {
259 vx_change_clock_source(chip, UER_SYNC);
260 mdelay(6);
261 src_changed = 1;
263 } else if (chip->clock_mode == VX_CLOCK_MODE_INTERNAL ||
264 (chip->clock_mode == VX_CLOCK_MODE_AUTO &&
265 chip->audio_source != VX_AUDIO_SRC_DIGITAL)) {
266 if (chip->clock_source != INTERNAL_QUARTZ) {
267 vx_change_clock_source(chip, INTERNAL_QUARTZ);
268 src_changed = 1;
270 if (chip->freq == freq)
271 return 0;
272 vx_set_internal_clock(chip, freq);
273 if (src_changed)
274 vx_modify_board_inputs(chip);
276 if (chip->freq == freq)
277 return 0;
278 chip->freq = freq;
279 vx_modify_board_clock(chip, 1);
280 return 0;
285 * vx_change_frequency - called from interrupt handler
287 int vx_change_frequency(struct vx_core *chip)
289 int freq;
291 if (chip->chip_status & VX_STAT_IS_STALE)
292 return 0;
294 if (chip->clock_source == INTERNAL_QUARTZ)
295 return 0;
297 * Read the real UER board frequency
299 freq = vx_read_uer_status(chip, &chip->uer_detected);
300 if (freq < 0)
301 return freq;
303 * The frequency computed by the DSP is good and
304 * is different from the previous computed.
306 if (freq == 48000 || freq == 44100 || freq == 32000)
307 chip->freq_detected = freq;
309 return 0;