hda_controller.c 36.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 *
 *  Implementation of primary alsa driver code base for Intel HD Audio.
 *
 *  Copyright(c) 2004 Intel Corporation. All rights reserved.
 *
 *  Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
 *                     PeiSen Hou <pshou@realtek.com.tw>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 *  more details.
 *
 *
 */

#include <linux/clocksource.h>
#include <linux/delay.h>
25
#include <linux/interrupt.h>
26 27
#include <linux/kernel.h>
#include <linux/module.h>
28
#include <linux/pm_runtime.h>
29
#include <linux/slab.h>
30 31 32 33 34 35

#ifdef CONFIG_X86
/* for art-tsc conversion */
#include <asm/tsc.h>
#endif

36 37 38 39
#include <sound/core.h>
#include <sound/initval.h>
#include "hda_controller.h"

40
#define CREATE_TRACE_POINTS
41
#include "hda_controller_trace.h"
42

43
/* DSP lock helpers */
44 45 46
#define dsp_lock(dev)		snd_hdac_dsp_lock(azx_stream(dev))
#define dsp_unlock(dev)		snd_hdac_dsp_unlock(azx_stream(dev))
#define dsp_is_locked(dev)	snd_hdac_stream_is_locked(azx_stream(dev))
47

48 49 50 51
/* assign a stream for the PCM */
static inline struct azx_dev *
azx_assign_device(struct azx *chip, struct snd_pcm_substream *substream)
{
52 53 54 55 56 57
	struct hdac_stream *s;

	s = snd_hdac_stream_assign(azx_bus(chip), substream);
	if (!s)
		return NULL;
	return stream_to_azx_dev(s);
58 59 60 61 62
}

/* release the assigned stream */
static inline void azx_release_device(struct azx_dev *azx_dev)
{
63
	snd_hdac_stream_release(azx_stream(azx_dev));
64 65
}

66 67 68 69 70 71 72
static inline struct hda_pcm_stream *
to_hda_pcm_stream(struct snd_pcm_substream *substream)
{
	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
	return &apcm->info->stream[substream->stream];
}

73 74 75 76
static u64 azx_adjust_codec_delay(struct snd_pcm_substream *substream,
				u64 nsec)
{
	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
77
	struct hda_pcm_stream *hinfo = to_hda_pcm_stream(substream);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	u64 codec_frames, codec_nsecs;

	if (!hinfo->ops.get_delay)
		return nsec;

	codec_frames = hinfo->ops.get_delay(hinfo, apcm->codec, substream);
	codec_nsecs = div_u64(codec_frames * 1000000000LL,
			      substream->runtime->rate);

	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
		return nsec + codec_nsecs;

	return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
}

/*
 * PCM ops
 */

static int azx_pcm_close(struct snd_pcm_substream *substream)
{
	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
100
	struct hda_pcm_stream *hinfo = to_hda_pcm_stream(substream);
101 102 103
	struct azx *chip = apcm->chip;
	struct azx_dev *azx_dev = get_azx_dev(substream);

104
	trace_azx_pcm_close(chip, azx_dev);
105 106
	mutex_lock(&chip->open_mutex);
	azx_release_device(azx_dev);
107 108
	if (hinfo->ops.close)
		hinfo->ops.close(hinfo, apcm->codec, substream);
109 110
	snd_hda_power_down(apcm->codec);
	mutex_unlock(&chip->open_mutex);
111
	snd_hda_codec_pcm_put(apcm->info);
112 113 114 115 116 117 118 119
	return 0;
}

static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
			     struct snd_pcm_hw_params *hw_params)
{
	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
	struct azx *chip = apcm->chip;
T
Takashi Iwai 已提交
120
	struct azx_dev *azx_dev = get_azx_dev(substream);
121 122
	int ret;

123
	trace_azx_pcm_hw_params(chip, azx_dev);
T
Takashi Iwai 已提交
124 125
	dsp_lock(azx_dev);
	if (dsp_is_locked(azx_dev)) {
126 127 128 129
		ret = -EBUSY;
		goto unlock;
	}

T
Takashi Iwai 已提交
130 131 132
	azx_dev->core.bufsize = 0;
	azx_dev->core.period_bytes = 0;
	azx_dev->core.format_val = 0;
133 134 135
	ret = chip->ops->substream_alloc_pages(chip, substream,
					  params_buffer_bytes(hw_params));
unlock:
T
Takashi Iwai 已提交
136
	dsp_unlock(azx_dev);
137 138 139 140 141 142 143 144
	return ret;
}

static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
{
	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
	struct azx_dev *azx_dev = get_azx_dev(substream);
	struct azx *chip = apcm->chip;
145
	struct hda_pcm_stream *hinfo = to_hda_pcm_stream(substream);
146 147 148 149
	int err;

	/* reset BDL address */
	dsp_lock(azx_dev);
150 151
	if (!dsp_is_locked(azx_dev))
		snd_hdac_stream_cleanup(azx_stream(azx_dev));
152 153 154 155

	snd_hda_codec_cleanup(apcm->codec, hinfo, substream);

	err = chip->ops->substream_free_pages(chip, substream);
156
	azx_stream(azx_dev)->prepared = 0;
157 158 159 160 161 162 163 164 165
	dsp_unlock(azx_dev);
	return err;
}

static int azx_pcm_prepare(struct snd_pcm_substream *substream)
{
	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
	struct azx *chip = apcm->chip;
	struct azx_dev *azx_dev = get_azx_dev(substream);
166
	struct hda_pcm_stream *hinfo = to_hda_pcm_stream(substream);
167
	struct snd_pcm_runtime *runtime = substream->runtime;
168
	unsigned int format_val, stream_tag;
169 170 171 172 173
	int err;
	struct hda_spdif_out *spdif =
		snd_hda_spdif_out_of_nid(apcm->codec, hinfo->nid);
	unsigned short ctls = spdif ? spdif->ctls : 0;

174
	trace_azx_pcm_prepare(chip, azx_dev);
175 176 177 178 179 180
	dsp_lock(azx_dev);
	if (dsp_is_locked(azx_dev)) {
		err = -EBUSY;
		goto unlock;
	}

181
	snd_hdac_stream_reset(azx_stream(azx_dev));
182
	format_val = snd_hdac_calc_stream_format(runtime->rate,
183 184 185 186 187 188 189 190 191 192 193 194
						runtime->channels,
						runtime->format,
						hinfo->maxbps,
						ctls);
	if (!format_val) {
		dev_err(chip->card->dev,
			"invalid format_val, rate=%d, ch=%d, format=%d\n",
			runtime->rate, runtime->channels, runtime->format);
		err = -EINVAL;
		goto unlock;
	}

195 196 197
	err = snd_hdac_stream_set_params(azx_stream(azx_dev), format_val);
	if (err < 0)
		goto unlock;
198

199
	snd_hdac_stream_setup(azx_stream(azx_dev));
200

201
	stream_tag = azx_dev->core.stream_tag;
202 203 204 205 206
	/* CA-IBG chips need the playback stream starting from 1 */
	if ((chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND) &&
	    stream_tag > chip->capture_streams)
		stream_tag -= chip->capture_streams;
	err = snd_hda_codec_prepare(apcm->codec, hinfo, stream_tag,
207
				     azx_dev->core.format_val, substream);
208 209 210

 unlock:
	if (!err)
211
		azx_stream(azx_dev)->prepared = 1;
212 213 214 215 216 217 218 219
	dsp_unlock(azx_dev);
	return err;
}

static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
{
	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
	struct azx *chip = apcm->chip;
220
	struct hdac_bus *bus = azx_bus(chip);
221 222
	struct azx_dev *azx_dev;
	struct snd_pcm_substream *s;
223 224 225 226
	struct hdac_stream *hstr;
	bool start;
	int sbits = 0;
	int sync_reg;
227 228

	azx_dev = get_azx_dev(substream);
229 230
	trace_azx_pcm_trigger(chip, azx_dev, cmd);

231 232 233 234 235
	hstr = azx_stream(azx_dev);
	if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
		sync_reg = AZX_REG_OLD_SSYNC;
	else
		sync_reg = AZX_REG_SSYNC;
236

237
	if (dsp_is_locked(azx_dev) || !hstr->prepared)
238 239 240 241 242 243
		return -EPIPE;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
	case SNDRV_PCM_TRIGGER_RESUME:
244
		start = true;
245 246 247 248
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_STOP:
249
		start = false;
250 251 252 253 254 255 256 257 258
		break;
	default:
		return -EINVAL;
	}

	snd_pcm_group_for_each_entry(s, substream) {
		if (s->pcm->card != substream->pcm->card)
			continue;
		azx_dev = get_azx_dev(s);
259
		sbits |= 1 << azx_dev->core.index;
260 261 262
		snd_pcm_trigger_done(s, substream);
	}

263
	spin_lock(&bus->reg_lock);
264 265

	/* first, set SYNC bits of corresponding streams */
266
	snd_hdac_stream_sync_trigger(hstr, true, sbits, sync_reg);
267 268 269 270 271 272

	snd_pcm_group_for_each_entry(s, substream) {
		if (s->pcm->card != substream->pcm->card)
			continue;
		azx_dev = get_azx_dev(s);
		if (start) {
273 274
			azx_dev->insufficient = 1;
			snd_hdac_stream_start(azx_stream(azx_dev), true);
275
		} else {
276
			snd_hdac_stream_stop(azx_stream(azx_dev));
277 278
		}
	}
279
	spin_unlock(&bus->reg_lock);
280 281 282

	snd_hdac_stream_sync(hstr, start, sbits);

283
	spin_lock(&bus->reg_lock);
284
	/* reset SYNC bits */
285 286 287
	snd_hdac_stream_sync_trigger(hstr, false, sbits, sync_reg);
	if (start)
		snd_hdac_stream_timecounter_init(hstr, sbits);
288
	spin_unlock(&bus->reg_lock);
289 290 291
	return 0;
}

292
unsigned int azx_get_pos_lpib(struct azx *chip, struct azx_dev *azx_dev)
293
{
294
	return snd_hdac_stream_get_pos_lpib(azx_stream(azx_dev));
295 296
}
EXPORT_SYMBOL_GPL(azx_get_pos_lpib);
297

298 299
unsigned int azx_get_pos_posbuf(struct azx *chip, struct azx_dev *azx_dev)
{
300
	return snd_hdac_stream_get_pos_posbuf(azx_stream(azx_dev));
301
}
302
EXPORT_SYMBOL_GPL(azx_get_pos_posbuf);
303 304

unsigned int azx_get_position(struct azx *chip,
305
			      struct azx_dev *azx_dev)
306
{
307
	struct snd_pcm_substream *substream = azx_dev->core.substream;
308 309 310 311
	unsigned int pos;
	int stream = substream->stream;
	int delay = 0;

312 313 314 315
	if (chip->get_position[stream])
		pos = chip->get_position[stream](chip, azx_dev);
	else /* use the position buffer as default */
		pos = azx_get_pos_posbuf(chip, azx_dev);
316

317
	if (pos >= azx_dev->core.bufsize)
318 319 320
		pos = 0;

	if (substream->runtime) {
321
		struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
322
		struct hda_pcm_stream *hinfo = to_hda_pcm_stream(substream);
323 324 325

		if (chip->get_delay[stream])
			delay += chip->get_delay[stream](chip, azx_dev, pos);
326 327 328 329 330 331
		if (hinfo->ops.get_delay)
			delay += hinfo->ops.get_delay(hinfo, apcm->codec,
						      substream);
		substream->runtime->delay = delay;
	}

332
	trace_azx_get_position(chip, azx_dev, pos, delay);
333 334 335 336 337 338 339 340 341 342
	return pos;
}
EXPORT_SYMBOL_GPL(azx_get_position);

static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream)
{
	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
	struct azx *chip = apcm->chip;
	struct azx_dev *azx_dev = get_azx_dev(substream);
	return bytes_to_frames(substream->runtime,
343
			       azx_get_position(chip, azx_dev));
344 345
}

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
/*
 * azx_scale64: Scale base by mult/div while not overflowing sanely
 *
 * Derived from scale64_check_overflow in kernel/time/timekeeping.c
 *
 * The tmestamps for a 48Khz stream can overflow after (2^64/10^9)/48K which
 * is about 384307 ie ~4.5 days.
 *
 * This scales the calculation so that overflow will happen but after 2^64 /
 * 48000 secs, which is pretty large!
 *
 * In caln below:
 *	base may overflow, but since there isn’t any additional division
 *	performed on base it’s OK
 *	rem can’t overflow because both are 32-bit values
 */

#ifdef CONFIG_X86
static u64 azx_scale64(u64 base, u32 num, u32 den)
{
	u64 rem;

	rem = do_div(base, den);

	base *= num;
	rem *= num;

	do_div(rem, den);

	return base + rem;
}

static int azx_get_sync_time(ktime_t *device,
		struct system_counterval_t *system, void *ctx)
{
	struct snd_pcm_substream *substream = ctx;
	struct azx_dev *azx_dev = get_azx_dev(substream);
	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
	struct azx *chip = apcm->chip;
	struct snd_pcm_runtime *runtime;
	u64 ll_counter, ll_counter_l, ll_counter_h;
	u64 tsc_counter, tsc_counter_l, tsc_counter_h;
	u32 wallclk_ctr, wallclk_cycles;
	bool direction;
	u32 dma_select;
	u32 timeout = 200;
	u32 retry_count = 0;

	runtime = substream->runtime;

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		direction = 1;
	else
		direction = 0;

	/* 0th stream tag is not used, so DMA ch 0 is for 1st stream tag */
	do {
		timeout = 100;
		dma_select = (direction << GTSCC_CDMAS_DMA_DIR_SHIFT) |
					(azx_dev->core.stream_tag - 1);
		snd_hdac_chip_writel(azx_bus(chip), GTSCC, dma_select);

		/* Enable the capture */
		snd_hdac_chip_updatel(azx_bus(chip), GTSCC, 0, GTSCC_TSCCI_MASK);

		while (timeout) {
			if (snd_hdac_chip_readl(azx_bus(chip), GTSCC) &
						GTSCC_TSCCD_MASK)
				break;

			timeout--;
		}

		if (!timeout) {
			dev_err(chip->card->dev, "GTSCC capture Timedout!\n");
			return -EIO;
		}

		/* Read wall clock counter */
		wallclk_ctr = snd_hdac_chip_readl(azx_bus(chip), WALFCC);

		/* Read TSC counter */
		tsc_counter_l = snd_hdac_chip_readl(azx_bus(chip), TSCCL);
		tsc_counter_h = snd_hdac_chip_readl(azx_bus(chip), TSCCU);

		/* Read Link counter */
		ll_counter_l = snd_hdac_chip_readl(azx_bus(chip), LLPCL);
		ll_counter_h = snd_hdac_chip_readl(azx_bus(chip), LLPCU);

		/* Ack: registers read done */
		snd_hdac_chip_writel(azx_bus(chip), GTSCC, GTSCC_TSCCD_SHIFT);

		tsc_counter = (tsc_counter_h << TSCCU_CCU_SHIFT) |
						tsc_counter_l;

		ll_counter = (ll_counter_h << LLPC_CCU_SHIFT) |	ll_counter_l;
		wallclk_cycles = wallclk_ctr & WALFCC_CIF_MASK;

		/*
		 * An error occurs near frame "rollover". The clocks in
		 * frame value indicates whether this error may have
		 * occurred. Here we use the value of 10 i.e.,
		 * HDA_MAX_CYCLE_OFFSET
		 */
		if (wallclk_cycles < HDA_MAX_CYCLE_VALUE - HDA_MAX_CYCLE_OFFSET
					&& wallclk_cycles > HDA_MAX_CYCLE_OFFSET)
			break;

		/*
		 * Sleep before we read again, else we may again get
		 * value near to MAX_CYCLE. Try to sleep for different
		 * amount of time so we dont hit the same number again
		 */
		udelay(retry_count++);

	} while (retry_count != HDA_MAX_CYCLE_READ_RETRY);

	if (retry_count == HDA_MAX_CYCLE_READ_RETRY) {
		dev_err_ratelimited(chip->card->dev,
			"Error in WALFCC cycle count\n");
		return -EIO;
	}

	*device = ns_to_ktime(azx_scale64(ll_counter,
				NSEC_PER_SEC, runtime->rate));
	*device = ktime_add_ns(*device, (wallclk_cycles * NSEC_PER_SEC) /
			       ((HDA_MAX_CYCLE_VALUE + 1) * runtime->rate));

	*system = convert_art_to_tsc(tsc_counter);

	return 0;
}

#else
static int azx_get_sync_time(ktime_t *device,
		struct system_counterval_t *system, void *ctx)
{
	return -ENXIO;
}
#endif

static int azx_get_crosststamp(struct snd_pcm_substream *substream,
			      struct system_device_crosststamp *xtstamp)
{
	return get_device_system_crosststamp(azx_get_sync_time,
					substream, NULL, xtstamp);
}

static inline bool is_link_time_supported(struct snd_pcm_runtime *runtime,
				struct snd_pcm_audio_tstamp_config *ts)
{
	if (runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_SYNCHRONIZED_ATIME)
		if (ts->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK_SYNCHRONIZED)
			return true;

	return false;
}

504 505 506 507
static int azx_get_time_info(struct snd_pcm_substream *substream,
			struct timespec *system_ts, struct timespec *audio_ts,
			struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
			struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
508 509
{
	struct azx_dev *azx_dev = get_azx_dev(substream);
510 511 512
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct system_device_crosststamp xtstamp;
	int ret;
513 514
	u64 nsec;

515 516
	if ((substream->runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_ATIME) &&
		(audio_tstamp_config->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK)) {
517

518 519
		snd_pcm_gettime(substream->runtime, system_ts);

520
		nsec = timecounter_read(&azx_dev->core.tc);
521 522 523 524 525 526 527 528 529 530
		nsec = div_u64(nsec, 3); /* can be optimized */
		if (audio_tstamp_config->report_delay)
			nsec = azx_adjust_codec_delay(substream, nsec);

		*audio_ts = ns_to_timespec(nsec);

		audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
		audio_tstamp_report->accuracy_report = 1; /* rest of structure is valid */
		audio_tstamp_report->accuracy = 42; /* 24 MHz WallClock == 42ns resolution */

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	} else if (is_link_time_supported(runtime, audio_tstamp_config)) {

		ret = azx_get_crosststamp(substream, &xtstamp);
		if (ret)
			return ret;

		switch (runtime->tstamp_type) {
		case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC:
			return -EINVAL;

		case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC_RAW:
			*system_ts = ktime_to_timespec(xtstamp.sys_monoraw);
			break;

		default:
			*system_ts = ktime_to_timespec(xtstamp.sys_realtime);
			break;

		}

		*audio_ts = ktime_to_timespec(xtstamp.device);

		audio_tstamp_report->actual_type =
			SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK_SYNCHRONIZED;
		audio_tstamp_report->accuracy_report = 1;
		/* 24 MHz WallClock == 42ns resolution */
		audio_tstamp_report->accuracy = 42;

	} else {
560
		audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
561
	}
562 563 564 565 566 567 568 569 570 571 572 573 574

	return 0;
}

static struct snd_pcm_hardware azx_pcm_hw = {
	.info =			(SNDRV_PCM_INFO_MMAP |
				 SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
				 SNDRV_PCM_INFO_MMAP_VALID |
				 /* No full-resume yet implemented */
				 /* SNDRV_PCM_INFO_RESUME |*/
				 SNDRV_PCM_INFO_PAUSE |
				 SNDRV_PCM_INFO_SYNC_START |
575 576
				 SNDRV_PCM_INFO_HAS_WALL_CLOCK | /* legacy */
				 SNDRV_PCM_INFO_HAS_LINK_ATIME |
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
				 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
	.rates =		SNDRV_PCM_RATE_48000,
	.rate_min =		48000,
	.rate_max =		48000,
	.channels_min =		2,
	.channels_max =		2,
	.buffer_bytes_max =	AZX_MAX_BUF_SIZE,
	.period_bytes_min =	128,
	.period_bytes_max =	AZX_MAX_BUF_SIZE / 2,
	.periods_min =		2,
	.periods_max =		AZX_MAX_FRAG,
	.fifo_size =		0,
};

static int azx_pcm_open(struct snd_pcm_substream *substream)
{
	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
595
	struct hda_pcm_stream *hinfo = to_hda_pcm_stream(substream);
596 597 598 599 600 601
	struct azx *chip = apcm->chip;
	struct azx_dev *azx_dev;
	struct snd_pcm_runtime *runtime = substream->runtime;
	int err;
	int buff_step;

602
	snd_hda_codec_pcm_get(apcm->info);
603 604
	mutex_lock(&chip->open_mutex);
	azx_dev = azx_assign_device(chip, substream);
605
	trace_azx_pcm_open(chip, azx_dev);
606
	if (azx_dev == NULL) {
607 608
		err = -EBUSY;
		goto unlock;
609
	}
610
	runtime->private_data = azx_dev;
611

612
	runtime->hw = azx_pcm_hw;
613 614
	if (chip->gts_present)
		runtime->hw.info |= SNDRV_PCM_INFO_HAS_LINK_SYNCHRONIZED_ATIME;
615 616 617 618 619 620 621 622 623 624 625 626
	runtime->hw.channels_min = hinfo->channels_min;
	runtime->hw.channels_max = hinfo->channels_max;
	runtime->hw.formats = hinfo->formats;
	runtime->hw.rates = hinfo->rates;
	snd_pcm_limit_hw_rates(runtime);
	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);

	/* avoid wrap-around with wall-clock */
	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
				     20,
				     178000000);

627 628 629 630 631 632 633
	/* by some reason, the playback stream stalls on PulseAudio with
	 * tsched=1 when a capture stream triggers.  Until we figure out the
	 * real cause, disable tsched mode by telling the PCM info flag.
	 */
	if (chip->driver_caps & AZX_DCAPS_AMD_WORKAROUND)
		runtime->hw.info |= SNDRV_PCM_INFO_BATCH;

634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
	if (chip->align_buffer_size)
		/* constrain buffer sizes to be multiple of 128
		   bytes. This is more efficient in terms of memory
		   access but isn't required by the HDA spec and
		   prevents users from specifying exact period/buffer
		   sizes. For example for 44.1kHz, a period size set
		   to 20ms will be rounded to 19.59ms. */
		buff_step = 128;
	else
		/* Don't enforce steps on buffer sizes, still need to
		   be multiple of 4 bytes (HDA spec). Tested on Intel
		   HDA controllers, may not work on all devices where
		   option needs to be disabled */
		buff_step = 4;

	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
				   buff_step);
	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
				   buff_step);
653
	snd_hda_power_up(apcm->codec);
654 655 656 657
	if (hinfo->ops.open)
		err = hinfo->ops.open(hinfo, apcm->codec, substream);
	else
		err = -ENODEV;
658 659
	if (err < 0) {
		azx_release_device(azx_dev);
660
		goto powerdown;
661 662 663 664 665 666 667 668
	}
	snd_pcm_limit_hw_rates(runtime);
	/* sanity check */
	if (snd_BUG_ON(!runtime->hw.channels_min) ||
	    snd_BUG_ON(!runtime->hw.channels_max) ||
	    snd_BUG_ON(!runtime->hw.formats) ||
	    snd_BUG_ON(!runtime->hw.rates)) {
		azx_release_device(azx_dev);
669 670 671 672
		if (hinfo->ops.close)
			hinfo->ops.close(hinfo, apcm->codec, substream);
		err = -EINVAL;
		goto powerdown;
673 674
	}

675
	/* disable LINK_ATIME timestamps for capture streams
676
	   until we figure out how to handle digital inputs */
677 678 679 680
	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
		runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK; /* legacy */
		runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_LINK_ATIME;
	}
681 682 683 684

	snd_pcm_set_sync(substream);
	mutex_unlock(&chip->open_mutex);
	return 0;
685 686 687 688 689

 powerdown:
	snd_hda_power_down(apcm->codec);
 unlock:
	mutex_unlock(&chip->open_mutex);
690
	snd_hda_codec_pcm_put(apcm->info);
691
	return err;
692 693 694 695 696 697 698 699 700 701 702 703
}

static int azx_pcm_mmap(struct snd_pcm_substream *substream,
			struct vm_area_struct *area)
{
	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
	struct azx *chip = apcm->chip;
	if (chip->ops->pcm_mmap_prepare)
		chip->ops->pcm_mmap_prepare(substream, area);
	return snd_pcm_lib_default_mmap(substream, area);
}

704
static const struct snd_pcm_ops azx_pcm_ops = {
705 706 707 708 709 710 711 712
	.open = azx_pcm_open,
	.close = azx_pcm_close,
	.ioctl = snd_pcm_lib_ioctl,
	.hw_params = azx_pcm_hw_params,
	.hw_free = azx_pcm_hw_free,
	.prepare = azx_pcm_prepare,
	.trigger = azx_pcm_trigger,
	.pointer = azx_pcm_pointer,
713
	.get_time_info =  azx_get_time_info,
714 715 716 717 718 719 720 721 722
	.mmap = azx_pcm_mmap,
	.page = snd_pcm_sgbuf_ops_page,
};

static void azx_pcm_free(struct snd_pcm *pcm)
{
	struct azx_pcm *apcm = pcm->private_data;
	if (apcm) {
		list_del(&apcm->list);
723
		apcm->info->pcm = NULL;
724 725 726 727 728 729
		kfree(apcm);
	}
}

#define MAX_PREALLOC_SIZE	(32 * 1024 * 1024)

730 731
int snd_hda_attach_pcm_stream(struct hda_bus *_bus, struct hda_codec *codec,
			      struct hda_pcm *cpcm)
732
{
733 734
	struct hdac_bus *bus = &_bus->core;
	struct azx *chip = bus_to_azx(bus);
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
	struct snd_pcm *pcm;
	struct azx_pcm *apcm;
	int pcm_dev = cpcm->device;
	unsigned int size;
	int s, err;

	list_for_each_entry(apcm, &chip->pcm_list, list) {
		if (apcm->pcm->device == pcm_dev) {
			dev_err(chip->card->dev, "PCM %d already exists\n",
				pcm_dev);
			return -EBUSY;
		}
	}
	err = snd_pcm_new(chip->card, cpcm->name, pcm_dev,
			  cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
			  cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams,
			  &pcm);
	if (err < 0)
		return err;
	strlcpy(pcm->name, cpcm->name, sizeof(pcm->name));
	apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
756 757
	if (apcm == NULL) {
		snd_device_free(chip->card, pcm);
758
		return -ENOMEM;
759
	}
760 761 762
	apcm->chip = chip;
	apcm->pcm = pcm;
	apcm->codec = codec;
763
	apcm->info = cpcm;
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
	pcm->private_data = apcm;
	pcm->private_free = azx_pcm_free;
	if (cpcm->pcm_type == HDA_PCM_TYPE_MODEM)
		pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
	list_add_tail(&apcm->list, &chip->pcm_list);
	cpcm->pcm = pcm;
	for (s = 0; s < 2; s++) {
		if (cpcm->stream[s].substreams)
			snd_pcm_set_ops(pcm, s, &azx_pcm_ops);
	}
	/* buffer pre-allocation */
	size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
	if (size > MAX_PREALLOC_SIZE)
		size = MAX_PREALLOC_SIZE;
	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
					      chip->card->dev,
					      size, MAX_PREALLOC_SIZE);
	return 0;
}

784 785 786 787 788 789 790 791 792 793 794 795 796
static unsigned int azx_command_addr(u32 cmd)
{
	unsigned int addr = cmd >> 28;

	if (addr >= AZX_MAX_CODECS) {
		snd_BUG();
		addr = 0;
	}

	return addr;
}

/* receive a response */
797
static int azx_rirb_get_response(struct hdac_bus *bus, unsigned int addr,
798
				 unsigned int *res)
799
{
800 801
	struct azx *chip = bus_to_azx(bus);
	struct hda_bus *hbus = &chip->bus;
802 803 804 805 806 807 808 809
	unsigned long timeout;
	unsigned long loopcounter;
	int do_poll = 0;

 again:
	timeout = jiffies + msecs_to_jiffies(1000);

	for (loopcounter = 0;; loopcounter++) {
810 811 812 813
		spin_lock_irq(&bus->reg_lock);
		if (chip->polling_mode || do_poll)
			snd_hdac_bus_update_rirb(bus);
		if (!bus->rirb.cmds[addr]) {
814 815
			if (!do_poll)
				chip->poll_count = 0;
816
			if (res)
817 818
				*res = bus->rirb.res[addr]; /* the last value */
			spin_unlock_irq(&bus->reg_lock);
819
			return 0;
820
		}
821
		spin_unlock_irq(&bus->reg_lock);
822 823
		if (time_after(jiffies, timeout))
			break;
824
		if (hbus->needs_damn_long_delay || loopcounter > 3000)
825 826 827 828 829 830 831
			msleep(2); /* temporary workaround */
		else {
			udelay(10);
			cond_resched();
		}
	}

832
	if (hbus->no_response_fallback)
833
		return -EIO;
834 835 836 837

	if (!chip->polling_mode && chip->poll_count < 2) {
		dev_dbg(chip->card->dev,
			"azx_get_response timeout, polling the codec once: last cmd=0x%08x\n",
838
			bus->last_cmd[addr]);
839 840 841 842 843 844 845 846 847
		do_poll = 1;
		chip->poll_count++;
		goto again;
	}


	if (!chip->polling_mode) {
		dev_warn(chip->card->dev,
			 "azx_get_response timeout, switching to polling mode: last cmd=0x%08x\n",
848
			 bus->last_cmd[addr]);
849 850 851 852 853 854 855
		chip->polling_mode = 1;
		goto again;
	}

	if (chip->msi) {
		dev_warn(chip->card->dev,
			 "No response from codec, disabling MSI: last cmd=0x%08x\n",
856 857
			 bus->last_cmd[addr]);
		if (chip->ops->disable_msi_reset_irq &&
858 859
		    chip->ops->disable_msi_reset_irq(chip) < 0)
			return -EIO;
860 861 862 863 864 865 866 867
		goto again;
	}

	if (chip->probing) {
		/* If this critical timeout happens during the codec probing
		 * phase, this is likely an access to a non-existing codec
		 * slot.  Better to return an error and reset the system.
		 */
868
		return -EIO;
869 870
	}

871 872 873 874
	/* no fallback mechanism? */
	if (!chip->fallback_to_single_cmd)
		return -EIO;

875 876 877
	/* a fatal communication error; need either to reset or to fallback
	 * to the single_cmd mode
	 */
878 879
	if (hbus->allow_bus_reset && !hbus->response_reset && !hbus->in_reset) {
		hbus->response_reset = 1;
880
		return -EAGAIN; /* give a chance to retry */
881 882 883 884
	}

	dev_err(chip->card->dev,
		"azx_get_response timeout, switching to single_cmd mode: last cmd=0x%08x\n",
885
		bus->last_cmd[addr]);
886
	chip->single_cmd = 1;
887 888
	hbus->response_reset = 0;
	snd_hdac_bus_stop_cmd_io(bus);
889
	return -EIO;
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
}

/*
 * Use the single immediate command instead of CORB/RIRB for simplicity
 *
 * Note: according to Intel, this is not preferred use.  The command was
 *       intended for the BIOS only, and may get confused with unsolicited
 *       responses.  So, we shouldn't use it for normal operation from the
 *       driver.
 *       I left the codes, however, for debugging/testing purposes.
 */

/* receive a response */
static int azx_single_wait_for_response(struct azx *chip, unsigned int addr)
{
	int timeout = 50;

	while (timeout--) {
		/* check IRV busy bit */
T
Takashi Iwai 已提交
909
		if (azx_readw(chip, IRS) & AZX_IRS_VALID) {
910
			/* reuse rirb.res as the response return value */
911
			azx_bus(chip)->rirb.res[addr] = azx_readl(chip, IR);
912 913 914 915 916 917 918
			return 0;
		}
		udelay(1);
	}
	if (printk_ratelimit())
		dev_dbg(chip->card->dev, "get_response timeout: IRS=0x%x\n",
			azx_readw(chip, IRS));
919
	azx_bus(chip)->rirb.res[addr] = -1;
920 921 922 923
	return -EIO;
}

/* send a command */
924
static int azx_single_send_cmd(struct hdac_bus *bus, u32 val)
925
{
926
	struct azx *chip = bus_to_azx(bus);
927 928 929
	unsigned int addr = azx_command_addr(val);
	int timeout = 50;

930
	bus->last_cmd[azx_command_addr(val)] = val;
931 932
	while (timeout--) {
		/* check ICB busy bit */
T
Takashi Iwai 已提交
933
		if (!((azx_readw(chip, IRS) & AZX_IRS_BUSY))) {
934 935
			/* Clear IRV valid bit */
			azx_writew(chip, IRS, azx_readw(chip, IRS) |
T
Takashi Iwai 已提交
936
				   AZX_IRS_VALID);
937 938
			azx_writel(chip, IC, val);
			azx_writew(chip, IRS, azx_readw(chip, IRS) |
T
Takashi Iwai 已提交
939
				   AZX_IRS_BUSY);
940 941 942 943 944 945 946 947 948 949 950 951
			return azx_single_wait_for_response(chip, addr);
		}
		udelay(1);
	}
	if (printk_ratelimit())
		dev_dbg(chip->card->dev,
			"send_cmd timeout: IRS=0x%x, val=0x%x\n",
			azx_readw(chip, IRS), val);
	return -EIO;
}

/* receive a response */
952
static int azx_single_get_response(struct hdac_bus *bus, unsigned int addr,
953
				   unsigned int *res)
954
{
955
	if (res)
956
		*res = bus->rirb.res[addr];
957
	return 0;
958 959 960 961 962 963 964 965 966 967
}

/*
 * The below are the main callbacks from hda_codec.
 *
 * They are just the skeleton to call sub-callbacks according to the
 * current setting of chip->single_cmd.
 */

/* send a command */
968
static int azx_send_cmd(struct hdac_bus *bus, unsigned int val)
969
{
970
	struct azx *chip = bus_to_azx(bus);
971 972 973 974 975 976

	if (chip->disabled)
		return 0;
	if (chip->single_cmd)
		return azx_single_send_cmd(bus, val);
	else
977
		return snd_hdac_bus_send_cmd(bus, val);
978 979 980
}

/* get a response */
981
static int azx_get_response(struct hdac_bus *bus, unsigned int addr,
982
			    unsigned int *res)
983
{
984 985
	struct azx *chip = bus_to_azx(bus);

986 987 988
	if (chip->disabled)
		return 0;
	if (chip->single_cmd)
989
		return azx_single_get_response(bus, addr, res);
990
	else
991
		return azx_rirb_get_response(bus, addr, res);
992 993
}

994 995 996 997 998 999 1000 1001 1002 1003
static int azx_link_power(struct hdac_bus *bus, bool enable)
{
	struct azx *chip = bus_to_azx(bus);

	if (chip->ops->link_power)
		return chip->ops->link_power(chip, enable);
	else
		return -EINVAL;
}

1004 1005 1006
static const struct hdac_bus_ops bus_core_ops = {
	.command = azx_send_cmd,
	.get_response = azx_get_response,
1007
	.link_power = azx_link_power,
1008 1009
};

1010 1011 1012 1013 1014 1015 1016 1017 1018
#ifdef CONFIG_SND_HDA_DSP_LOADER
/*
 * DSP loading code (e.g. for CA0132)
 */

/* use the first stream for loading DSP */
static struct azx_dev *
azx_get_dsp_loader_dev(struct azx *chip)
{
1019 1020 1021 1022 1023 1024 1025 1026
	struct hdac_bus *bus = azx_bus(chip);
	struct hdac_stream *s;

	list_for_each_entry(s, &bus->stream_list, list)
		if (s->index == chip->playback_index_offset)
			return stream_to_azx_dev(s);

	return NULL;
1027 1028
}

1029 1030 1031
int snd_hda_codec_load_dsp_prepare(struct hda_codec *codec, unsigned int format,
				   unsigned int byte_size,
				   struct snd_dma_buffer *bufp)
1032
{
1033
	struct hdac_bus *bus = &codec->bus->core;
1034
	struct azx *chip = bus_to_azx(bus);
1035
	struct azx_dev *azx_dev;
1036 1037
	struct hdac_stream *hstr;
	bool saved = false;
1038 1039 1040
	int err;

	azx_dev = azx_get_dsp_loader_dev(chip);
1041
	hstr = azx_stream(azx_dev);
1042
	spin_lock_irq(&bus->reg_lock);
1043 1044 1045
	if (hstr->opened) {
		chip->saved_azx_dev = *azx_dev;
		saved = true;
1046
	}
1047
	spin_unlock_irq(&bus->reg_lock);
1048

1049 1050
	err = snd_hdac_dsp_prepare(hstr, format, byte_size, bufp);
	if (err < 0) {
1051
		spin_lock_irq(&bus->reg_lock);
1052 1053
		if (saved)
			*azx_dev = chip->saved_azx_dev;
1054
		spin_unlock_irq(&bus->reg_lock);
1055 1056
		return err;
	}
1057

1058
	hstr->prepared = 0;
1059 1060
	return err;
}
1061
EXPORT_SYMBOL_GPL(snd_hda_codec_load_dsp_prepare);
1062

1063
void snd_hda_codec_load_dsp_trigger(struct hda_codec *codec, bool start)
1064
{
1065
	struct hdac_bus *bus = &codec->bus->core;
1066
	struct azx *chip = bus_to_azx(bus);
1067 1068
	struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);

1069
	snd_hdac_dsp_trigger(azx_stream(azx_dev), start);
1070
}
1071
EXPORT_SYMBOL_GPL(snd_hda_codec_load_dsp_trigger);
1072

1073 1074
void snd_hda_codec_load_dsp_cleanup(struct hda_codec *codec,
				    struct snd_dma_buffer *dmab)
1075
{
1076
	struct hdac_bus *bus = &codec->bus->core;
1077
	struct azx *chip = bus_to_azx(bus);
1078
	struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
1079
	struct hdac_stream *hstr = azx_stream(azx_dev);
1080

1081
	if (!dmab->area || !hstr->locked)
1082 1083
		return;

1084
	snd_hdac_dsp_cleanup(hstr, dmab);
1085
	spin_lock_irq(&bus->reg_lock);
1086
	if (hstr->opened)
1087
		*azx_dev = chip->saved_azx_dev;
1088
	hstr->locked = false;
1089
	spin_unlock_irq(&bus->reg_lock);
1090
}
1091
EXPORT_SYMBOL_GPL(snd_hda_codec_load_dsp_cleanup);
1092 1093
#endif /* CONFIG_SND_HDA_DSP_LOADER */

1094 1095 1096
/*
 * reset and start the controller registers
 */
1097
void azx_init_chip(struct azx *chip, bool full_reset)
1098
{
1099 1100 1101 1102 1103
	if (snd_hdac_bus_init_chip(azx_bus(chip), full_reset)) {
		/* correct RINTCNT for CXT */
		if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND)
			azx_writew(chip, RINTCNT, 0xc0);
	}
1104 1105 1106
}
EXPORT_SYMBOL_GPL(azx_init_chip);

1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
void azx_stop_all_streams(struct azx *chip)
{
	struct hdac_bus *bus = azx_bus(chip);
	struct hdac_stream *s;

	list_for_each_entry(s, &bus->stream_list, list)
		snd_hdac_stream_stop(s);
}
EXPORT_SYMBOL_GPL(azx_stop_all_streams);

1117 1118
void azx_stop_chip(struct azx *chip)
{
1119
	snd_hdac_bus_stop_chip(azx_bus(chip));
1120
}
1121
EXPORT_SYMBOL_GPL(azx_stop_chip);
1122

1123 1124 1125
/*
 * interrupt handler
 */
1126 1127
static void stream_update(struct hdac_bus *bus, struct hdac_stream *s)
{
1128
	struct azx *chip = bus_to_azx(bus);
1129 1130 1131 1132 1133
	struct azx_dev *azx_dev = stream_to_azx_dev(s);

	/* check whether this IRQ is really acceptable */
	if (!chip->ops->position_check ||
	    chip->ops->position_check(chip, azx_dev)) {
1134 1135 1136
		spin_unlock(&bus->reg_lock);
		snd_pcm_period_elapsed(azx_stream(azx_dev)->substream);
		spin_lock(&bus->reg_lock);
1137 1138 1139
	}
}

1140 1141 1142
irqreturn_t azx_interrupt(int irq, void *dev_id)
{
	struct azx *chip = dev_id;
1143
	struct hdac_bus *bus = azx_bus(chip);
1144
	u32 status;
1145 1146
	bool active, handled = false;
	int repeat = 0; /* count for avoiding endless loop */
1147

1148
#ifdef CONFIG_PM
1149
	if (azx_has_pm_runtime(chip))
1150
		if (!pm_runtime_active(chip->card->dev))
1151 1152 1153
			return IRQ_NONE;
#endif

1154
	spin_lock(&bus->reg_lock);
1155

1156 1157
	if (chip->disabled)
		goto unlock;
1158

1159 1160 1161 1162
	do {
		status = azx_readl(chip, INTSTS);
		if (status == 0 || status == 0xffffffff)
			break;
1163

1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
		handled = true;
		active = false;
		if (snd_hdac_bus_handle_stream_irq(bus, status, stream_update))
			active = true;

		/* clear rirb int */
		status = azx_readb(chip, RIRBSTS);
		if (status & RIRB_INT_MASK) {
			active = true;
			if (status & RIRB_INT_RESPONSE) {
				if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND)
					udelay(80);
				snd_hdac_bus_update_rirb(bus);
			}
			azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
1179
		}
1180
	} while (active && ++repeat < 10);
1181

1182
 unlock:
1183
	spin_unlock(&bus->reg_lock);
1184

1185
	return IRQ_RETVAL(handled);
1186 1187 1188
}
EXPORT_SYMBOL_GPL(azx_interrupt);

1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
/*
 * Codec initerface
 */

/*
 * Probe the given codec address
 */
static int probe_codec(struct azx *chip, int addr)
{
	unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
		(AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
1200
	struct hdac_bus *bus = azx_bus(chip);
1201
	int err;
1202
	unsigned int res = -1;
1203

1204
	mutex_lock(&bus->cmd_mutex);
1205
	chip->probing = 1;
1206 1207
	azx_send_cmd(bus, cmd);
	err = azx_get_response(bus, addr, &res);
1208
	chip->probing = 0;
1209
	mutex_unlock(&bus->cmd_mutex);
1210
	if (err < 0 || res == -1)
1211 1212 1213 1214 1215
		return -EIO;
	dev_dbg(chip->card->dev, "codec #%d probed OK\n", addr);
	return 0;
}

1216
void snd_hda_bus_reset(struct hda_bus *bus)
1217
{
1218
	struct azx *chip = bus_to_azx(&bus->core);
1219 1220 1221

	bus->in_reset = 1;
	azx_stop_chip(chip);
1222
	azx_init_chip(chip, true);
1223
	if (bus->core.chip_init)
1224
		snd_hda_bus_reset_codecs(bus);
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	bus->in_reset = 0;
}

static int get_jackpoll_interval(struct azx *chip)
{
	int i;
	unsigned int j;

	if (!chip->jackpoll_ms)
		return 0;

	i = chip->jackpoll_ms[chip->dev_index];
	if (i == 0)
		return 0;
	if (i < 50 || i > 60000)
		j = 0;
	else
		j = msecs_to_jiffies(i);
	if (j == 0)
		dev_warn(chip->card->dev,
			 "jackpoll_ms value out of range: %d\n", i);
	return j;
}

1249
/* HD-audio bus initialization */
1250 1251
int azx_bus_init(struct azx *chip, const char *model,
		 const struct hdac_io_ops *io_ops)
1252
{
1253
	struct hda_bus *bus = &chip->bus;
1254
	int err;
1255

1256 1257
	err = snd_hdac_bus_init(&bus->core, chip->card->dev, &bus_core_ops,
				io_ops);
1258 1259 1260
	if (err < 0)
		return err;

1261 1262
	bus->card = chip->card;
	mutex_init(&bus->prepare_mutex);
1263 1264
	bus->pci = chip->pci;
	bus->modelname = model;
1265
	bus->mixer_assigned = -1;
1266 1267 1268 1269
	bus->core.snoop = azx_snoop(chip);
	if (chip->get_position[0] != azx_get_pos_lpib ||
	    chip->get_position[1] != azx_get_pos_lpib)
		bus->core.use_posbuf = true;
1270
	bus->core.bdl_pos_adj = chip->bdl_pos_adj;
1271 1272
	if (chip->driver_caps & AZX_DCAPS_CORBRP_SELF_CLEAR)
		bus->core.corbrp_self_clear = true;
1273

1274 1275 1276
	if (chip->driver_caps & AZX_DCAPS_4K_BDLE_BOUNDARY)
		bus->core.align_bdle_4k = true;

1277 1278 1279 1280 1281 1282
	/* AMD chipsets often cause the communication stalls upon certain
	 * sequence like the pin-detection.  It seems that forcing the synced
	 * access works around the stall.  Grrr...
	 */
	if (chip->driver_caps & AZX_DCAPS_SYNC_WRITE) {
		dev_dbg(chip->card->dev, "Enable sync_write for stable communication\n");
1283
		bus->core.sync_write = 1;
1284 1285 1286 1287 1288
		bus->allow_bus_reset = 1;
	}

	return 0;
}
1289
EXPORT_SYMBOL_GPL(azx_bus_init);
1290 1291 1292 1293

/* Probe codecs */
int azx_probe_codecs(struct azx *chip, unsigned int max_slots)
{
1294
	struct hdac_bus *bus = azx_bus(chip);
1295 1296
	int c, codecs, err;

1297 1298 1299 1300 1301 1302
	codecs = 0;
	if (!max_slots)
		max_slots = AZX_DEFAULT_CODECS;

	/* First try to probe all given codec slots */
	for (c = 0; c < max_slots; c++) {
1303
		if ((bus->codec_mask & (1 << c)) & chip->codec_probe_mask) {
1304 1305 1306 1307 1308 1309
			if (probe_codec(chip, c) < 0) {
				/* Some BIOSen give you wrong codec addresses
				 * that don't exist
				 */
				dev_warn(chip->card->dev,
					 "Codec #%d probe error; disabling it...\n", c);
1310
				bus->codec_mask &= ~(1 << c);
1311 1312 1313 1314 1315 1316 1317 1318
				/* More badly, accessing to a non-existing
				 * codec often screws up the controller chip,
				 * and disturbs the further communications.
				 * Thus if an error occurs during probing,
				 * better to reset the controller chip to
				 * get back to the sanity state.
				 */
				azx_stop_chip(chip);
1319
				azx_init_chip(chip, true);
1320 1321 1322 1323 1324 1325
			}
		}
	}

	/* Then create codec instances */
	for (c = 0; c < max_slots; c++) {
1326
		if ((bus->codec_mask & (1 << c)) & chip->codec_probe_mask) {
1327
			struct hda_codec *codec;
1328
			err = snd_hda_codec_new(&chip->bus, chip->card, c, &codec);
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
			if (err < 0)
				continue;
			codec->jackpoll_interval = get_jackpoll_interval(chip);
			codec->beep_mode = chip->beep_mode;
			codecs++;
		}
	}
	if (!codecs) {
		dev_err(chip->card->dev, "no codecs initialized\n");
		return -ENXIO;
	}
	return 0;
}
1342
EXPORT_SYMBOL_GPL(azx_probe_codecs);
1343 1344 1345 1346

/* configure each codec instance */
int azx_codec_configure(struct azx *chip)
{
1347 1348 1349 1350 1351 1352
	struct hda_codec *codec, *next;

	/* use _safe version here since snd_hda_codec_configure() deregisters
	 * the device upon error and deletes itself from the bus list.
	 */
	list_for_each_codec_safe(codec, next, &chip->bus) {
1353 1354
		snd_hda_codec_configure(codec);
	}
1355 1356 1357

	if (!azx_bus(chip)->num_codecs)
		return -ENODEV;
1358 1359 1360 1361
	return 0;
}
EXPORT_SYMBOL_GPL(azx_codec_configure);

1362
static int stream_direction(struct azx *chip, unsigned char index)
1363
{
1364 1365 1366 1367
	if (index >= chip->capture_index_offset &&
	    index < chip->capture_index_offset + chip->capture_streams)
		return SNDRV_PCM_STREAM_CAPTURE;
	return SNDRV_PCM_STREAM_PLAYBACK;
1368 1369
}

1370
/* initialize SD streams */
1371
int azx_init_streams(struct azx *chip)
1372 1373
{
	int i;
1374
	int stream_tags[2] = { 0, 0 };
1375 1376 1377 1378 1379 1380

	/* initialize each stream (aka device)
	 * assign the starting bdl address to each stream (device)
	 * and initialize
	 */
	for (i = 0; i < chip->num_streams; i++) {
1381 1382 1383 1384 1385
		struct azx_dev *azx_dev = kzalloc(sizeof(*azx_dev), GFP_KERNEL);
		int dir, tag;

		if (!azx_dev)
			return -ENOMEM;
1386

1387
		dir = stream_direction(chip, i);
1388 1389 1390 1391 1392 1393 1394
		/* stream tag must be unique throughout
		 * the stream direction group,
		 * valid values 1...15
		 * use separate stream tag if the flag
		 * AZX_DCAPS_SEPARATE_STREAM_TAG is used
		 */
		if (chip->driver_caps & AZX_DCAPS_SEPARATE_STREAM_TAG)
1395
			tag = ++stream_tags[dir];
1396
		else
1397 1398 1399
			tag = i + 1;
		snd_hdac_stream_init(azx_bus(chip), azx_stream(azx_dev),
				     i, dir, tag);
1400 1401 1402 1403
	}

	return 0;
}
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
EXPORT_SYMBOL_GPL(azx_init_streams);

void azx_free_streams(struct azx *chip)
{
	struct hdac_bus *bus = azx_bus(chip);
	struct hdac_stream *s;

	while (!list_empty(&bus->stream_list)) {
		s = list_first_entry(&bus->stream_list, struct hdac_stream, list);
		list_del(&s->list);
		kfree(stream_to_azx_dev(s));
	}
}
EXPORT_SYMBOL_GPL(azx_free_streams);