xref: /openbmc/linux/sound/pci/emu10k1/io.c (revision 7559e757)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4  *                   Creative Labs, Inc.
5  *  Routines for control of EMU10K1 chips
6  *
7  *  BUGS:
8  *    --
9  *
10  *  TODO:
11  *    --
12  */
13 
14 #include <linux/time.h>
15 #include <sound/core.h>
16 #include <sound/emu10k1.h>
17 #include <linux/delay.h>
18 #include <linux/export.h>
19 #include "p17v.h"
20 
21 static inline bool check_ptr_reg(struct snd_emu10k1 *emu, unsigned int reg)
22 {
23 	if (snd_BUG_ON(!emu))
24 		return false;
25 	if (snd_BUG_ON(reg & (emu->audigy ? (0xffff0000 & ~A_PTR_ADDRESS_MASK)
26 					  : (0xffff0000 & ~PTR_ADDRESS_MASK))))
27 		return false;
28 	if (snd_BUG_ON(reg & 0x0000ffff & ~PTR_CHANNELNUM_MASK))
29 		return false;
30 	return true;
31 }
32 
33 unsigned int snd_emu10k1_ptr_read(struct snd_emu10k1 * emu, unsigned int reg, unsigned int chn)
34 {
35 	unsigned long flags;
36 	unsigned int regptr, val;
37 	unsigned int mask;
38 
39 	regptr = (reg << 16) | chn;
40 	if (!check_ptr_reg(emu, regptr))
41 		return 0;
42 
43 	spin_lock_irqsave(&emu->emu_lock, flags);
44 	outl(regptr, emu->port + PTR);
45 	val = inl(emu->port + DATA);
46 	spin_unlock_irqrestore(&emu->emu_lock, flags);
47 
48 	if (reg & 0xff000000) {
49 		unsigned char size, offset;
50 
51 		size = (reg >> 24) & 0x3f;
52 		offset = (reg >> 16) & 0x1f;
53 		mask = (1 << size) - 1;
54 
55 		return (val >> offset) & mask;
56 	} else {
57 		return val;
58 	}
59 }
60 
61 EXPORT_SYMBOL(snd_emu10k1_ptr_read);
62 
63 void snd_emu10k1_ptr_write(struct snd_emu10k1 *emu, unsigned int reg, unsigned int chn, unsigned int data)
64 {
65 	unsigned int regptr;
66 	unsigned long flags;
67 	unsigned int mask;
68 
69 	regptr = (reg << 16) | chn;
70 	if (!check_ptr_reg(emu, regptr))
71 		return;
72 
73 	if (reg & 0xff000000) {
74 		unsigned char size, offset;
75 
76 		size = (reg >> 24) & 0x3f;
77 		offset = (reg >> 16) & 0x1f;
78 		mask = (1 << size) - 1;
79 		if (snd_BUG_ON(data & ~mask))
80 			return;
81 		mask <<= offset;
82 		data <<= offset;
83 
84 		spin_lock_irqsave(&emu->emu_lock, flags);
85 		outl(regptr, emu->port + PTR);
86 		data |= inl(emu->port + DATA) & ~mask;
87 	} else {
88 		spin_lock_irqsave(&emu->emu_lock, flags);
89 		outl(regptr, emu->port + PTR);
90 	}
91 	outl(data, emu->port + DATA);
92 	spin_unlock_irqrestore(&emu->emu_lock, flags);
93 }
94 
95 EXPORT_SYMBOL(snd_emu10k1_ptr_write);
96 
97 void snd_emu10k1_ptr_write_multiple(struct snd_emu10k1 *emu, unsigned int chn, ...)
98 {
99 	va_list va;
100 	u32 addr_mask;
101 	unsigned long flags;
102 
103 	if (snd_BUG_ON(!emu))
104 		return;
105 	if (snd_BUG_ON(chn & ~PTR_CHANNELNUM_MASK))
106 		return;
107 	addr_mask = ~((emu->audigy ? A_PTR_ADDRESS_MASK : PTR_ADDRESS_MASK) >> 16);
108 
109 	va_start(va, chn);
110 	spin_lock_irqsave(&emu->emu_lock, flags);
111 	for (;;) {
112 		u32 data;
113 		u32 reg = va_arg(va, u32);
114 		if (reg == REGLIST_END)
115 			break;
116 		data = va_arg(va, u32);
117 		if (snd_BUG_ON(reg & addr_mask))  // Only raw registers supported here
118 			continue;
119 		outl((reg << 16) | chn, emu->port + PTR);
120 		outl(data, emu->port + DATA);
121 	}
122 	spin_unlock_irqrestore(&emu->emu_lock, flags);
123 	va_end(va);
124 }
125 
126 EXPORT_SYMBOL(snd_emu10k1_ptr_write_multiple);
127 
128 unsigned int snd_emu10k1_ptr20_read(struct snd_emu10k1 * emu,
129 					  unsigned int reg,
130 					  unsigned int chn)
131 {
132 	unsigned long flags;
133 	unsigned int regptr, val;
134 
135 	regptr = (reg << 16) | chn;
136 
137 	spin_lock_irqsave(&emu->emu_lock, flags);
138 	outl(regptr, emu->port + PTR2);
139 	val = inl(emu->port + DATA2);
140 	spin_unlock_irqrestore(&emu->emu_lock, flags);
141 	return val;
142 }
143 
144 void snd_emu10k1_ptr20_write(struct snd_emu10k1 *emu,
145 				   unsigned int reg,
146 				   unsigned int chn,
147 				   unsigned int data)
148 {
149 	unsigned int regptr;
150 	unsigned long flags;
151 
152 	regptr = (reg << 16) | chn;
153 
154 	spin_lock_irqsave(&emu->emu_lock, flags);
155 	outl(regptr, emu->port + PTR2);
156 	outl(data, emu->port + DATA2);
157 	spin_unlock_irqrestore(&emu->emu_lock, flags);
158 }
159 
160 int snd_emu10k1_spi_write(struct snd_emu10k1 * emu,
161 				   unsigned int data)
162 {
163 	unsigned int reset, set;
164 	unsigned int reg, tmp;
165 	int n, result;
166 	int err = 0;
167 
168 	/* This function is not re-entrant, so protect against it. */
169 	spin_lock(&emu->spi_lock);
170 	if (emu->card_capabilities->ca0108_chip)
171 		reg = P17V_SPI;
172 	else {
173 		/* For other chip types the SPI register
174 		 * is currently unknown. */
175 		err = 1;
176 		goto spi_write_exit;
177 	}
178 	if (data > 0xffff) {
179 		/* Only 16bit values allowed */
180 		err = 1;
181 		goto spi_write_exit;
182 	}
183 
184 	tmp = snd_emu10k1_ptr20_read(emu, reg, 0);
185 	reset = (tmp & ~0x3ffff) | 0x20000; /* Set xxx20000 */
186 	set = reset | 0x10000; /* Set xxx1xxxx */
187 	snd_emu10k1_ptr20_write(emu, reg, 0, reset | data);
188 	tmp = snd_emu10k1_ptr20_read(emu, reg, 0); /* write post */
189 	snd_emu10k1_ptr20_write(emu, reg, 0, set | data);
190 	result = 1;
191 	/* Wait for status bit to return to 0 */
192 	for (n = 0; n < 100; n++) {
193 		udelay(10);
194 		tmp = snd_emu10k1_ptr20_read(emu, reg, 0);
195 		if (!(tmp & 0x10000)) {
196 			result = 0;
197 			break;
198 		}
199 	}
200 	if (result) {
201 		/* Timed out */
202 		err = 1;
203 		goto spi_write_exit;
204 	}
205 	snd_emu10k1_ptr20_write(emu, reg, 0, reset | data);
206 	tmp = snd_emu10k1_ptr20_read(emu, reg, 0); /* Write post */
207 	err = 0;
208 spi_write_exit:
209 	spin_unlock(&emu->spi_lock);
210 	return err;
211 }
212 
213 /* The ADC does not support i2c read, so only write is implemented */
214 int snd_emu10k1_i2c_write(struct snd_emu10k1 *emu,
215 				u32 reg,
216 				u32 value)
217 {
218 	u32 tmp;
219 	int timeout = 0;
220 	int status;
221 	int retry;
222 	int err = 0;
223 
224 	if ((reg > 0x7f) || (value > 0x1ff)) {
225 		dev_err(emu->card->dev, "i2c_write: invalid values.\n");
226 		return -EINVAL;
227 	}
228 
229 	/* This function is not re-entrant, so protect against it. */
230 	spin_lock(&emu->i2c_lock);
231 
232 	tmp = reg << 25 | value << 16;
233 
234 	/* This controls the I2C connected to the WM8775 ADC Codec */
235 	snd_emu10k1_ptr20_write(emu, P17V_I2C_1, 0, tmp);
236 	tmp = snd_emu10k1_ptr20_read(emu, P17V_I2C_1, 0); /* write post */
237 
238 	for (retry = 0; retry < 10; retry++) {
239 		/* Send the data to i2c */
240 		tmp = 0;
241 		tmp = tmp | (I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD);
242 		snd_emu10k1_ptr20_write(emu, P17V_I2C_ADDR, 0, tmp);
243 
244 		/* Wait till the transaction ends */
245 		while (1) {
246 			mdelay(1);
247 			status = snd_emu10k1_ptr20_read(emu, P17V_I2C_ADDR, 0);
248 			timeout++;
249 			if ((status & I2C_A_ADC_START) == 0)
250 				break;
251 
252 			if (timeout > 1000) {
253 				dev_warn(emu->card->dev,
254 					   "emu10k1:I2C:timeout status=0x%x\n",
255 					   status);
256 				break;
257 			}
258 		}
259 		//Read back and see if the transaction is successful
260 		if ((status & I2C_A_ADC_ABORT) == 0)
261 			break;
262 	}
263 
264 	if (retry == 10) {
265 		dev_err(emu->card->dev, "Writing to ADC failed!\n");
266 		dev_err(emu->card->dev, "status=0x%x, reg=%d, value=%d\n",
267 			status, reg, value);
268 		/* dump_stack(); */
269 		err = -EINVAL;
270 	}
271 
272 	spin_unlock(&emu->i2c_lock);
273 	return err;
274 }
275 
276 static void snd_emu1010_fpga_write_locked(struct snd_emu10k1 *emu, u32 reg, u32 value)
277 {
278 	if (snd_BUG_ON(reg > 0x3f))
279 		return;
280 	reg += 0x40; /* 0x40 upwards are registers. */
281 	if (snd_BUG_ON(value > 0x3f)) /* 0 to 0x3f are values */
282 		return;
283 	outw(reg, emu->port + A_GPIO);
284 	udelay(10);
285 	outw(reg | 0x80, emu->port + A_GPIO);  /* High bit clocks the value into the fpga. */
286 	udelay(10);
287 	outw(value, emu->port + A_GPIO);
288 	udelay(10);
289 	outw(value | 0x80 , emu->port + A_GPIO);  /* High bit clocks the value into the fpga. */
290 }
291 
292 void snd_emu1010_fpga_write(struct snd_emu10k1 *emu, u32 reg, u32 value)
293 {
294 	unsigned long flags;
295 
296 	spin_lock_irqsave(&emu->emu_lock, flags);
297 	snd_emu1010_fpga_write_locked(emu, reg, value);
298 	spin_unlock_irqrestore(&emu->emu_lock, flags);
299 }
300 
301 static void snd_emu1010_fpga_read_locked(struct snd_emu10k1 *emu, u32 reg, u32 *value)
302 {
303 	// The higest input pin is used as the designated interrupt trigger,
304 	// so it needs to be masked out.
305 	u32 mask = emu->card_capabilities->ca0108_chip ? 0x1f : 0x7f;
306 	if (snd_BUG_ON(reg > 0x3f))
307 		return;
308 	reg += 0x40; /* 0x40 upwards are registers. */
309 	outw(reg, emu->port + A_GPIO);
310 	udelay(10);
311 	outw(reg | 0x80, emu->port + A_GPIO);  /* High bit clocks the value into the fpga. */
312 	udelay(10);
313 	*value = ((inw(emu->port + A_GPIO) >> 8) & mask);
314 }
315 
316 void snd_emu1010_fpga_read(struct snd_emu10k1 *emu, u32 reg, u32 *value)
317 {
318 	unsigned long flags;
319 
320 	spin_lock_irqsave(&emu->emu_lock, flags);
321 	snd_emu1010_fpga_read_locked(emu, reg, value);
322 	spin_unlock_irqrestore(&emu->emu_lock, flags);
323 }
324 
325 /* Each Destination has one and only one Source,
326  * but one Source can feed any number of Destinations simultaneously.
327  */
328 void snd_emu1010_fpga_link_dst_src_write(struct snd_emu10k1 *emu, u32 dst, u32 src)
329 {
330 	unsigned long flags;
331 
332 	if (snd_BUG_ON(dst & ~0x71f))
333 		return;
334 	if (snd_BUG_ON(src & ~0x71f))
335 		return;
336 	spin_lock_irqsave(&emu->emu_lock, flags);
337 	snd_emu1010_fpga_write_locked(emu, EMU_HANA_DESTHI, dst >> 8);
338 	snd_emu1010_fpga_write_locked(emu, EMU_HANA_DESTLO, dst & 0x1f);
339 	snd_emu1010_fpga_write_locked(emu, EMU_HANA_SRCHI, src >> 8);
340 	snd_emu1010_fpga_write_locked(emu, EMU_HANA_SRCLO, src & 0x1f);
341 	spin_unlock_irqrestore(&emu->emu_lock, flags);
342 }
343 
344 u32 snd_emu1010_fpga_link_dst_src_read(struct snd_emu10k1 *emu, u32 dst)
345 {
346 	unsigned long flags;
347 	u32 hi, lo;
348 
349 	if (snd_BUG_ON(dst & ~0x71f))
350 		return 0;
351 	spin_lock_irqsave(&emu->emu_lock, flags);
352 	snd_emu1010_fpga_write_locked(emu, EMU_HANA_DESTHI, dst >> 8);
353 	snd_emu1010_fpga_write_locked(emu, EMU_HANA_DESTLO, dst & 0x1f);
354 	snd_emu1010_fpga_read_locked(emu, EMU_HANA_SRCHI, &hi);
355 	snd_emu1010_fpga_read_locked(emu, EMU_HANA_SRCLO, &lo);
356 	spin_unlock_irqrestore(&emu->emu_lock, flags);
357 	return (hi << 8) | lo;
358 }
359 
360 int snd_emu1010_get_raw_rate(struct snd_emu10k1 *emu, u8 src)
361 {
362 	u32 reg_lo, reg_hi, value, value2;
363 
364 	switch (src) {
365 	case EMU_HANA_WCLOCK_HANA_SPDIF_IN:
366 		snd_emu1010_fpga_read(emu, EMU_HANA_SPDIF_MODE, &value);
367 		if (value & EMU_HANA_SPDIF_MODE_RX_INVALID)
368 			return 0;
369 		reg_lo = EMU_HANA_WC_SPDIF_LO;
370 		reg_hi = EMU_HANA_WC_SPDIF_HI;
371 		break;
372 	case EMU_HANA_WCLOCK_HANA_ADAT_IN:
373 		reg_lo = EMU_HANA_WC_ADAT_LO;
374 		reg_hi = EMU_HANA_WC_ADAT_HI;
375 		break;
376 	case EMU_HANA_WCLOCK_SYNC_BNC:
377 		reg_lo = EMU_HANA_WC_BNC_LO;
378 		reg_hi = EMU_HANA_WC_BNC_HI;
379 		break;
380 	case EMU_HANA_WCLOCK_2ND_HANA:
381 		reg_lo = EMU_HANA2_WC_SPDIF_LO;
382 		reg_hi = EMU_HANA2_WC_SPDIF_HI;
383 		break;
384 	default:
385 		return 0;
386 	}
387 	snd_emu1010_fpga_read(emu, reg_hi, &value);
388 	snd_emu1010_fpga_read(emu, reg_lo, &value2);
389 	// FIXME: The /4 is valid for 0404b, but contradicts all other info.
390 	return 0x1770000 / 4 / (((value << 5) | value2) + 1);
391 }
392 
393 void snd_emu1010_update_clock(struct snd_emu10k1 *emu)
394 {
395 	int clock;
396 	u32 leds;
397 
398 	switch (emu->emu1010.wclock) {
399 	case EMU_HANA_WCLOCK_INT_44_1K | EMU_HANA_WCLOCK_1X:
400 		clock = 44100;
401 		leds = EMU_HANA_DOCK_LEDS_2_44K;
402 		break;
403 	case EMU_HANA_WCLOCK_INT_48K | EMU_HANA_WCLOCK_1X:
404 		clock = 48000;
405 		leds = EMU_HANA_DOCK_LEDS_2_48K;
406 		break;
407 	default:
408 		clock = snd_emu1010_get_raw_rate(
409 				emu, emu->emu1010.wclock & EMU_HANA_WCLOCK_SRC_MASK);
410 		// The raw rate reading is rather coarse (it cannot accurately
411 		// represent 44.1 kHz) and fluctuates slightly. Luckily, the
412 		// clock comes from digital inputs, which use standardized rates.
413 		// So we round to the closest standard rate and ignore discrepancies.
414 		if (clock < 46000) {
415 			clock = 44100;
416 			leds = EMU_HANA_DOCK_LEDS_2_EXT | EMU_HANA_DOCK_LEDS_2_44K;
417 		} else {
418 			clock = 48000;
419 			leds = EMU_HANA_DOCK_LEDS_2_EXT | EMU_HANA_DOCK_LEDS_2_48K;
420 		}
421 		break;
422 	}
423 	emu->emu1010.word_clock = clock;
424 
425 	// FIXME: this should probably represent the AND of all currently
426 	// used sources' lock status. But we don't know how to get that ...
427 	leds |= EMU_HANA_DOCK_LEDS_2_LOCK;
428 
429 	snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_LEDS_2, leds);
430 }
431 
432 void snd_emu10k1_intr_enable(struct snd_emu10k1 *emu, unsigned int intrenb)
433 {
434 	unsigned long flags;
435 	unsigned int enable;
436 
437 	spin_lock_irqsave(&emu->emu_lock, flags);
438 	enable = inl(emu->port + INTE) | intrenb;
439 	outl(enable, emu->port + INTE);
440 	spin_unlock_irqrestore(&emu->emu_lock, flags);
441 }
442 
443 void snd_emu10k1_intr_disable(struct snd_emu10k1 *emu, unsigned int intrenb)
444 {
445 	unsigned long flags;
446 	unsigned int enable;
447 
448 	spin_lock_irqsave(&emu->emu_lock, flags);
449 	enable = inl(emu->port + INTE) & ~intrenb;
450 	outl(enable, emu->port + INTE);
451 	spin_unlock_irqrestore(&emu->emu_lock, flags);
452 }
453 
454 void snd_emu10k1_voice_intr_enable(struct snd_emu10k1 *emu, unsigned int voicenum)
455 {
456 	unsigned long flags;
457 	unsigned int val;
458 
459 	spin_lock_irqsave(&emu->emu_lock, flags);
460 	if (voicenum >= 32) {
461 		outl(CLIEH << 16, emu->port + PTR);
462 		val = inl(emu->port + DATA);
463 		val |= 1 << (voicenum - 32);
464 	} else {
465 		outl(CLIEL << 16, emu->port + PTR);
466 		val = inl(emu->port + DATA);
467 		val |= 1 << voicenum;
468 	}
469 	outl(val, emu->port + DATA);
470 	spin_unlock_irqrestore(&emu->emu_lock, flags);
471 }
472 
473 void snd_emu10k1_voice_intr_disable(struct snd_emu10k1 *emu, unsigned int voicenum)
474 {
475 	unsigned long flags;
476 	unsigned int val;
477 
478 	spin_lock_irqsave(&emu->emu_lock, flags);
479 	if (voicenum >= 32) {
480 		outl(CLIEH << 16, emu->port + PTR);
481 		val = inl(emu->port + DATA);
482 		val &= ~(1 << (voicenum - 32));
483 	} else {
484 		outl(CLIEL << 16, emu->port + PTR);
485 		val = inl(emu->port + DATA);
486 		val &= ~(1 << voicenum);
487 	}
488 	outl(val, emu->port + DATA);
489 	spin_unlock_irqrestore(&emu->emu_lock, flags);
490 }
491 
492 void snd_emu10k1_voice_intr_ack(struct snd_emu10k1 *emu, unsigned int voicenum)
493 {
494 	unsigned long flags;
495 
496 	spin_lock_irqsave(&emu->emu_lock, flags);
497 	if (voicenum >= 32) {
498 		outl(CLIPH << 16, emu->port + PTR);
499 		voicenum = 1 << (voicenum - 32);
500 	} else {
501 		outl(CLIPL << 16, emu->port + PTR);
502 		voicenum = 1 << voicenum;
503 	}
504 	outl(voicenum, emu->port + DATA);
505 	spin_unlock_irqrestore(&emu->emu_lock, flags);
506 }
507 
508 void snd_emu10k1_voice_half_loop_intr_enable(struct snd_emu10k1 *emu, unsigned int voicenum)
509 {
510 	unsigned long flags;
511 	unsigned int val;
512 
513 	spin_lock_irqsave(&emu->emu_lock, flags);
514 	if (voicenum >= 32) {
515 		outl(HLIEH << 16, emu->port + PTR);
516 		val = inl(emu->port + DATA);
517 		val |= 1 << (voicenum - 32);
518 	} else {
519 		outl(HLIEL << 16, emu->port + PTR);
520 		val = inl(emu->port + DATA);
521 		val |= 1 << voicenum;
522 	}
523 	outl(val, emu->port + DATA);
524 	spin_unlock_irqrestore(&emu->emu_lock, flags);
525 }
526 
527 void snd_emu10k1_voice_half_loop_intr_disable(struct snd_emu10k1 *emu, unsigned int voicenum)
528 {
529 	unsigned long flags;
530 	unsigned int val;
531 
532 	spin_lock_irqsave(&emu->emu_lock, flags);
533 	if (voicenum >= 32) {
534 		outl(HLIEH << 16, emu->port + PTR);
535 		val = inl(emu->port + DATA);
536 		val &= ~(1 << (voicenum - 32));
537 	} else {
538 		outl(HLIEL << 16, emu->port + PTR);
539 		val = inl(emu->port + DATA);
540 		val &= ~(1 << voicenum);
541 	}
542 	outl(val, emu->port + DATA);
543 	spin_unlock_irqrestore(&emu->emu_lock, flags);
544 }
545 
546 void snd_emu10k1_voice_half_loop_intr_ack(struct snd_emu10k1 *emu, unsigned int voicenum)
547 {
548 	unsigned long flags;
549 
550 	spin_lock_irqsave(&emu->emu_lock, flags);
551 	if (voicenum >= 32) {
552 		outl(HLIPH << 16, emu->port + PTR);
553 		voicenum = 1 << (voicenum - 32);
554 	} else {
555 		outl(HLIPL << 16, emu->port + PTR);
556 		voicenum = 1 << voicenum;
557 	}
558 	outl(voicenum, emu->port + DATA);
559 	spin_unlock_irqrestore(&emu->emu_lock, flags);
560 }
561 
562 #if 0
563 void snd_emu10k1_voice_set_loop_stop(struct snd_emu10k1 *emu, unsigned int voicenum)
564 {
565 	unsigned long flags;
566 	unsigned int sol;
567 
568 	spin_lock_irqsave(&emu->emu_lock, flags);
569 	if (voicenum >= 32) {
570 		outl(SOLEH << 16, emu->port + PTR);
571 		sol = inl(emu->port + DATA);
572 		sol |= 1 << (voicenum - 32);
573 	} else {
574 		outl(SOLEL << 16, emu->port + PTR);
575 		sol = inl(emu->port + DATA);
576 		sol |= 1 << voicenum;
577 	}
578 	outl(sol, emu->port + DATA);
579 	spin_unlock_irqrestore(&emu->emu_lock, flags);
580 }
581 
582 void snd_emu10k1_voice_clear_loop_stop(struct snd_emu10k1 *emu, unsigned int voicenum)
583 {
584 	unsigned long flags;
585 	unsigned int sol;
586 
587 	spin_lock_irqsave(&emu->emu_lock, flags);
588 	if (voicenum >= 32) {
589 		outl(SOLEH << 16, emu->port + PTR);
590 		sol = inl(emu->port + DATA);
591 		sol &= ~(1 << (voicenum - 32));
592 	} else {
593 		outl(SOLEL << 16, emu->port + PTR);
594 		sol = inl(emu->port + DATA);
595 		sol &= ~(1 << voicenum);
596 	}
597 	outl(sol, emu->port + DATA);
598 	spin_unlock_irqrestore(&emu->emu_lock, flags);
599 }
600 #endif
601 
602 void snd_emu10k1_voice_set_loop_stop_multiple(struct snd_emu10k1 *emu, u64 voices)
603 {
604 	unsigned long flags;
605 
606 	spin_lock_irqsave(&emu->emu_lock, flags);
607 	outl(SOLEL << 16, emu->port + PTR);
608 	outl(inl(emu->port + DATA) | (u32)voices, emu->port + DATA);
609 	outl(SOLEH << 16, emu->port + PTR);
610 	outl(inl(emu->port + DATA) | (u32)(voices >> 32), emu->port + DATA);
611 	spin_unlock_irqrestore(&emu->emu_lock, flags);
612 }
613 
614 void snd_emu10k1_voice_clear_loop_stop_multiple(struct snd_emu10k1 *emu, u64 voices)
615 {
616 	unsigned long flags;
617 
618 	spin_lock_irqsave(&emu->emu_lock, flags);
619 	outl(SOLEL << 16, emu->port + PTR);
620 	outl(inl(emu->port + DATA) & (u32)~voices, emu->port + DATA);
621 	outl(SOLEH << 16, emu->port + PTR);
622 	outl(inl(emu->port + DATA) & (u32)(~voices >> 32), emu->port + DATA);
623 	spin_unlock_irqrestore(&emu->emu_lock, flags);
624 }
625 
626 int snd_emu10k1_voice_clear_loop_stop_multiple_atomic(struct snd_emu10k1 *emu, u64 voices)
627 {
628 	unsigned long flags;
629 	u32 soll, solh;
630 	int ret = -EIO;
631 
632 	spin_lock_irqsave(&emu->emu_lock, flags);
633 
634 	outl(SOLEL << 16, emu->port + PTR);
635 	soll = inl(emu->port + DATA);
636 	outl(SOLEH << 16, emu->port + PTR);
637 	solh = inl(emu->port + DATA);
638 
639 	soll &= (u32)~voices;
640 	solh &= (u32)(~voices >> 32);
641 
642 	for (int tries = 0; tries < 1000; tries++) {
643 		const u32 quart = 1U << (REG_SIZE(WC_CURRENTCHANNEL) - 2);
644 		// First we wait for the third quarter of the sample cycle ...
645 		u32 wc = inl(emu->port + WC);
646 		u32 cc = REG_VAL_GET(WC_CURRENTCHANNEL, wc);
647 		if (cc >= quart * 2 && cc < quart * 3) {
648 			// ... and release the low voices, while the high ones are serviced.
649 			outl(SOLEL << 16, emu->port + PTR);
650 			outl(soll, emu->port + DATA);
651 			// Then we wait for the first quarter of the next sample cycle ...
652 			for (; tries < 1000; tries++) {
653 				cc = REG_VAL_GET(WC_CURRENTCHANNEL, inl(emu->port + WC));
654 				if (cc < quart)
655 					goto good;
656 				// We will block for 10+ us with interrupts disabled. This is
657 				// not nice at all, but necessary for reasonable reliability.
658 				udelay(1);
659 			}
660 			break;
661 		good:
662 			// ... and release the high voices, while the low ones are serviced.
663 			outl(SOLEH << 16, emu->port + PTR);
664 			outl(solh, emu->port + DATA);
665 			// Finally we verify that nothing interfered in fact.
666 			if (REG_VAL_GET(WC_SAMPLECOUNTER, inl(emu->port + WC)) ==
667 			    ((REG_VAL_GET(WC_SAMPLECOUNTER, wc) + 1) & REG_MASK0(WC_SAMPLECOUNTER))) {
668 				ret = 0;
669 			} else {
670 				ret = -EAGAIN;
671 			}
672 			break;
673 		}
674 		// Don't block for too long
675 		spin_unlock_irqrestore(&emu->emu_lock, flags);
676 		udelay(1);
677 		spin_lock_irqsave(&emu->emu_lock, flags);
678 	}
679 
680 	spin_unlock_irqrestore(&emu->emu_lock, flags);
681 	return ret;
682 }
683 
684 void snd_emu10k1_wait(struct snd_emu10k1 *emu, unsigned int wait)
685 {
686 	volatile unsigned count;
687 	unsigned int newtime = 0, curtime;
688 
689 	curtime = inl(emu->port + WC) >> 6;
690 	while (wait-- > 0) {
691 		count = 0;
692 		while (count++ < 16384) {
693 			newtime = inl(emu->port + WC) >> 6;
694 			if (newtime != curtime)
695 				break;
696 		}
697 		if (count > 16384)
698 			break;
699 		curtime = newtime;
700 	}
701 }
702 
703 unsigned short snd_emu10k1_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
704 {
705 	struct snd_emu10k1 *emu = ac97->private_data;
706 	unsigned long flags;
707 	unsigned short val;
708 
709 	spin_lock_irqsave(&emu->emu_lock, flags);
710 	outb(reg, emu->port + AC97ADDRESS);
711 	val = inw(emu->port + AC97DATA);
712 	spin_unlock_irqrestore(&emu->emu_lock, flags);
713 	return val;
714 }
715 
716 void snd_emu10k1_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short data)
717 {
718 	struct snd_emu10k1 *emu = ac97->private_data;
719 	unsigned long flags;
720 
721 	spin_lock_irqsave(&emu->emu_lock, flags);
722 	outb(reg, emu->port + AC97ADDRESS);
723 	outw(data, emu->port + AC97DATA);
724 	spin_unlock_irqrestore(&emu->emu_lock, flags);
725 }
726