File Index
Files and directories
Sign in
FILE

sis7019.c

File preview with syntax highlighting.

linux-6.19.14/sound/pci/sis7019.c C
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *  Driver for SiS7019 Audio Accelerator
   4 *
   5 *  Copyright (C) 2004-2007, David Dillow
   6 *  Written by David Dillow <dave@thedillows.org>
   7 *  Inspired by the Trident 4D-WaveDX/NX driver.
   8 *
   9 *  All rights reserved.
  10 */
  11
  12#include <linux/init.h>
  13#include <linux/pci.h>
  14#include <linux/time.h>
  15#include <linux/slab.h>
  16#include <linux/module.h>
  17#include <linux/interrupt.h>
  18#include <linux/delay.h>
  19#include <sound/core.h>
  20#include <sound/ac97_codec.h>
  21#include <sound/initval.h>
  22#include "sis7019.h"
  23
  24MODULE_AUTHOR("David Dillow <dave@thedillows.org>");
  25MODULE_DESCRIPTION("SiS7019");
  26MODULE_LICENSE("GPL");
  27
  28static int index = SNDRV_DEFAULT_IDX1;	/* Index 0-MAX */
  29static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
  30static bool enable = 1;
  31static int codecs = 1;
  32
  33module_param(index, int, 0444);
  34MODULE_PARM_DESC(index, "Index value for SiS7019 Audio Accelerator.");
  35module_param(id, charp, 0444);
  36MODULE_PARM_DESC(id, "ID string for SiS7019 Audio Accelerator.");
  37module_param(enable, bool, 0444);
  38MODULE_PARM_DESC(enable, "Enable SiS7019 Audio Accelerator.");
  39module_param(codecs, int, 0444);
  40MODULE_PARM_DESC(codecs, "Set bit to indicate that codec number is expected to be present (default 1)");
  41
  42static const struct pci_device_id snd_sis7019_ids[] = {
  43	{ PCI_DEVICE(PCI_VENDOR_ID_SI, 0x7019) },
  44	{ 0, }
  45};
  46
  47MODULE_DEVICE_TABLE(pci, snd_sis7019_ids);
  48
  49/* There are three timing modes for the voices.
  50 *
  51 * For both playback and capture, when the buffer is one or two periods long,
  52 * we use the hardware's built-in Mid-Loop Interrupt and End-Loop Interrupt
  53 * to let us know when the periods have ended.
  54 *
  55 * When performing playback with more than two periods per buffer, we set
  56 * the "Stop Sample Offset" and tell the hardware to interrupt us when we
  57 * reach it. We then update the offset and continue on until we are
  58 * interrupted for the next period.
  59 *
  60 * Capture channels do not have a SSO, so we allocate a playback channel to
  61 * use as a timer for the capture periods. We use the SSO on the playback
  62 * channel to clock out virtual periods, and adjust the virtual period length
  63 * to maintain synchronization. This algorithm came from the Trident driver.
  64 *
  65 * FIXME: It'd be nice to make use of some of the synth features in the
  66 * hardware, but a woeful lack of documentation is a significant roadblock.
  67 */
  68struct voice {
  69	u16 flags;
  70#define 	VOICE_IN_USE		1
  71#define 	VOICE_CAPTURE		2
  72#define 	VOICE_SSO_TIMING	4
  73#define 	VOICE_SYNC_TIMING	8
  74	u16 sync_cso;
  75	u16 period_size;
  76	u16 buffer_size;
  77	u16 sync_period_size;
  78	u16 sync_buffer_size;
  79	u32 sso;
  80	u32 vperiod;
  81	struct snd_pcm_substream *substream;
  82	struct voice *timing;
  83	void __iomem *ctrl_base;
  84	void __iomem *wave_base;
  85	void __iomem *sync_base;
  86	int num;
  87};
  88
  89/* We need four pages to store our wave parameters during a suspend. If
  90 * we're not doing power management, we still need to allocate a page
  91 * for the silence buffer.
  92 */
  93#define SIS_SUSPEND_PAGES	4
  94
  95struct sis7019 {
  96	unsigned long ioport;
  97	void __iomem *ioaddr;
  98	int irq;
  99	int codecs_present;
 100
 101	struct pci_dev *pci;
 102	struct snd_pcm *pcm;
 103	struct snd_card *card;
 104	struct snd_ac97 *ac97[3];
 105
 106	/* Protect against more than one thread hitting the AC97
 107	 * registers (in a more polite manner than pounding the hardware
 108	 * semaphore)
 109	 */
 110	struct mutex ac97_mutex;
 111
 112	/* voice_lock protects allocation/freeing of the voice descriptions
 113	 */
 114	spinlock_t voice_lock;
 115
 116	struct voice voices[64];
 117	struct voice capture_voice;
 118
 119	/* Allocate pages to store the internal wave state during
 120	 * suspends. When we're operating, this can be used as a silence
 121	 * buffer for a timing channel.
 122	 */
 123	void *suspend_state[SIS_SUSPEND_PAGES];
 124
 125	int silence_users;
 126	dma_addr_t silence_dma_addr;
 127};
 128
 129/* These values are also used by the module param 'codecs' to indicate
 130 * which codecs should be present.
 131 */
 132#define SIS_PRIMARY_CODEC_PRESENT	0x0001
 133#define SIS_SECONDARY_CODEC_PRESENT	0x0002
 134#define SIS_TERTIARY_CODEC_PRESENT	0x0004
 135
 136/* The HW offset parameters (Loop End, Stop Sample, End Sample) have a
 137 * documented range of 8-0xfff8 samples. Given that they are 0-based,
 138 * that places our period/buffer range at 9-0xfff9 samples. That makes the
 139 * max buffer size 0xfff9 samples * 2 channels * 2 bytes per sample, and
 140 * max samples / min samples gives us the max periods in a buffer.
 141 *
 142 * We'll add a constraint upon open that limits the period and buffer sample
 143 * size to values that are legal for the hardware.
 144 */
 145static const struct snd_pcm_hardware sis_playback_hw_info = {
 146	.info = (SNDRV_PCM_INFO_MMAP |
 147		 SNDRV_PCM_INFO_MMAP_VALID |
 148		 SNDRV_PCM_INFO_INTERLEAVED |
 149		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
 150		 SNDRV_PCM_INFO_SYNC_START |
 151		 SNDRV_PCM_INFO_RESUME),
 152	.formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
 153		    SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
 154	.rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS,
 155	.rate_min = 4000,
 156	.rate_max = 48000,
 157	.channels_min = 1,
 158	.channels_max = 2,
 159	.buffer_bytes_max = (0xfff9 * 4),
 160	.period_bytes_min = 9,
 161	.period_bytes_max = (0xfff9 * 4),
 162	.periods_min = 1,
 163	.periods_max = (0xfff9 / 9),
 164};
 165
 166static const struct snd_pcm_hardware sis_capture_hw_info = {
 167	.info = (SNDRV_PCM_INFO_MMAP |
 168		 SNDRV_PCM_INFO_MMAP_VALID |
 169		 SNDRV_PCM_INFO_INTERLEAVED |
 170		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
 171		 SNDRV_PCM_INFO_SYNC_START |
 172		 SNDRV_PCM_INFO_RESUME),
 173	.formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
 174		    SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
 175	.rates = SNDRV_PCM_RATE_48000,
 176	.rate_min = 4000,
 177	.rate_max = 48000,
 178	.channels_min = 1,
 179	.channels_max = 2,
 180	.buffer_bytes_max = (0xfff9 * 4),
 181	.period_bytes_min = 9,
 182	.period_bytes_max = (0xfff9 * 4),
 183	.periods_min = 1,
 184	.periods_max = (0xfff9 / 9),
 185};
 186
 187static void sis_update_sso(struct voice *voice, u16 period)
 188{
 189	void __iomem *base = voice->ctrl_base;
 190
 191	voice->sso += period;
 192	if (voice->sso >= voice->buffer_size)
 193		voice->sso -= voice->buffer_size;
 194
 195	/* Enforce the documented hardware minimum offset */
 196	if (voice->sso < 8)
 197		voice->sso = 8;
 198
 199	/* The SSO is in the upper 16 bits of the register. */
 200	writew(voice->sso & 0xffff, base + SIS_PLAY_DMA_SSO_ESO + 2);
 201}
 202
 203static void sis_update_voice(struct voice *voice)
 204{
 205	if (voice->flags & VOICE_SSO_TIMING) {
 206		sis_update_sso(voice, voice->period_size);
 207	} else if (voice->flags & VOICE_SYNC_TIMING) {
 208		int sync;
 209
 210		/* If we've not hit the end of the virtual period, update
 211		 * our records and keep going.
 212		 */
 213		if (voice->vperiod > voice->period_size) {
 214			voice->vperiod -= voice->period_size;
 215			if (voice->vperiod < voice->period_size)
 216				sis_update_sso(voice, voice->vperiod);
 217			else
 218				sis_update_sso(voice, voice->period_size);
 219			return;
 220		}
 221
 222		/* Calculate our relative offset between the target and
 223		 * the actual CSO value. Since we're operating in a loop,
 224		 * if the value is more than half way around, we can
 225		 * consider ourselves wrapped.
 226		 */
 227		sync = voice->sync_cso;
 228		sync -= readw(voice->sync_base + SIS_CAPTURE_DMA_FORMAT_CSO);
 229		if (sync > (voice->sync_buffer_size / 2))
 230			sync -= voice->sync_buffer_size;
 231
 232		/* If sync is positive, then we interrupted too early, and
 233		 * we'll need to come back in a few samples and try again.
 234		 * There's a minimum wait, as it takes some time for the DMA
 235		 * engine to startup, etc...
 236		 */
 237		if (sync > 0) {
 238			if (sync < 16)
 239				sync = 16;
 240			sis_update_sso(voice, sync);
 241			return;
 242		}
 243
 244		/* Ok, we interrupted right on time, or (hopefully) just
 245		 * a bit late. We'll adjst our next waiting period based
 246		 * on how close we got.
 247		 *
 248		 * We need to stay just behind the actual channel to ensure
 249		 * it really is past a period when we get our interrupt --
 250		 * otherwise we'll fall into the early code above and have
 251		 * a minimum wait time, which makes us quite late here,
 252		 * eating into the user's time to refresh the buffer, esp.
 253		 * if using small periods.
 254		 *
 255		 * If we're less than 9 samples behind, we're on target.
 256		 * Otherwise, shorten the next vperiod by the amount we've
 257		 * been delayed.
 258		 */
 259		if (sync > -9)
 260			voice->vperiod = voice->sync_period_size + 1;
 261		else
 262			voice->vperiod = voice->sync_period_size + sync + 10;
 263
 264		if (voice->vperiod < voice->buffer_size) {
 265			sis_update_sso(voice, voice->vperiod);
 266			voice->vperiod = 0;
 267		} else
 268			sis_update_sso(voice, voice->period_size);
 269
 270		sync = voice->sync_cso + voice->sync_period_size;
 271		if (sync >= voice->sync_buffer_size)
 272			sync -= voice->sync_buffer_size;
 273		voice->sync_cso = sync;
 274	}
 275
 276	snd_pcm_period_elapsed(voice->substream);
 277}
 278
 279static void sis_voice_irq(u32 status, struct voice *voice)
 280{
 281	int bit;
 282
 283	while (status) {
 284		bit = __ffs(status);
 285		status >>= bit + 1;
 286		voice += bit;
 287		sis_update_voice(voice);
 288		voice++;
 289	}
 290}
 291
 292static irqreturn_t sis_interrupt(int irq, void *dev)
 293{
 294	struct sis7019 *sis = dev;
 295	unsigned long io = sis->ioport;
 296	struct voice *voice;
 297	u32 intr, status;
 298
 299	/* We only use the DMA interrupts, and we don't enable any other
 300	 * source of interrupts. But, it is possible to see an interrupt
 301	 * status that didn't actually interrupt us, so eliminate anything
 302	 * we're not expecting to avoid falsely claiming an IRQ, and an
 303	 * ensuing endless loop.
 304	 */
 305	intr = inl(io + SIS_GISR);
 306	intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
 307		SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
 308	if (!intr)
 309		return IRQ_NONE;
 310
 311	do {
 312		status = inl(io + SIS_PISR_A);
 313		if (status) {
 314			sis_voice_irq(status, sis->voices);
 315			outl(status, io + SIS_PISR_A);
 316		}
 317
 318		status = inl(io + SIS_PISR_B);
 319		if (status) {
 320			sis_voice_irq(status, &sis->voices[32]);
 321			outl(status, io + SIS_PISR_B);
 322		}
 323
 324		status = inl(io + SIS_RISR);
 325		if (status) {
 326			voice = &sis->capture_voice;
 327			if (!voice->timing)
 328				snd_pcm_period_elapsed(voice->substream);
 329
 330			outl(status, io + SIS_RISR);
 331		}
 332
 333		outl(intr, io + SIS_GISR);
 334		intr = inl(io + SIS_GISR);
 335		intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
 336			SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
 337	} while (intr);
 338
 339	return IRQ_HANDLED;
 340}
 341
 342static u32 sis_rate_to_delta(unsigned int rate)
 343{
 344	u32 delta;
 345
 346	/* This was copied from the trident driver, but it seems its gotten
 347	 * around a bit... nevertheless, it works well.
 348	 *
 349	 * We special case 44100 and 8000 since rounding with the equation
 350	 * does not give us an accurate enough value. For 11025 and 22050
 351	 * the equation gives us the best answer. All other frequencies will
 352	 * also use the equation. JDW
 353	 */
 354	if (rate == 44100)
 355		delta = 0xeb3;
 356	else if (rate == 8000)
 357		delta = 0x2ab;
 358	else if (rate == 48000)
 359		delta = 0x1000;
 360	else
 361		delta = DIV_ROUND_CLOSEST(rate << 12, 48000) & 0x0000ffff;
 362	return delta;
 363}
 364
 365static void __sis_map_silence(struct sis7019 *sis)
 366{
 367	/* Helper function: must hold sis->voice_lock on entry */
 368	if (!sis->silence_users)
 369		sis->silence_dma_addr = dma_map_single(&sis->pci->dev,
 370						sis->suspend_state[0],
 371						4096, DMA_TO_DEVICE);
 372	sis->silence_users++;
 373}
 374
 375static void __sis_unmap_silence(struct sis7019 *sis)
 376{
 377	/* Helper function: must hold sis->voice_lock on entry */
 378	sis->silence_users--;
 379	if (!sis->silence_users)
 380		dma_unmap_single(&sis->pci->dev, sis->silence_dma_addr, 4096,
 381					DMA_TO_DEVICE);
 382}
 383
 384static void sis_free_voice(struct sis7019 *sis, struct voice *voice)
 385{
 386	guard(spinlock_irqsave)(&sis->voice_lock);
 387	if (voice->timing) {
 388		__sis_unmap_silence(sis);
 389		voice->timing->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING |
 390						VOICE_SYNC_TIMING);
 391		voice->timing = NULL;
 392	}
 393	voice->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING | VOICE_SYNC_TIMING);
 394}
 395
 396static struct voice *__sis_alloc_playback_voice(struct sis7019 *sis)
 397{
 398	/* Must hold the voice_lock on entry */
 399	struct voice *voice;
 400	int i;
 401
 402	for (i = 0; i < 64; i++) {
 403		voice = &sis->voices[i];
 404		if (voice->flags & VOICE_IN_USE)
 405			continue;
 406		voice->flags |= VOICE_IN_USE;
 407		goto found_one;
 408	}
 409	voice = NULL;
 410
 411found_one:
 412	return voice;
 413}
 414
 415static struct voice *sis_alloc_playback_voice(struct sis7019 *sis)
 416{
 417	guard(spinlock_irqsave)(&sis->voice_lock);
 418	return __sis_alloc_playback_voice(sis);
 419}
 420
 421static int sis_alloc_timing_voice(struct snd_pcm_substream *substream,
 422					struct snd_pcm_hw_params *hw_params)
 423{
 424	struct sis7019 *sis = snd_pcm_substream_chip(substream);
 425	struct snd_pcm_runtime *runtime = substream->runtime;
 426	struct voice *voice = runtime->private_data;
 427	unsigned int period_size, buffer_size;
 428	int needed;
 429
 430	/* If there are one or two periods per buffer, we don't need a
 431	 * timing voice, as we can use the capture channel's interrupts
 432	 * to clock out the periods.
 433	 */
 434	period_size = params_period_size(hw_params);
 435	buffer_size = params_buffer_size(hw_params);
 436	needed = (period_size != buffer_size &&
 437			period_size != (buffer_size / 2));
 438
 439	if (needed && !voice->timing) {
 440		scoped_guard(spinlock_irqsave, &sis->voice_lock) {
 441			voice->timing = __sis_alloc_playback_voice(sis);
 442			if (voice->timing)
 443				__sis_map_silence(sis);
 444		}
 445		if (!voice->timing)
 446			return -ENOMEM;
 447		voice->timing->substream = substream;
 448	} else if (!needed && voice->timing) {
 449		sis_free_voice(sis, voice);
 450		voice->timing = NULL;
 451	}
 452
 453	return 0;
 454}
 455
 456static int sis_playback_open(struct snd_pcm_substream *substream)
 457{
 458	struct sis7019 *sis = snd_pcm_substream_chip(substream);
 459	struct snd_pcm_runtime *runtime = substream->runtime;
 460	struct voice *voice;
 461
 462	voice = sis_alloc_playback_voice(sis);
 463	if (!voice)
 464		return -EAGAIN;
 465
 466	voice->substream = substream;
 467	runtime->private_data = voice;
 468	runtime->hw = sis_playback_hw_info;
 469	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
 470						9, 0xfff9);
 471	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
 472						9, 0xfff9);
 473	snd_pcm_set_sync(substream);
 474	return 0;
 475}
 476
 477static int sis_substream_close(struct snd_pcm_substream *substream)
 478{
 479	struct sis7019 *sis = snd_pcm_substream_chip(substream);
 480	struct snd_pcm_runtime *runtime = substream->runtime;
 481	struct voice *voice = runtime->private_data;
 482
 483	sis_free_voice(sis, voice);
 484	return 0;
 485}
 486
 487static int sis_pcm_playback_prepare(struct snd_pcm_substream *substream)
 488{
 489	struct snd_pcm_runtime *runtime = substream->runtime;
 490	struct voice *voice = runtime->private_data;
 491	void __iomem *ctrl_base = voice->ctrl_base;
 492	void __iomem *wave_base = voice->wave_base;
 493	u32 format, dma_addr, control, sso_eso, delta, reg;
 494	u16 leo;
 495
 496	/* We rely on the PCM core to ensure that the parameters for this
 497	 * substream do not change on us while we're programming the HW.
 498	 */
 499	format = 0;
 500	if (snd_pcm_format_width(runtime->format) == 8)
 501		format |= SIS_PLAY_DMA_FORMAT_8BIT;
 502	if (!snd_pcm_format_signed(runtime->format))
 503		format |= SIS_PLAY_DMA_FORMAT_UNSIGNED;
 504	if (runtime->channels == 1)
 505		format |= SIS_PLAY_DMA_FORMAT_MONO;
 506
 507	/* The baseline setup is for a single period per buffer, and
 508	 * we add bells and whistles as needed from there.
 509	 */
 510	dma_addr = runtime->dma_addr;
 511	leo = runtime->buffer_size - 1;
 512	control = leo | SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_LEO;
 513	sso_eso = leo;
 514
 515	if (runtime->period_size == (runtime->buffer_size / 2)) {
 516		control |= SIS_PLAY_DMA_INTR_AT_MLP;
 517	} else if (runtime->period_size != runtime->buffer_size) {
 518		voice->flags |= VOICE_SSO_TIMING;
 519		voice->sso = runtime->period_size - 1;
 520		voice->period_size = runtime->period_size;
 521		voice->buffer_size = runtime->buffer_size;
 522
 523		control &= ~SIS_PLAY_DMA_INTR_AT_LEO;
 524		control |= SIS_PLAY_DMA_INTR_AT_SSO;
 525		sso_eso |= (runtime->period_size - 1) << 16;
 526	}
 527
 528	delta = sis_rate_to_delta(runtime->rate);
 529
 530	/* Ok, we're ready to go, set up the channel.
 531	 */
 532	writel(format, ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
 533	writel(dma_addr, ctrl_base + SIS_PLAY_DMA_BASE);
 534	writel(control, ctrl_base + SIS_PLAY_DMA_CONTROL);
 535	writel(sso_eso, ctrl_base + SIS_PLAY_DMA_SSO_ESO);
 536
 537	for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
 538		writel(0, wave_base + reg);
 539
 540	writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
 541	writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
 542	writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
 543			SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
 544			SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
 545			wave_base + SIS_WAVE_CHANNEL_CONTROL);
 546
 547	/* Force PCI writes to post. */
 548	readl(ctrl_base);
 549
 550	return 0;
 551}
 552
 553static int sis_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 554{
 555	struct sis7019 *sis = snd_pcm_substream_chip(substream);
 556	unsigned long io = sis->ioport;
 557	struct snd_pcm_substream *s;
 558	struct voice *voice;
 559	void *chip;
 560	int starting;
 561	u32 record = 0;
 562	u32 play[2] = { 0, 0 };
 563
 564	/* No locks needed, as the PCM core will hold the locks on the
 565	 * substreams, and the HW will only start/stop the indicated voices
 566	 * without changing the state of the others.
 567	 */
 568	switch (cmd) {
 569	case SNDRV_PCM_TRIGGER_START:
 570	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 571	case SNDRV_PCM_TRIGGER_RESUME:
 572		starting = 1;
 573		break;
 574	case SNDRV_PCM_TRIGGER_STOP:
 575	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 576	case SNDRV_PCM_TRIGGER_SUSPEND:
 577		starting = 0;
 578		break;
 579	default:
 580		return -EINVAL;
 581	}
 582
 583	snd_pcm_group_for_each_entry(s, substream) {
 584		/* Make sure it is for us... */
 585		chip = snd_pcm_substream_chip(s);
 586		if (chip != sis)
 587			continue;
 588
 589		voice = s->runtime->private_data;
 590		if (voice->flags & VOICE_CAPTURE) {
 591			record |= 1 << voice->num;
 592			voice = voice->timing;
 593		}
 594
 595		/* voice could be NULL if this a recording stream, and it
 596		 * doesn't have an external timing channel.
 597		 */
 598		if (voice)
 599			play[voice->num / 32] |= 1 << (voice->num & 0x1f);
 600
 601		snd_pcm_trigger_done(s, substream);
 602	}
 603
 604	if (starting) {
 605		if (record)
 606			outl(record, io + SIS_RECORD_START_REG);
 607		if (play[0])
 608			outl(play[0], io + SIS_PLAY_START_A_REG);
 609		if (play[1])
 610			outl(play[1], io + SIS_PLAY_START_B_REG);
 611	} else {
 612		if (record)
 613			outl(record, io + SIS_RECORD_STOP_REG);
 614		if (play[0])
 615			outl(play[0], io + SIS_PLAY_STOP_A_REG);
 616		if (play[1])
 617			outl(play[1], io + SIS_PLAY_STOP_B_REG);
 618	}
 619	return 0;
 620}
 621
 622static snd_pcm_uframes_t sis_pcm_pointer(struct snd_pcm_substream *substream)
 623{
 624	struct snd_pcm_runtime *runtime = substream->runtime;
 625	struct voice *voice = runtime->private_data;
 626	u32 cso;
 627
 628	cso = readl(voice->ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
 629	cso &= 0xffff;
 630	return cso;
 631}
 632
 633static int sis_capture_open(struct snd_pcm_substream *substream)
 634{
 635	struct sis7019 *sis = snd_pcm_substream_chip(substream);
 636	struct snd_pcm_runtime *runtime = substream->runtime;
 637	struct voice *voice = &sis->capture_voice;
 638
 639	/* FIXME: The driver only supports recording from one channel
 640	 * at the moment, but it could support more.
 641	 */
 642	scoped_guard(spinlock_irqsave, &sis->voice_lock) {
 643		if (voice->flags & VOICE_IN_USE)
 644			voice = NULL;
 645		else
 646			voice->flags |= VOICE_IN_USE;
 647	}
 648
 649	if (!voice)
 650		return -EAGAIN;
 651
 652	voice->substream = substream;
 653	runtime->private_data = voice;
 654	runtime->hw = sis_capture_hw_info;
 655	runtime->hw.rates = sis->ac97[0]->rates[AC97_RATES_ADC];
 656	snd_pcm_limit_hw_rates(runtime);
 657	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
 658						9, 0xfff9);
 659	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
 660						9, 0xfff9);
 661	snd_pcm_set_sync(substream);
 662	return 0;
 663}
 664
 665static int sis_capture_hw_params(struct snd_pcm_substream *substream,
 666					struct snd_pcm_hw_params *hw_params)
 667{
 668	struct sis7019 *sis = snd_pcm_substream_chip(substream);
 669	int rc;
 670
 671	rc = snd_ac97_set_rate(sis->ac97[0], AC97_PCM_LR_ADC_RATE,
 672						params_rate(hw_params));
 673	if (rc)
 674		goto out;
 675
 676	rc = sis_alloc_timing_voice(substream, hw_params);
 677
 678out:
 679	return rc;
 680}
 681
 682static void sis_prepare_timing_voice(struct voice *voice,
 683					struct snd_pcm_substream *substream)
 684{
 685	struct sis7019 *sis = snd_pcm_substream_chip(substream);
 686	struct snd_pcm_runtime *runtime = substream->runtime;
 687	struct voice *timing = voice->timing;
 688	void __iomem *play_base = timing->ctrl_base;
 689	void __iomem *wave_base = timing->wave_base;
 690	u16 buffer_size, period_size;
 691	u32 format, control, sso_eso, delta;
 692	u32 vperiod, sso, reg;
 693
 694	/* Set our initial buffer and period as large as we can given a
 695	 * single page of silence.
 696	 */
 697	buffer_size = 4096 / runtime->channels;
 698	buffer_size /= snd_pcm_format_size(runtime->format, 1);
 699	period_size = buffer_size;
 700
 701	/* Initially, we want to interrupt just a bit behind the end of
 702	 * the period we're clocking out. 12 samples seems to give a good
 703	 * delay.
 704	 *
 705	 * We want to spread our interrupts throughout the virtual period,
 706	 * so that we don't end up with two interrupts back to back at the
 707	 * end -- this helps minimize the effects of any jitter. Adjust our
 708	 * clocking period size so that the last period is at least a fourth
 709	 * of a full period.
 710	 *
 711	 * This is all moot if we don't need to use virtual periods.
 712	 */
 713	vperiod = runtime->period_size + 12;
 714	if (vperiod > period_size) {
 715		u16 tail = vperiod % period_size;
 716		u16 quarter_period = period_size / 4;
 717
 718		if (tail && tail < quarter_period) {
 719			u16 loops = vperiod / period_size;
 720
 721			tail = quarter_period - tail;
 722			tail += loops - 1;
 723			tail /= loops;
 724			period_size -= tail;
 725		}
 726
 727		sso = period_size - 1;
 728	} else {
 729		/* The initial period will fit inside the buffer, so we
 730		 * don't need to use virtual periods -- disable them.
 731		 */
 732		period_size = runtime->period_size;
 733		sso = vperiod - 1;
 734		vperiod = 0;
 735	}
 736
 737	/* The interrupt handler implements the timing synchronization, so
 738	 * setup its state.
 739	 */
 740	timing->flags |= VOICE_SYNC_TIMING;
 741	timing->sync_base = voice->ctrl_base;
 742	timing->sync_cso = runtime->period_size;
 743	timing->sync_period_size = runtime->period_size;
 744	timing->sync_buffer_size = runtime->buffer_size;
 745	timing->period_size = period_size;
 746	timing->buffer_size = buffer_size;
 747	timing->sso = sso;
 748	timing->vperiod = vperiod;
 749
 750	/* Using unsigned samples with the all-zero silence buffer
 751	 * forces the output to the lower rail, killing playback.
 752	 * So ignore unsigned vs signed -- it doesn't change the timing.
 753	 */
 754	format = 0;
 755	if (snd_pcm_format_width(runtime->format) == 8)
 756		format = SIS_CAPTURE_DMA_FORMAT_8BIT;
 757	if (runtime->channels == 1)
 758		format |= SIS_CAPTURE_DMA_FORMAT_MONO;
 759
 760	control = timing->buffer_size - 1;
 761	control |= SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_SSO;
 762	sso_eso = timing->buffer_size - 1;
 763	sso_eso |= timing->sso << 16;
 764
 765	delta = sis_rate_to_delta(runtime->rate);
 766
 767	/* We've done the math, now configure the channel.
 768	 */
 769	writel(format, play_base + SIS_PLAY_DMA_FORMAT_CSO);
 770	writel(sis->silence_dma_addr, play_base + SIS_PLAY_DMA_BASE);
 771	writel(control, play_base + SIS_PLAY_DMA_CONTROL);
 772	writel(sso_eso, play_base + SIS_PLAY_DMA_SSO_ESO);
 773
 774	for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
 775		writel(0, wave_base + reg);
 776
 777	writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
 778	writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
 779	writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
 780			SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
 781			SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
 782			wave_base + SIS_WAVE_CHANNEL_CONTROL);
 783}
 784
 785static int sis_pcm_capture_prepare(struct snd_pcm_substream *substream)
 786{
 787	struct snd_pcm_runtime *runtime = substream->runtime;
 788	struct voice *voice = runtime->private_data;
 789	void __iomem *rec_base = voice->ctrl_base;
 790	u32 format, dma_addr, control;
 791	u16 leo;
 792
 793	/* We rely on the PCM core to ensure that the parameters for this
 794	 * substream do not change on us while we're programming the HW.
 795	 */
 796	format = 0;
 797	if (snd_pcm_format_width(runtime->format) == 8)
 798		format = SIS_CAPTURE_DMA_FORMAT_8BIT;
 799	if (!snd_pcm_format_signed(runtime->format))
 800		format |= SIS_CAPTURE_DMA_FORMAT_UNSIGNED;
 801	if (runtime->channels == 1)
 802		format |= SIS_CAPTURE_DMA_FORMAT_MONO;
 803
 804	dma_addr = runtime->dma_addr;
 805	leo = runtime->buffer_size - 1;
 806	control = leo | SIS_CAPTURE_DMA_LOOP;
 807
 808	/* If we've got more than two periods per buffer, then we have
 809	 * use a timing voice to clock out the periods. Otherwise, we can
 810	 * use the capture channel's interrupts.
 811	 */
 812	if (voice->timing) {
 813		sis_prepare_timing_voice(voice, substream);
 814	} else {
 815		control |= SIS_CAPTURE_DMA_INTR_AT_LEO;
 816		if (runtime->period_size != runtime->buffer_size)
 817			control |= SIS_CAPTURE_DMA_INTR_AT_MLP;
 818	}
 819
 820	writel(format, rec_base + SIS_CAPTURE_DMA_FORMAT_CSO);
 821	writel(dma_addr, rec_base + SIS_CAPTURE_DMA_BASE);
 822	writel(control, rec_base + SIS_CAPTURE_DMA_CONTROL);
 823
 824	/* Force the writes to post. */
 825	readl(rec_base);
 826
 827	return 0;
 828}
 829
 830static const struct snd_pcm_ops sis_playback_ops = {
 831	.open = sis_playback_open,
 832	.close = sis_substream_close,
 833	.prepare = sis_pcm_playback_prepare,
 834	.trigger = sis_pcm_trigger,
 835	.pointer = sis_pcm_pointer,
 836};
 837
 838static const struct snd_pcm_ops sis_capture_ops = {
 839	.open = sis_capture_open,
 840	.close = sis_substream_close,
 841	.hw_params = sis_capture_hw_params,
 842	.prepare = sis_pcm_capture_prepare,
 843	.trigger = sis_pcm_trigger,
 844	.pointer = sis_pcm_pointer,
 845};
 846
 847static int sis_pcm_create(struct sis7019 *sis)
 848{
 849	struct snd_pcm *pcm;
 850	int rc;
 851
 852	/* We have 64 voices, and the driver currently records from
 853	 * only one channel, though that could change in the future.
 854	 */
 855	rc = snd_pcm_new(sis->card, "SiS7019", 0, 64, 1, &pcm);
 856	if (rc)
 857		return rc;
 858
 859	pcm->private_data = sis;
 860	strscpy(pcm->name, "SiS7019");
 861	sis->pcm = pcm;
 862
 863	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &sis_playback_ops);
 864	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &sis_capture_ops);
 865
 866	/* Try to preallocate some memory, but it's not the end of the
 867	 * world if this fails.
 868	 */
 869	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
 870				       &sis->pci->dev, 64*1024, 128*1024);
 871
 872	return 0;
 873}
 874
 875static unsigned short sis_ac97_rw(struct sis7019 *sis, int codec, u32 cmd)
 876{
 877	unsigned long io = sis->ioport;
 878	unsigned short val = 0xffff;
 879	u16 status;
 880	u16 rdy;
 881	int count;
 882	static const u16 codec_ready[3] = {
 883		SIS_AC97_STATUS_CODEC_READY,
 884		SIS_AC97_STATUS_CODEC2_READY,
 885		SIS_AC97_STATUS_CODEC3_READY,
 886	};
 887
 888	rdy = codec_ready[codec];
 889
 890
 891	/* Get the AC97 semaphore -- software first, so we don't spin
 892	 * pounding out IO reads on the hardware semaphore...
 893	 */
 894	guard(mutex)(&sis->ac97_mutex);
 895
 896	count = 0xffff;
 897	while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
 898		udelay(1);
 899
 900	if (!count)
 901		goto timeout;
 902
 903	/* ... and wait for any outstanding commands to complete ...
 904	 */
 905	count = 0xffff;
 906	do {
 907		status = inw(io + SIS_AC97_STATUS);
 908		if ((status & rdy) && !(status & SIS_AC97_STATUS_BUSY))
 909			break;
 910
 911		udelay(1);
 912	} while (--count);
 913
 914	if (!count)
 915		goto timeout_sema;
 916
 917	/* ... before sending our command and waiting for it to finish ...
 918	 */
 919	outl(cmd, io + SIS_AC97_CMD);
 920	udelay(10);
 921
 922	count = 0xffff;
 923	while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
 924		udelay(1);
 925
 926	/* ... and reading the results (if any).
 927	 */
 928	val = inl(io + SIS_AC97_CMD) >> 16;
 929
 930timeout_sema:
 931	outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
 932timeout:
 933	if (!count) {
 934		dev_err(&sis->pci->dev, "ac97 codec %d timeout cmd 0x%08x\n",
 935					codec, cmd);
 936	}
 937
 938	return val;
 939}
 940
 941static void sis_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
 942				unsigned short val)
 943{
 944	static const u32 cmd[3] = {
 945		SIS_AC97_CMD_CODEC_WRITE,
 946		SIS_AC97_CMD_CODEC2_WRITE,
 947		SIS_AC97_CMD_CODEC3_WRITE,
 948	};
 949	sis_ac97_rw(ac97->private_data, ac97->num,
 950			(val << 16) | (reg << 8) | cmd[ac97->num]);
 951}
 952
 953static unsigned short sis_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
 954{
 955	static const u32 cmd[3] = {
 956		SIS_AC97_CMD_CODEC_READ,
 957		SIS_AC97_CMD_CODEC2_READ,
 958		SIS_AC97_CMD_CODEC3_READ,
 959	};
 960	return sis_ac97_rw(ac97->private_data, ac97->num,
 961					(reg << 8) | cmd[ac97->num]);
 962}
 963
 964static int sis_mixer_create(struct sis7019 *sis)
 965{
 966	struct snd_ac97_bus *bus;
 967	struct snd_ac97_template ac97;
 968	static const struct snd_ac97_bus_ops ops = {
 969		.write = sis_ac97_write,
 970		.read = sis_ac97_read,
 971	};
 972	int rc;
 973
 974	memset(&ac97, 0, sizeof(ac97));
 975	ac97.private_data = sis;
 976
 977	rc = snd_ac97_bus(sis->card, 0, &ops, NULL, &bus);
 978	if (!rc && sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
 979		rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[0]);
 980	ac97.num = 1;
 981	if (!rc && (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT))
 982		rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[1]);
 983	ac97.num = 2;
 984	if (!rc && (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT))
 985		rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[2]);
 986
 987	/* If we return an error here, then snd_card_free() should
 988	 * free up any ac97 codecs that got created, as well as the bus.
 989	 */
 990	return rc;
 991}
 992
 993static void sis_chip_free(struct snd_card *card)
 994{
 995	struct sis7019 *sis = card->private_data;
 996
 997	/* Reset the chip, and disable all interrputs.
 998	 */
 999	outl(SIS_GCR_SOFTWARE_RESET, sis->ioport + SIS_GCR);
1000	udelay(25);
1001	outl(0, sis->ioport + SIS_GCR);
1002	outl(0, sis->ioport + SIS_GIER);
1003
1004	/* Now, free everything we allocated.
1005	 */
1006	if (sis->irq >= 0)
1007		free_irq(sis->irq, sis);
1008}
1009
1010static int sis_chip_init(struct sis7019 *sis)
1011{
1012	unsigned long io = sis->ioport;
1013	void __iomem *ioaddr = sis->ioaddr;
1014	unsigned long timeout;
1015	u16 status;
1016	int count;
1017	int i;
1018
1019	/* Reset the audio controller
1020	 */
1021	outl(SIS_GCR_SOFTWARE_RESET, io + SIS_GCR);
1022	udelay(25);
1023	outl(0, io + SIS_GCR);
1024
1025	/* Get the AC-link semaphore, and reset the codecs
1026	 */
1027	count = 0xffff;
1028	while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
1029		udelay(1);
1030
1031	if (!count)
1032		return -EIO;
1033
1034	outl(SIS_AC97_CMD_CODEC_COLD_RESET, io + SIS_AC97_CMD);
1035	udelay(250);
1036
1037	count = 0xffff;
1038	while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
1039		udelay(1);
1040
1041	/* Command complete, we can let go of the semaphore now.
1042	 */
1043	outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
1044	if (!count)
1045		return -EIO;
1046
1047	/* Now that we've finished the reset, find out what's attached.
1048	 * There are some codec/board combinations that take an extremely
1049	 * long time to come up. 350+ ms has been observed in the field,
1050	 * so we'll give them up to 500ms.
1051	 */
1052	sis->codecs_present = 0;
1053	timeout = msecs_to_jiffies(500) + jiffies;
1054	while (time_before_eq(jiffies, timeout)) {
1055		status = inl(io + SIS_AC97_STATUS);
1056		if (status & SIS_AC97_STATUS_CODEC_READY)
1057			sis->codecs_present |= SIS_PRIMARY_CODEC_PRESENT;
1058		if (status & SIS_AC97_STATUS_CODEC2_READY)
1059			sis->codecs_present |= SIS_SECONDARY_CODEC_PRESENT;
1060		if (status & SIS_AC97_STATUS_CODEC3_READY)
1061			sis->codecs_present |= SIS_TERTIARY_CODEC_PRESENT;
1062
1063		if (sis->codecs_present == codecs)
1064			break;
1065
1066		msleep(1);
1067	}
1068
1069	/* All done, check for errors.
1070	 */
1071	if (!sis->codecs_present) {
1072		dev_err(&sis->pci->dev, "could not find any codecs\n");
1073		return -EIO;
1074	}
1075
1076	if (sis->codecs_present != codecs) {
1077		dev_warn(&sis->pci->dev, "missing codecs, found %0x, expected %0x\n",
1078					 sis->codecs_present, codecs);
1079	}
1080
1081	/* Let the hardware know that the audio driver is alive,
1082	 * and enable PCM slots on the AC-link for L/R playback (3 & 4) and
1083	 * record channels. We're going to want to use Variable Rate Audio
1084	 * for recording, to avoid needlessly resampling from 48kHZ.
1085	 */
1086	outl(SIS_AC97_CONF_AUDIO_ALIVE, io + SIS_AC97_CONF);
1087	outl(SIS_AC97_CONF_AUDIO_ALIVE | SIS_AC97_CONF_PCM_LR_ENABLE |
1088		SIS_AC97_CONF_PCM_CAP_MIC_ENABLE |
1089		SIS_AC97_CONF_PCM_CAP_LR_ENABLE |
1090		SIS_AC97_CONF_CODEC_VRA_ENABLE, io + SIS_AC97_CONF);
1091
1092	/* All AC97 PCM slots should be sourced from sub-mixer 0.
1093	 */
1094	outl(0, io + SIS_AC97_PSR);
1095
1096	/* There is only one valid DMA setup for a PCI environment.
1097	 */
1098	outl(SIS_DMA_CSR_PCI_SETTINGS, io + SIS_DMA_CSR);
1099
1100	/* Reset the synchronization groups for all of the channels
1101	 * to be asynchronous. If we start doing SPDIF or 5.1 sound, etc.
1102	 * we'll need to change how we handle these. Until then, we just
1103	 * assign sub-mixer 0 to all playback channels, and avoid any
1104	 * attenuation on the audio.
1105	 */
1106	outl(0, io + SIS_PLAY_SYNC_GROUP_A);
1107	outl(0, io + SIS_PLAY_SYNC_GROUP_B);
1108	outl(0, io + SIS_PLAY_SYNC_GROUP_C);
1109	outl(0, io + SIS_PLAY_SYNC_GROUP_D);
1110	outl(0, io + SIS_MIXER_SYNC_GROUP);
1111
1112	for (i = 0; i < 64; i++) {
1113		writel(i, SIS_MIXER_START_ADDR(ioaddr, i));
1114		writel(SIS_MIXER_RIGHT_NO_ATTEN | SIS_MIXER_LEFT_NO_ATTEN |
1115				SIS_MIXER_DEST_0, SIS_MIXER_ADDR(ioaddr, i));
1116	}
1117
1118	/* Don't attenuate any audio set for the wave amplifier.
1119	 *
1120	 * FIXME: Maximum attenuation is set for the music amp, which will
1121	 * need to change if we start using the synth engine.
1122	 */
1123	outl(0xffff0000, io + SIS_WEVCR);
1124
1125	/* Ensure that the wave engine is in normal operating mode.
1126	 */
1127	outl(0, io + SIS_WECCR);
1128
1129	/* Go ahead and enable the DMA interrupts. They won't go live
1130	 * until we start a channel.
1131	 */
1132	outl(SIS_GIER_AUDIO_PLAY_DMA_IRQ_ENABLE |
1133		SIS_GIER_AUDIO_RECORD_DMA_IRQ_ENABLE, io + SIS_GIER);
1134
1135	return 0;
1136}
1137
1138static int sis_suspend(struct device *dev)
1139{
1140	struct snd_card *card = dev_get_drvdata(dev);
1141	struct sis7019 *sis = card->private_data;
1142	void __iomem *ioaddr = sis->ioaddr;
1143	int i;
1144
1145	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1146	if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1147		snd_ac97_suspend(sis->ac97[0]);
1148	if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1149		snd_ac97_suspend(sis->ac97[1]);
1150	if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1151		snd_ac97_suspend(sis->ac97[2]);
1152
1153	/* snd_pcm_suspend_all() stopped all channels, so we're quiescent.
1154	 */
1155	if (sis->irq >= 0) {
1156		free_irq(sis->irq, sis);
1157		sis->irq = -1;
1158	}
1159
1160	/* Save the internal state away
1161	 */
1162	for (i = 0; i < 4; i++) {
1163		memcpy_fromio(sis->suspend_state[i], ioaddr, 4096);
1164		ioaddr += 4096;
1165	}
1166
1167	return 0;
1168}
1169
1170static int sis_resume(struct device *dev)
1171{
1172	struct pci_dev *pci = to_pci_dev(dev);
1173	struct snd_card *card = dev_get_drvdata(dev);
1174	struct sis7019 *sis = card->private_data;
1175	void __iomem *ioaddr = sis->ioaddr;
1176	int i;
1177
1178	if (sis_chip_init(sis)) {
1179		dev_err(&pci->dev, "unable to re-init controller\n");
1180		goto error;
1181	}
1182
1183	if (request_irq(pci->irq, sis_interrupt, IRQF_SHARED,
1184			KBUILD_MODNAME, sis)) {
1185		dev_err(&pci->dev, "unable to regain IRQ %d\n", pci->irq);
1186		goto error;
1187	}
1188
1189	/* Restore saved state, then clear out the page we use for the
1190	 * silence buffer.
1191	 */
1192	for (i = 0; i < 4; i++) {
1193		memcpy_toio(ioaddr, sis->suspend_state[i], 4096);
1194		ioaddr += 4096;
1195	}
1196
1197	memset(sis->suspend_state[0], 0, 4096);
1198
1199	sis->irq = pci->irq;
1200
1201	if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1202		snd_ac97_resume(sis->ac97[0]);
1203	if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1204		snd_ac97_resume(sis->ac97[1]);
1205	if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1206		snd_ac97_resume(sis->ac97[2]);
1207
1208	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1209	return 0;
1210
1211error:
1212	snd_card_disconnect(card);
1213	return -EIO;
1214}
1215
1216static DEFINE_SIMPLE_DEV_PM_OPS(sis_pm, sis_suspend, sis_resume);
1217
1218static int sis_alloc_suspend(struct sis7019 *sis)
1219{
1220	int i;
1221
1222	/* We need 16K to store the internal wave engine state during a
1223	 * suspend, but we don't need it to be contiguous, so play nice
1224	 * with the memory system. We'll also use this area for a silence
1225	 * buffer.
1226	 */
1227	for (i = 0; i < SIS_SUSPEND_PAGES; i++) {
1228		sis->suspend_state[i] = devm_kmalloc(&sis->pci->dev, 4096,
1229						     GFP_KERNEL);
1230		if (!sis->suspend_state[i])
1231			return -ENOMEM;
1232	}
1233	memset(sis->suspend_state[0], 0, 4096);
1234
1235	return 0;
1236}
1237
1238static int sis_chip_create(struct snd_card *card,
1239			   struct pci_dev *pci)
1240{
1241	struct sis7019 *sis = card->private_data;
1242	struct voice *voice;
1243	int rc;
1244	int i;
1245
1246	rc = pcim_enable_device(pci);
1247	if (rc)
1248		return rc;
1249
1250	rc = dma_set_mask(&pci->dev, DMA_BIT_MASK(30));
1251	if (rc < 0) {
1252		dev_err(&pci->dev, "architecture does not support 30-bit PCI busmaster DMA");
1253		return -ENXIO;
1254	}
1255
1256	mutex_init(&sis->ac97_mutex);
1257	spin_lock_init(&sis->voice_lock);
1258	sis->card = card;
1259	sis->pci = pci;
1260	sis->irq = -1;
1261	sis->ioport = pci_resource_start(pci, 0);
1262
1263	rc = pcim_request_all_regions(pci, "SiS7019");
1264	if (rc) {
1265		dev_err(&pci->dev, "unable request regions\n");
1266		return rc;
1267	}
1268
1269	sis->ioaddr = devm_ioremap(&pci->dev, pci_resource_start(pci, 1), 0x4000);
1270	if (!sis->ioaddr) {
1271		dev_err(&pci->dev, "unable to remap MMIO, aborting\n");
1272		return -EIO;
1273	}
1274
1275	rc = sis_alloc_suspend(sis);
1276	if (rc < 0) {
1277		dev_err(&pci->dev, "unable to allocate state storage\n");
1278		return rc;
1279	}
1280
1281	rc = sis_chip_init(sis);
1282	if (rc)
1283		return rc;
1284	card->private_free = sis_chip_free;
1285
1286	rc = request_irq(pci->irq, sis_interrupt, IRQF_SHARED, KBUILD_MODNAME,
1287			 sis);
1288	if (rc) {
1289		dev_err(&pci->dev, "unable to allocate irq %d\n", sis->irq);
1290		return rc;
1291	}
1292
1293	sis->irq = pci->irq;
1294	card->sync_irq = sis->irq;
1295	pci_set_master(pci);
1296
1297	for (i = 0; i < 64; i++) {
1298		voice = &sis->voices[i];
1299		voice->num = i;
1300		voice->ctrl_base = SIS_PLAY_DMA_ADDR(sis->ioaddr, i);
1301		voice->wave_base = SIS_WAVE_ADDR(sis->ioaddr, i);
1302	}
1303
1304	voice = &sis->capture_voice;
1305	voice->flags = VOICE_CAPTURE;
1306	voice->num = SIS_CAPTURE_CHAN_AC97_PCM_IN;
1307	voice->ctrl_base = SIS_CAPTURE_DMA_ADDR(sis->ioaddr, voice->num);
1308
1309	return 0;
1310}
1311
1312static int __snd_sis7019_probe(struct pci_dev *pci,
1313			       const struct pci_device_id *pci_id)
1314{
1315	struct snd_card *card;
1316	struct sis7019 *sis;
1317	int rc;
1318
1319	if (!enable)
1320		return -ENOENT;
1321
1322	/* The user can specify which codecs should be present so that we
1323	 * can wait for them to show up if they are slow to recover from
1324	 * the AC97 cold reset. We default to a single codec, the primary.
1325	 *
1326	 * We assume that SIS_PRIMARY_*_PRESENT matches bits 0-2.
1327	 */
1328	codecs &= SIS_PRIMARY_CODEC_PRESENT | SIS_SECONDARY_CODEC_PRESENT |
1329		  SIS_TERTIARY_CODEC_PRESENT;
1330	if (!codecs)
1331		codecs = SIS_PRIMARY_CODEC_PRESENT;
1332
1333	rc = snd_devm_card_new(&pci->dev, index, id, THIS_MODULE,
1334			       sizeof(*sis), &card);
1335	if (rc < 0)
1336		return rc;
1337
1338	strscpy(card->driver, "SiS7019");
1339	strscpy(card->shortname, "SiS7019");
1340	rc = sis_chip_create(card, pci);
1341	if (rc)
1342		return rc;
1343
1344	sis = card->private_data;
1345
1346	rc = sis_mixer_create(sis);
1347	if (rc)
1348		return rc;
1349
1350	rc = sis_pcm_create(sis);
1351	if (rc)
1352		return rc;
1353
1354	snprintf(card->longname, sizeof(card->longname),
1355			"%s Audio Accelerator with %s at 0x%lx, irq %d",
1356			card->shortname, snd_ac97_get_short_name(sis->ac97[0]),
1357			sis->ioport, sis->irq);
1358
1359	rc = snd_card_register(card);
1360	if (rc)
1361		return rc;
1362
1363	pci_set_drvdata(pci, card);
1364	return 0;
1365}
1366
1367static int snd_sis7019_probe(struct pci_dev *pci,
1368			     const struct pci_device_id *pci_id)
1369{
1370	return snd_card_free_on_error(&pci->dev, __snd_sis7019_probe(pci, pci_id));
1371}
1372
1373static struct pci_driver sis7019_driver = {
1374	.name = KBUILD_MODNAME,
1375	.id_table = snd_sis7019_ids,
1376	.probe = snd_sis7019_probe,
1377	.driver = {
1378		.pm = &sis_pm,
1379	},
1380};
1381
1382module_pci_driver(sis7019_driver);
~/ Read-only file preview.