Dylan Reid | 05e8487 | 2014-02-28 15:41:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Implementation of primary alsa driver code base for Intel HD Audio. |
| 4 | * |
| 5 | * Copyright(c) 2004 Intel Corporation. All rights reserved. |
| 6 | * |
| 7 | * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de> |
| 8 | * PeiSen Hou <pshou@realtek.com.tw> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the Free |
| 12 | * Software Foundation; either version 2 of the License, or (at your option) |
| 13 | * any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 18 | * more details. |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <linux/clocksource.h> |
| 24 | #include <linux/delay.h> |
Dylan Reid | f0b1df8 | 2014-02-28 15:41:29 -0800 | [diff] [blame^] | 25 | #include <linux/interrupt.h> |
Dylan Reid | 05e8487 | 2014-02-28 15:41:22 -0800 | [diff] [blame] | 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <sound/core.h> |
| 30 | #include <sound/initval.h> |
| 31 | #include "hda_priv.h" |
| 32 | #include "hda_controller.h" |
| 33 | |
| 34 | #define CREATE_TRACE_POINTS |
| 35 | #include "hda_intel_trace.h" |
| 36 | |
Dylan Reid | 2b5fd6c | 2014-02-28 15:41:24 -0800 | [diff] [blame] | 37 | /* DSP lock helpers */ |
| 38 | #ifdef CONFIG_SND_HDA_DSP_LOADER |
| 39 | #define dsp_lock_init(dev) mutex_init(&(dev)->dsp_mutex) |
| 40 | #define dsp_lock(dev) mutex_lock(&(dev)->dsp_mutex) |
| 41 | #define dsp_unlock(dev) mutex_unlock(&(dev)->dsp_mutex) |
| 42 | #define dsp_is_locked(dev) ((dev)->locked) |
| 43 | #else |
| 44 | #define dsp_lock_init(dev) do {} while (0) |
| 45 | #define dsp_lock(dev) do {} while (0) |
| 46 | #define dsp_unlock(dev) do {} while (0) |
| 47 | #define dsp_is_locked(dev) 0 |
| 48 | #endif |
| 49 | |
Dylan Reid | 05e8487 | 2014-02-28 15:41:22 -0800 | [diff] [blame] | 50 | /* |
| 51 | * AZX stream operations. |
| 52 | */ |
| 53 | |
| 54 | /* start a stream */ |
Dylan Reid | 2b5fd6c | 2014-02-28 15:41:24 -0800 | [diff] [blame] | 55 | static void azx_stream_start(struct azx *chip, struct azx_dev *azx_dev) |
Dylan Reid | 05e8487 | 2014-02-28 15:41:22 -0800 | [diff] [blame] | 56 | { |
| 57 | /* |
| 58 | * Before stream start, initialize parameter |
| 59 | */ |
| 60 | azx_dev->insufficient = 1; |
| 61 | |
| 62 | /* enable SIE */ |
| 63 | azx_writel(chip, INTCTL, |
| 64 | azx_readl(chip, INTCTL) | (1 << azx_dev->index)); |
| 65 | /* set DMA start and interrupt mask */ |
| 66 | azx_sd_writeb(chip, azx_dev, SD_CTL, |
| 67 | azx_sd_readb(chip, azx_dev, SD_CTL) | |
| 68 | SD_CTL_DMA_START | SD_INT_MASK); |
| 69 | } |
Dylan Reid | 05e8487 | 2014-02-28 15:41:22 -0800 | [diff] [blame] | 70 | |
| 71 | /* stop DMA */ |
| 72 | static void azx_stream_clear(struct azx *chip, struct azx_dev *azx_dev) |
| 73 | { |
| 74 | azx_sd_writeb(chip, azx_dev, SD_CTL, |
| 75 | azx_sd_readb(chip, azx_dev, SD_CTL) & |
| 76 | ~(SD_CTL_DMA_START | SD_INT_MASK)); |
| 77 | azx_sd_writeb(chip, azx_dev, SD_STS, SD_INT_MASK); /* to be sure */ |
| 78 | } |
| 79 | |
| 80 | /* stop a stream */ |
| 81 | void azx_stream_stop(struct azx *chip, struct azx_dev *azx_dev) |
| 82 | { |
| 83 | azx_stream_clear(chip, azx_dev); |
| 84 | /* disable SIE */ |
| 85 | azx_writel(chip, INTCTL, |
| 86 | azx_readl(chip, INTCTL) & ~(1 << azx_dev->index)); |
| 87 | } |
| 88 | EXPORT_SYMBOL_GPL(azx_stream_stop); |
| 89 | |
| 90 | /* reset stream */ |
Dylan Reid | 2b5fd6c | 2014-02-28 15:41:24 -0800 | [diff] [blame] | 91 | static void azx_stream_reset(struct azx *chip, struct azx_dev *azx_dev) |
Dylan Reid | 05e8487 | 2014-02-28 15:41:22 -0800 | [diff] [blame] | 92 | { |
| 93 | unsigned char val; |
| 94 | int timeout; |
| 95 | |
| 96 | azx_stream_clear(chip, azx_dev); |
| 97 | |
| 98 | azx_sd_writeb(chip, azx_dev, SD_CTL, |
| 99 | azx_sd_readb(chip, azx_dev, SD_CTL) | |
| 100 | SD_CTL_STREAM_RESET); |
| 101 | udelay(3); |
| 102 | timeout = 300; |
| 103 | while (!((val = azx_sd_readb(chip, azx_dev, SD_CTL)) & |
| 104 | SD_CTL_STREAM_RESET) && --timeout) |
| 105 | ; |
| 106 | val &= ~SD_CTL_STREAM_RESET; |
| 107 | azx_sd_writeb(chip, azx_dev, SD_CTL, val); |
| 108 | udelay(3); |
| 109 | |
| 110 | timeout = 300; |
| 111 | /* waiting for hardware to report that the stream is out of reset */ |
| 112 | while (((val = azx_sd_readb(chip, azx_dev, SD_CTL)) & |
| 113 | SD_CTL_STREAM_RESET) && --timeout) |
| 114 | ; |
| 115 | |
| 116 | /* reset first position - may not be synced with hw at this time */ |
| 117 | *azx_dev->posbuf = 0; |
| 118 | } |
Dylan Reid | 05e8487 | 2014-02-28 15:41:22 -0800 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * set up the SD for streaming |
| 122 | */ |
Dylan Reid | 2b5fd6c | 2014-02-28 15:41:24 -0800 | [diff] [blame] | 123 | static int azx_setup_controller(struct azx *chip, struct azx_dev *azx_dev) |
Dylan Reid | 05e8487 | 2014-02-28 15:41:22 -0800 | [diff] [blame] | 124 | { |
| 125 | unsigned int val; |
| 126 | /* make sure the run bit is zero for SD */ |
| 127 | azx_stream_clear(chip, azx_dev); |
| 128 | /* program the stream_tag */ |
| 129 | val = azx_sd_readl(chip, azx_dev, SD_CTL); |
| 130 | val = (val & ~SD_CTL_STREAM_TAG_MASK) | |
| 131 | (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT); |
| 132 | if (!azx_snoop(chip)) |
| 133 | val |= SD_CTL_TRAFFIC_PRIO; |
| 134 | azx_sd_writel(chip, azx_dev, SD_CTL, val); |
| 135 | |
| 136 | /* program the length of samples in cyclic buffer */ |
| 137 | azx_sd_writel(chip, azx_dev, SD_CBL, azx_dev->bufsize); |
| 138 | |
| 139 | /* program the stream format */ |
| 140 | /* this value needs to be the same as the one programmed */ |
| 141 | azx_sd_writew(chip, azx_dev, SD_FORMAT, azx_dev->format_val); |
| 142 | |
| 143 | /* program the stream LVI (last valid index) of the BDL */ |
| 144 | azx_sd_writew(chip, azx_dev, SD_LVI, azx_dev->frags - 1); |
| 145 | |
| 146 | /* program the BDL address */ |
| 147 | /* lower BDL address */ |
| 148 | azx_sd_writel(chip, azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr); |
| 149 | /* upper BDL address */ |
| 150 | azx_sd_writel(chip, azx_dev, SD_BDLPU, |
| 151 | upper_32_bits(azx_dev->bdl.addr)); |
| 152 | |
| 153 | /* enable the position buffer */ |
| 154 | if (chip->position_fix[0] != POS_FIX_LPIB || |
| 155 | chip->position_fix[1] != POS_FIX_LPIB) { |
| 156 | if (!(azx_readl(chip, DPLBASE) & ICH6_DPLBASE_ENABLE)) |
| 157 | azx_writel(chip, DPLBASE, |
| 158 | (u32)chip->posbuf.addr | ICH6_DPLBASE_ENABLE); |
| 159 | } |
| 160 | |
| 161 | /* set the interrupt enable bits in the descriptor control register */ |
| 162 | azx_sd_writel(chip, azx_dev, SD_CTL, |
| 163 | azx_sd_readl(chip, azx_dev, SD_CTL) | SD_INT_MASK); |
| 164 | |
| 165 | return 0; |
| 166 | } |
Dylan Reid | 05e8487 | 2014-02-28 15:41:22 -0800 | [diff] [blame] | 167 | |
| 168 | /* assign a stream for the PCM */ |
| 169 | static inline struct azx_dev * |
| 170 | azx_assign_device(struct azx *chip, struct snd_pcm_substream *substream) |
| 171 | { |
| 172 | int dev, i, nums; |
| 173 | struct azx_dev *res = NULL; |
| 174 | /* make a non-zero unique key for the substream */ |
| 175 | int key = (substream->pcm->device << 16) | (substream->number << 2) | |
| 176 | (substream->stream + 1); |
| 177 | |
| 178 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 179 | dev = chip->playback_index_offset; |
| 180 | nums = chip->playback_streams; |
| 181 | } else { |
| 182 | dev = chip->capture_index_offset; |
| 183 | nums = chip->capture_streams; |
| 184 | } |
| 185 | for (i = 0; i < nums; i++, dev++) { |
| 186 | struct azx_dev *azx_dev = &chip->azx_dev[dev]; |
| 187 | dsp_lock(azx_dev); |
| 188 | if (!azx_dev->opened && !dsp_is_locked(azx_dev)) { |
| 189 | res = azx_dev; |
| 190 | if (res->assigned_key == key) { |
| 191 | res->opened = 1; |
| 192 | res->assigned_key = key; |
| 193 | dsp_unlock(azx_dev); |
| 194 | return azx_dev; |
| 195 | } |
| 196 | } |
| 197 | dsp_unlock(azx_dev); |
| 198 | } |
| 199 | if (res) { |
| 200 | dsp_lock(res); |
| 201 | res->opened = 1; |
| 202 | res->assigned_key = key; |
| 203 | dsp_unlock(res); |
| 204 | } |
| 205 | return res; |
| 206 | } |
| 207 | |
| 208 | /* release the assigned stream */ |
| 209 | static inline void azx_release_device(struct azx_dev *azx_dev) |
| 210 | { |
| 211 | azx_dev->opened = 0; |
| 212 | } |
| 213 | |
| 214 | static cycle_t azx_cc_read(const struct cyclecounter *cc) |
| 215 | { |
| 216 | struct azx_dev *azx_dev = container_of(cc, struct azx_dev, azx_cc); |
| 217 | struct snd_pcm_substream *substream = azx_dev->substream; |
| 218 | struct azx_pcm *apcm = snd_pcm_substream_chip(substream); |
| 219 | struct azx *chip = apcm->chip; |
| 220 | |
| 221 | return azx_readl(chip, WALLCLK); |
| 222 | } |
| 223 | |
| 224 | static void azx_timecounter_init(struct snd_pcm_substream *substream, |
| 225 | bool force, cycle_t last) |
| 226 | { |
| 227 | struct azx_dev *azx_dev = get_azx_dev(substream); |
| 228 | struct timecounter *tc = &azx_dev->azx_tc; |
| 229 | struct cyclecounter *cc = &azx_dev->azx_cc; |
| 230 | u64 nsec; |
| 231 | |
| 232 | cc->read = azx_cc_read; |
| 233 | cc->mask = CLOCKSOURCE_MASK(32); |
| 234 | |
| 235 | /* |
| 236 | * Converting from 24 MHz to ns means applying a 125/3 factor. |
| 237 | * To avoid any saturation issues in intermediate operations, |
| 238 | * the 125 factor is applied first. The division is applied |
| 239 | * last after reading the timecounter value. |
| 240 | * Applying the 1/3 factor as part of the multiplication |
| 241 | * requires at least 20 bits for a decent precision, however |
| 242 | * overflows occur after about 4 hours or less, not a option. |
| 243 | */ |
| 244 | |
| 245 | cc->mult = 125; /* saturation after 195 years */ |
| 246 | cc->shift = 0; |
| 247 | |
| 248 | nsec = 0; /* audio time is elapsed time since trigger */ |
| 249 | timecounter_init(tc, cc, nsec); |
| 250 | if (force) |
| 251 | /* |
| 252 | * force timecounter to use predefined value, |
| 253 | * used for synchronized starts |
| 254 | */ |
| 255 | tc->cycle_last = last; |
| 256 | } |
| 257 | |
| 258 | static u64 azx_adjust_codec_delay(struct snd_pcm_substream *substream, |
| 259 | u64 nsec) |
| 260 | { |
| 261 | struct azx_pcm *apcm = snd_pcm_substream_chip(substream); |
| 262 | struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream]; |
| 263 | u64 codec_frames, codec_nsecs; |
| 264 | |
| 265 | if (!hinfo->ops.get_delay) |
| 266 | return nsec; |
| 267 | |
| 268 | codec_frames = hinfo->ops.get_delay(hinfo, apcm->codec, substream); |
| 269 | codec_nsecs = div_u64(codec_frames * 1000000000LL, |
| 270 | substream->runtime->rate); |
| 271 | |
| 272 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 273 | return nsec + codec_nsecs; |
| 274 | |
| 275 | return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * set up a BDL entry |
| 280 | */ |
Dylan Reid | 2b5fd6c | 2014-02-28 15:41:24 -0800 | [diff] [blame] | 281 | static int setup_bdle(struct azx *chip, |
| 282 | struct snd_dma_buffer *dmab, |
| 283 | struct azx_dev *azx_dev, u32 **bdlp, |
| 284 | int ofs, int size, int with_ioc) |
Dylan Reid | 05e8487 | 2014-02-28 15:41:22 -0800 | [diff] [blame] | 285 | { |
| 286 | u32 *bdl = *bdlp; |
| 287 | |
| 288 | while (size > 0) { |
| 289 | dma_addr_t addr; |
| 290 | int chunk; |
| 291 | |
| 292 | if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES) |
| 293 | return -EINVAL; |
| 294 | |
| 295 | addr = snd_sgbuf_get_addr(dmab, ofs); |
| 296 | /* program the address field of the BDL entry */ |
| 297 | bdl[0] = cpu_to_le32((u32)addr); |
| 298 | bdl[1] = cpu_to_le32(upper_32_bits(addr)); |
| 299 | /* program the size field of the BDL entry */ |
| 300 | chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size); |
| 301 | /* one BDLE cannot cross 4K boundary on CTHDA chips */ |
| 302 | if (chip->driver_caps & AZX_DCAPS_4K_BDLE_BOUNDARY) { |
| 303 | u32 remain = 0x1000 - (ofs & 0xfff); |
| 304 | if (chunk > remain) |
| 305 | chunk = remain; |
| 306 | } |
| 307 | bdl[2] = cpu_to_le32(chunk); |
| 308 | /* program the IOC to enable interrupt |
| 309 | * only when the whole fragment is processed |
| 310 | */ |
| 311 | size -= chunk; |
| 312 | bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01); |
| 313 | bdl += 4; |
| 314 | azx_dev->frags++; |
| 315 | ofs += chunk; |
| 316 | } |
| 317 | *bdlp = bdl; |
| 318 | return ofs; |
| 319 | } |
Dylan Reid | 05e8487 | 2014-02-28 15:41:22 -0800 | [diff] [blame] | 320 | |
| 321 | /* |
| 322 | * set up BDL entries |
| 323 | */ |
| 324 | static int azx_setup_periods(struct azx *chip, |
| 325 | struct snd_pcm_substream *substream, |
| 326 | struct azx_dev *azx_dev) |
| 327 | { |
| 328 | u32 *bdl; |
| 329 | int i, ofs, periods, period_bytes; |
| 330 | int pos_adj = 0; |
| 331 | |
| 332 | /* reset BDL address */ |
| 333 | azx_sd_writel(chip, azx_dev, SD_BDLPL, 0); |
| 334 | azx_sd_writel(chip, azx_dev, SD_BDLPU, 0); |
| 335 | |
| 336 | period_bytes = azx_dev->period_bytes; |
| 337 | periods = azx_dev->bufsize / period_bytes; |
| 338 | |
| 339 | /* program the initial BDL entries */ |
| 340 | bdl = (u32 *)azx_dev->bdl.area; |
| 341 | ofs = 0; |
| 342 | azx_dev->frags = 0; |
| 343 | |
| 344 | if (chip->bdl_pos_adj) |
| 345 | pos_adj = chip->bdl_pos_adj[chip->dev_index]; |
| 346 | if (!azx_dev->no_period_wakeup && pos_adj > 0) { |
| 347 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 348 | int pos_align = pos_adj; |
| 349 | pos_adj = (pos_adj * runtime->rate + 47999) / 48000; |
| 350 | if (!pos_adj) |
| 351 | pos_adj = pos_align; |
| 352 | else |
| 353 | pos_adj = ((pos_adj + pos_align - 1) / pos_align) * |
| 354 | pos_align; |
| 355 | pos_adj = frames_to_bytes(runtime, pos_adj); |
| 356 | if (pos_adj >= period_bytes) { |
| 357 | dev_warn(chip->card->dev,"Too big adjustment %d\n", |
| 358 | pos_adj); |
| 359 | pos_adj = 0; |
| 360 | } else { |
| 361 | ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream), |
| 362 | azx_dev, |
| 363 | &bdl, ofs, pos_adj, true); |
| 364 | if (ofs < 0) |
| 365 | goto error; |
| 366 | } |
| 367 | } else |
| 368 | pos_adj = 0; |
| 369 | |
| 370 | for (i = 0; i < periods; i++) { |
| 371 | if (i == periods - 1 && pos_adj) |
| 372 | ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream), |
| 373 | azx_dev, &bdl, ofs, |
| 374 | period_bytes - pos_adj, 0); |
| 375 | else |
| 376 | ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream), |
| 377 | azx_dev, &bdl, ofs, |
| 378 | period_bytes, |
| 379 | !azx_dev->no_period_wakeup); |
| 380 | if (ofs < 0) |
| 381 | goto error; |
| 382 | } |
| 383 | return 0; |
| 384 | |
| 385 | error: |
| 386 | dev_err(chip->card->dev, "Too many BDL entries: buffer=%d, period=%d\n", |
| 387 | azx_dev->bufsize, period_bytes); |
| 388 | return -EINVAL; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * PCM ops |
| 393 | */ |
| 394 | |
| 395 | static int azx_pcm_close(struct snd_pcm_substream *substream) |
| 396 | { |
| 397 | struct azx_pcm *apcm = snd_pcm_substream_chip(substream); |
| 398 | struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream]; |
| 399 | struct azx *chip = apcm->chip; |
| 400 | struct azx_dev *azx_dev = get_azx_dev(substream); |
| 401 | unsigned long flags; |
| 402 | |
| 403 | mutex_lock(&chip->open_mutex); |
| 404 | spin_lock_irqsave(&chip->reg_lock, flags); |
| 405 | azx_dev->substream = NULL; |
| 406 | azx_dev->running = 0; |
| 407 | spin_unlock_irqrestore(&chip->reg_lock, flags); |
| 408 | azx_release_device(azx_dev); |
| 409 | hinfo->ops.close(hinfo, apcm->codec, substream); |
| 410 | snd_hda_power_down(apcm->codec); |
| 411 | mutex_unlock(&chip->open_mutex); |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static int azx_pcm_hw_params(struct snd_pcm_substream *substream, |
| 416 | struct snd_pcm_hw_params *hw_params) |
| 417 | { |
| 418 | struct azx_pcm *apcm = snd_pcm_substream_chip(substream); |
| 419 | struct azx *chip = apcm->chip; |
| 420 | int ret; |
| 421 | |
| 422 | dsp_lock(get_azx_dev(substream)); |
| 423 | if (dsp_is_locked(get_azx_dev(substream))) { |
| 424 | ret = -EBUSY; |
| 425 | goto unlock; |
| 426 | } |
| 427 | |
| 428 | ret = chip->ops->substream_alloc_pages(chip, substream, |
| 429 | params_buffer_bytes(hw_params)); |
| 430 | unlock: |
| 431 | dsp_unlock(get_azx_dev(substream)); |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | static int azx_pcm_hw_free(struct snd_pcm_substream *substream) |
| 436 | { |
| 437 | struct azx_pcm *apcm = snd_pcm_substream_chip(substream); |
| 438 | struct azx_dev *azx_dev = get_azx_dev(substream); |
| 439 | struct azx *chip = apcm->chip; |
| 440 | struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream]; |
| 441 | int err; |
| 442 | |
| 443 | /* reset BDL address */ |
| 444 | dsp_lock(azx_dev); |
| 445 | if (!dsp_is_locked(azx_dev)) { |
| 446 | azx_sd_writel(chip, azx_dev, SD_BDLPL, 0); |
| 447 | azx_sd_writel(chip, azx_dev, SD_BDLPU, 0); |
| 448 | azx_sd_writel(chip, azx_dev, SD_CTL, 0); |
| 449 | azx_dev->bufsize = 0; |
| 450 | azx_dev->period_bytes = 0; |
| 451 | azx_dev->format_val = 0; |
| 452 | } |
| 453 | |
| 454 | snd_hda_codec_cleanup(apcm->codec, hinfo, substream); |
| 455 | |
| 456 | err = chip->ops->substream_free_pages(chip, substream); |
| 457 | azx_dev->prepared = 0; |
| 458 | dsp_unlock(azx_dev); |
| 459 | return err; |
| 460 | } |
| 461 | |
| 462 | static int azx_pcm_prepare(struct snd_pcm_substream *substream) |
| 463 | { |
| 464 | struct azx_pcm *apcm = snd_pcm_substream_chip(substream); |
| 465 | struct azx *chip = apcm->chip; |
| 466 | struct azx_dev *azx_dev = get_azx_dev(substream); |
| 467 | struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream]; |
| 468 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 469 | unsigned int bufsize, period_bytes, format_val, stream_tag; |
| 470 | int err; |
| 471 | struct hda_spdif_out *spdif = |
| 472 | snd_hda_spdif_out_of_nid(apcm->codec, hinfo->nid); |
| 473 | unsigned short ctls = spdif ? spdif->ctls : 0; |
| 474 | |
| 475 | dsp_lock(azx_dev); |
| 476 | if (dsp_is_locked(azx_dev)) { |
| 477 | err = -EBUSY; |
| 478 | goto unlock; |
| 479 | } |
| 480 | |
| 481 | azx_stream_reset(chip, azx_dev); |
| 482 | format_val = snd_hda_calc_stream_format(runtime->rate, |
| 483 | runtime->channels, |
| 484 | runtime->format, |
| 485 | hinfo->maxbps, |
| 486 | ctls); |
| 487 | if (!format_val) { |
| 488 | dev_err(chip->card->dev, |
| 489 | "invalid format_val, rate=%d, ch=%d, format=%d\n", |
| 490 | runtime->rate, runtime->channels, runtime->format); |
| 491 | err = -EINVAL; |
| 492 | goto unlock; |
| 493 | } |
| 494 | |
| 495 | bufsize = snd_pcm_lib_buffer_bytes(substream); |
| 496 | period_bytes = snd_pcm_lib_period_bytes(substream); |
| 497 | |
| 498 | dev_dbg(chip->card->dev, "azx_pcm_prepare: bufsize=0x%x, format=0x%x\n", |
| 499 | bufsize, format_val); |
| 500 | |
| 501 | if (bufsize != azx_dev->bufsize || |
| 502 | period_bytes != azx_dev->period_bytes || |
| 503 | format_val != azx_dev->format_val || |
| 504 | runtime->no_period_wakeup != azx_dev->no_period_wakeup) { |
| 505 | azx_dev->bufsize = bufsize; |
| 506 | azx_dev->period_bytes = period_bytes; |
| 507 | azx_dev->format_val = format_val; |
| 508 | azx_dev->no_period_wakeup = runtime->no_period_wakeup; |
| 509 | err = azx_setup_periods(chip, substream, azx_dev); |
| 510 | if (err < 0) |
| 511 | goto unlock; |
| 512 | } |
| 513 | |
| 514 | /* when LPIB delay correction gives a small negative value, |
| 515 | * we ignore it; currently set the threshold statically to |
| 516 | * 64 frames |
| 517 | */ |
| 518 | if (runtime->period_size > 64) |
| 519 | azx_dev->delay_negative_threshold = -frames_to_bytes(runtime, 64); |
| 520 | else |
| 521 | azx_dev->delay_negative_threshold = 0; |
| 522 | |
| 523 | /* wallclk has 24Mhz clock source */ |
| 524 | azx_dev->period_wallclk = (((runtime->period_size * 24000) / |
| 525 | runtime->rate) * 1000); |
| 526 | azx_setup_controller(chip, azx_dev); |
| 527 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 528 | azx_dev->fifo_size = |
| 529 | azx_sd_readw(chip, azx_dev, SD_FIFOSIZE) + 1; |
| 530 | else |
| 531 | azx_dev->fifo_size = 0; |
| 532 | |
| 533 | stream_tag = azx_dev->stream_tag; |
| 534 | /* CA-IBG chips need the playback stream starting from 1 */ |
| 535 | if ((chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND) && |
| 536 | stream_tag > chip->capture_streams) |
| 537 | stream_tag -= chip->capture_streams; |
| 538 | err = snd_hda_codec_prepare(apcm->codec, hinfo, stream_tag, |
| 539 | azx_dev->format_val, substream); |
| 540 | |
| 541 | unlock: |
| 542 | if (!err) |
| 543 | azx_dev->prepared = 1; |
| 544 | dsp_unlock(azx_dev); |
| 545 | return err; |
| 546 | } |
| 547 | |
| 548 | static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 549 | { |
| 550 | struct azx_pcm *apcm = snd_pcm_substream_chip(substream); |
| 551 | struct azx *chip = apcm->chip; |
| 552 | struct azx_dev *azx_dev; |
| 553 | struct snd_pcm_substream *s; |
| 554 | int rstart = 0, start, nsync = 0, sbits = 0; |
| 555 | int nwait, timeout; |
| 556 | |
| 557 | azx_dev = get_azx_dev(substream); |
| 558 | trace_azx_pcm_trigger(chip, azx_dev, cmd); |
| 559 | |
| 560 | if (dsp_is_locked(azx_dev) || !azx_dev->prepared) |
| 561 | return -EPIPE; |
| 562 | |
| 563 | switch (cmd) { |
| 564 | case SNDRV_PCM_TRIGGER_START: |
| 565 | rstart = 1; |
| 566 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 567 | case SNDRV_PCM_TRIGGER_RESUME: |
| 568 | start = 1; |
| 569 | break; |
| 570 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 571 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 572 | case SNDRV_PCM_TRIGGER_STOP: |
| 573 | start = 0; |
| 574 | break; |
| 575 | default: |
| 576 | return -EINVAL; |
| 577 | } |
| 578 | |
| 579 | snd_pcm_group_for_each_entry(s, substream) { |
| 580 | if (s->pcm->card != substream->pcm->card) |
| 581 | continue; |
| 582 | azx_dev = get_azx_dev(s); |
| 583 | sbits |= 1 << azx_dev->index; |
| 584 | nsync++; |
| 585 | snd_pcm_trigger_done(s, substream); |
| 586 | } |
| 587 | |
| 588 | spin_lock(&chip->reg_lock); |
| 589 | |
| 590 | /* first, set SYNC bits of corresponding streams */ |
| 591 | if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC) |
| 592 | azx_writel(chip, OLD_SSYNC, |
| 593 | azx_readl(chip, OLD_SSYNC) | sbits); |
| 594 | else |
| 595 | azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) | sbits); |
| 596 | |
| 597 | snd_pcm_group_for_each_entry(s, substream) { |
| 598 | if (s->pcm->card != substream->pcm->card) |
| 599 | continue; |
| 600 | azx_dev = get_azx_dev(s); |
| 601 | if (start) { |
| 602 | azx_dev->start_wallclk = azx_readl(chip, WALLCLK); |
| 603 | if (!rstart) |
| 604 | azx_dev->start_wallclk -= |
| 605 | azx_dev->period_wallclk; |
| 606 | azx_stream_start(chip, azx_dev); |
| 607 | } else { |
| 608 | azx_stream_stop(chip, azx_dev); |
| 609 | } |
| 610 | azx_dev->running = start; |
| 611 | } |
| 612 | spin_unlock(&chip->reg_lock); |
| 613 | if (start) { |
| 614 | /* wait until all FIFOs get ready */ |
| 615 | for (timeout = 5000; timeout; timeout--) { |
| 616 | nwait = 0; |
| 617 | snd_pcm_group_for_each_entry(s, substream) { |
| 618 | if (s->pcm->card != substream->pcm->card) |
| 619 | continue; |
| 620 | azx_dev = get_azx_dev(s); |
| 621 | if (!(azx_sd_readb(chip, azx_dev, SD_STS) & |
| 622 | SD_STS_FIFO_READY)) |
| 623 | nwait++; |
| 624 | } |
| 625 | if (!nwait) |
| 626 | break; |
| 627 | cpu_relax(); |
| 628 | } |
| 629 | } else { |
| 630 | /* wait until all RUN bits are cleared */ |
| 631 | for (timeout = 5000; timeout; timeout--) { |
| 632 | nwait = 0; |
| 633 | snd_pcm_group_for_each_entry(s, substream) { |
| 634 | if (s->pcm->card != substream->pcm->card) |
| 635 | continue; |
| 636 | azx_dev = get_azx_dev(s); |
| 637 | if (azx_sd_readb(chip, azx_dev, SD_CTL) & |
| 638 | SD_CTL_DMA_START) |
| 639 | nwait++; |
| 640 | } |
| 641 | if (!nwait) |
| 642 | break; |
| 643 | cpu_relax(); |
| 644 | } |
| 645 | } |
| 646 | spin_lock(&chip->reg_lock); |
| 647 | /* reset SYNC bits */ |
| 648 | if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC) |
| 649 | azx_writel(chip, OLD_SSYNC, |
| 650 | azx_readl(chip, OLD_SSYNC) & ~sbits); |
| 651 | else |
| 652 | azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) & ~sbits); |
| 653 | if (start) { |
| 654 | azx_timecounter_init(substream, 0, 0); |
| 655 | if (nsync > 1) { |
| 656 | cycle_t cycle_last; |
| 657 | |
| 658 | /* same start cycle for master and group */ |
| 659 | azx_dev = get_azx_dev(substream); |
| 660 | cycle_last = azx_dev->azx_tc.cycle_last; |
| 661 | |
| 662 | snd_pcm_group_for_each_entry(s, substream) { |
| 663 | if (s->pcm->card != substream->pcm->card) |
| 664 | continue; |
| 665 | azx_timecounter_init(s, 1, cycle_last); |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | spin_unlock(&chip->reg_lock); |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | /* get the current DMA position with correction on VIA chips */ |
| 674 | static unsigned int azx_via_get_position(struct azx *chip, |
| 675 | struct azx_dev *azx_dev) |
| 676 | { |
| 677 | unsigned int link_pos, mini_pos, bound_pos; |
| 678 | unsigned int mod_link_pos, mod_dma_pos, mod_mini_pos; |
| 679 | unsigned int fifo_size; |
| 680 | |
| 681 | link_pos = azx_sd_readl(chip, azx_dev, SD_LPIB); |
| 682 | if (azx_dev->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 683 | /* Playback, no problem using link position */ |
| 684 | return link_pos; |
| 685 | } |
| 686 | |
| 687 | /* Capture */ |
| 688 | /* For new chipset, |
| 689 | * use mod to get the DMA position just like old chipset |
| 690 | */ |
| 691 | mod_dma_pos = le32_to_cpu(*azx_dev->posbuf); |
| 692 | mod_dma_pos %= azx_dev->period_bytes; |
| 693 | |
| 694 | /* azx_dev->fifo_size can't get FIFO size of in stream. |
| 695 | * Get from base address + offset. |
| 696 | */ |
| 697 | fifo_size = readw(chip->remap_addr + VIA_IN_STREAM0_FIFO_SIZE_OFFSET); |
| 698 | |
| 699 | if (azx_dev->insufficient) { |
| 700 | /* Link position never gather than FIFO size */ |
| 701 | if (link_pos <= fifo_size) |
| 702 | return 0; |
| 703 | |
| 704 | azx_dev->insufficient = 0; |
| 705 | } |
| 706 | |
| 707 | if (link_pos <= fifo_size) |
| 708 | mini_pos = azx_dev->bufsize + link_pos - fifo_size; |
| 709 | else |
| 710 | mini_pos = link_pos - fifo_size; |
| 711 | |
| 712 | /* Find nearest previous boudary */ |
| 713 | mod_mini_pos = mini_pos % azx_dev->period_bytes; |
| 714 | mod_link_pos = link_pos % azx_dev->period_bytes; |
| 715 | if (mod_link_pos >= fifo_size) |
| 716 | bound_pos = link_pos - mod_link_pos; |
| 717 | else if (mod_dma_pos >= mod_mini_pos) |
| 718 | bound_pos = mini_pos - mod_mini_pos; |
| 719 | else { |
| 720 | bound_pos = mini_pos - mod_mini_pos + azx_dev->period_bytes; |
| 721 | if (bound_pos >= azx_dev->bufsize) |
| 722 | bound_pos = 0; |
| 723 | } |
| 724 | |
| 725 | /* Calculate real DMA position we want */ |
| 726 | return bound_pos + mod_dma_pos; |
| 727 | } |
| 728 | |
| 729 | unsigned int azx_get_position(struct azx *chip, |
| 730 | struct azx_dev *azx_dev, |
| 731 | bool with_check) |
| 732 | { |
| 733 | struct snd_pcm_substream *substream = azx_dev->substream; |
| 734 | struct azx_pcm *apcm = snd_pcm_substream_chip(substream); |
| 735 | unsigned int pos; |
| 736 | int stream = substream->stream; |
| 737 | struct hda_pcm_stream *hinfo = apcm->hinfo[stream]; |
| 738 | int delay = 0; |
| 739 | |
| 740 | switch (chip->position_fix[stream]) { |
| 741 | case POS_FIX_LPIB: |
| 742 | /* read LPIB */ |
| 743 | pos = azx_sd_readl(chip, azx_dev, SD_LPIB); |
| 744 | break; |
| 745 | case POS_FIX_VIACOMBO: |
| 746 | pos = azx_via_get_position(chip, azx_dev); |
| 747 | break; |
| 748 | default: |
| 749 | /* use the position buffer */ |
| 750 | pos = le32_to_cpu(*azx_dev->posbuf); |
| 751 | if (with_check && chip->position_fix[stream] == POS_FIX_AUTO) { |
| 752 | if (!pos || pos == (u32)-1) { |
| 753 | dev_info(chip->card->dev, |
| 754 | "Invalid position buffer, using LPIB read method instead.\n"); |
| 755 | chip->position_fix[stream] = POS_FIX_LPIB; |
| 756 | pos = azx_sd_readl(chip, azx_dev, SD_LPIB); |
| 757 | } else |
| 758 | chip->position_fix[stream] = POS_FIX_POSBUF; |
| 759 | } |
| 760 | break; |
| 761 | } |
| 762 | |
| 763 | if (pos >= azx_dev->bufsize) |
| 764 | pos = 0; |
| 765 | |
| 766 | /* calculate runtime delay from LPIB */ |
| 767 | if (substream->runtime && |
| 768 | chip->position_fix[stream] == POS_FIX_POSBUF && |
| 769 | (chip->driver_caps & AZX_DCAPS_COUNT_LPIB_DELAY)) { |
| 770 | unsigned int lpib_pos = azx_sd_readl(chip, azx_dev, SD_LPIB); |
| 771 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 772 | delay = pos - lpib_pos; |
| 773 | else |
| 774 | delay = lpib_pos - pos; |
| 775 | if (delay < 0) { |
| 776 | if (delay >= azx_dev->delay_negative_threshold) |
| 777 | delay = 0; |
| 778 | else |
| 779 | delay += azx_dev->bufsize; |
| 780 | } |
| 781 | if (delay >= azx_dev->period_bytes) { |
| 782 | dev_info(chip->card->dev, |
| 783 | "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n", |
| 784 | delay, azx_dev->period_bytes); |
| 785 | delay = 0; |
| 786 | chip->driver_caps &= ~AZX_DCAPS_COUNT_LPIB_DELAY; |
| 787 | } |
| 788 | delay = bytes_to_frames(substream->runtime, delay); |
| 789 | } |
| 790 | |
| 791 | if (substream->runtime) { |
| 792 | if (hinfo->ops.get_delay) |
| 793 | delay += hinfo->ops.get_delay(hinfo, apcm->codec, |
| 794 | substream); |
| 795 | substream->runtime->delay = delay; |
| 796 | } |
| 797 | |
| 798 | trace_azx_get_position(chip, azx_dev, pos, delay); |
| 799 | return pos; |
| 800 | } |
| 801 | EXPORT_SYMBOL_GPL(azx_get_position); |
| 802 | |
| 803 | static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream) |
| 804 | { |
| 805 | struct azx_pcm *apcm = snd_pcm_substream_chip(substream); |
| 806 | struct azx *chip = apcm->chip; |
| 807 | struct azx_dev *azx_dev = get_azx_dev(substream); |
| 808 | return bytes_to_frames(substream->runtime, |
| 809 | azx_get_position(chip, azx_dev, false)); |
| 810 | } |
| 811 | |
| 812 | static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream, |
| 813 | struct timespec *ts) |
| 814 | { |
| 815 | struct azx_dev *azx_dev = get_azx_dev(substream); |
| 816 | u64 nsec; |
| 817 | |
| 818 | nsec = timecounter_read(&azx_dev->azx_tc); |
| 819 | nsec = div_u64(nsec, 3); /* can be optimized */ |
| 820 | nsec = azx_adjust_codec_delay(substream, nsec); |
| 821 | |
| 822 | *ts = ns_to_timespec(nsec); |
| 823 | |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | static struct snd_pcm_hardware azx_pcm_hw = { |
| 828 | .info = (SNDRV_PCM_INFO_MMAP | |
| 829 | SNDRV_PCM_INFO_INTERLEAVED | |
| 830 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 831 | SNDRV_PCM_INFO_MMAP_VALID | |
| 832 | /* No full-resume yet implemented */ |
| 833 | /* SNDRV_PCM_INFO_RESUME |*/ |
| 834 | SNDRV_PCM_INFO_PAUSE | |
| 835 | SNDRV_PCM_INFO_SYNC_START | |
| 836 | SNDRV_PCM_INFO_HAS_WALL_CLOCK | |
| 837 | SNDRV_PCM_INFO_NO_PERIOD_WAKEUP), |
| 838 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 839 | .rates = SNDRV_PCM_RATE_48000, |
| 840 | .rate_min = 48000, |
| 841 | .rate_max = 48000, |
| 842 | .channels_min = 2, |
| 843 | .channels_max = 2, |
| 844 | .buffer_bytes_max = AZX_MAX_BUF_SIZE, |
| 845 | .period_bytes_min = 128, |
| 846 | .period_bytes_max = AZX_MAX_BUF_SIZE / 2, |
| 847 | .periods_min = 2, |
| 848 | .periods_max = AZX_MAX_FRAG, |
| 849 | .fifo_size = 0, |
| 850 | }; |
| 851 | |
| 852 | static int azx_pcm_open(struct snd_pcm_substream *substream) |
| 853 | { |
| 854 | struct azx_pcm *apcm = snd_pcm_substream_chip(substream); |
| 855 | struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream]; |
| 856 | struct azx *chip = apcm->chip; |
| 857 | struct azx_dev *azx_dev; |
| 858 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 859 | unsigned long flags; |
| 860 | int err; |
| 861 | int buff_step; |
| 862 | |
| 863 | mutex_lock(&chip->open_mutex); |
| 864 | azx_dev = azx_assign_device(chip, substream); |
| 865 | if (azx_dev == NULL) { |
| 866 | mutex_unlock(&chip->open_mutex); |
| 867 | return -EBUSY; |
| 868 | } |
| 869 | runtime->hw = azx_pcm_hw; |
| 870 | runtime->hw.channels_min = hinfo->channels_min; |
| 871 | runtime->hw.channels_max = hinfo->channels_max; |
| 872 | runtime->hw.formats = hinfo->formats; |
| 873 | runtime->hw.rates = hinfo->rates; |
| 874 | snd_pcm_limit_hw_rates(runtime); |
| 875 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
| 876 | |
| 877 | /* avoid wrap-around with wall-clock */ |
| 878 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME, |
| 879 | 20, |
| 880 | 178000000); |
| 881 | |
| 882 | if (chip->align_buffer_size) |
| 883 | /* constrain buffer sizes to be multiple of 128 |
| 884 | bytes. This is more efficient in terms of memory |
| 885 | access but isn't required by the HDA spec and |
| 886 | prevents users from specifying exact period/buffer |
| 887 | sizes. For example for 44.1kHz, a period size set |
| 888 | to 20ms will be rounded to 19.59ms. */ |
| 889 | buff_step = 128; |
| 890 | else |
| 891 | /* Don't enforce steps on buffer sizes, still need to |
| 892 | be multiple of 4 bytes (HDA spec). Tested on Intel |
| 893 | HDA controllers, may not work on all devices where |
| 894 | option needs to be disabled */ |
| 895 | buff_step = 4; |
| 896 | |
| 897 | snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, |
| 898 | buff_step); |
| 899 | snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, |
| 900 | buff_step); |
| 901 | snd_hda_power_up_d3wait(apcm->codec); |
| 902 | err = hinfo->ops.open(hinfo, apcm->codec, substream); |
| 903 | if (err < 0) { |
| 904 | azx_release_device(azx_dev); |
| 905 | snd_hda_power_down(apcm->codec); |
| 906 | mutex_unlock(&chip->open_mutex); |
| 907 | return err; |
| 908 | } |
| 909 | snd_pcm_limit_hw_rates(runtime); |
| 910 | /* sanity check */ |
| 911 | if (snd_BUG_ON(!runtime->hw.channels_min) || |
| 912 | snd_BUG_ON(!runtime->hw.channels_max) || |
| 913 | snd_BUG_ON(!runtime->hw.formats) || |
| 914 | snd_BUG_ON(!runtime->hw.rates)) { |
| 915 | azx_release_device(azx_dev); |
| 916 | hinfo->ops.close(hinfo, apcm->codec, substream); |
| 917 | snd_hda_power_down(apcm->codec); |
| 918 | mutex_unlock(&chip->open_mutex); |
| 919 | return -EINVAL; |
| 920 | } |
| 921 | |
| 922 | /* disable WALLCLOCK timestamps for capture streams |
| 923 | until we figure out how to handle digital inputs */ |
| 924 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 925 | runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK; |
| 926 | |
| 927 | spin_lock_irqsave(&chip->reg_lock, flags); |
| 928 | azx_dev->substream = substream; |
| 929 | azx_dev->running = 0; |
| 930 | spin_unlock_irqrestore(&chip->reg_lock, flags); |
| 931 | |
| 932 | runtime->private_data = azx_dev; |
| 933 | snd_pcm_set_sync(substream); |
| 934 | mutex_unlock(&chip->open_mutex); |
| 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | static int azx_pcm_mmap(struct snd_pcm_substream *substream, |
| 939 | struct vm_area_struct *area) |
| 940 | { |
| 941 | struct azx_pcm *apcm = snd_pcm_substream_chip(substream); |
| 942 | struct azx *chip = apcm->chip; |
| 943 | if (chip->ops->pcm_mmap_prepare) |
| 944 | chip->ops->pcm_mmap_prepare(substream, area); |
| 945 | return snd_pcm_lib_default_mmap(substream, area); |
| 946 | } |
| 947 | |
| 948 | static struct snd_pcm_ops azx_pcm_ops = { |
| 949 | .open = azx_pcm_open, |
| 950 | .close = azx_pcm_close, |
| 951 | .ioctl = snd_pcm_lib_ioctl, |
| 952 | .hw_params = azx_pcm_hw_params, |
| 953 | .hw_free = azx_pcm_hw_free, |
| 954 | .prepare = azx_pcm_prepare, |
| 955 | .trigger = azx_pcm_trigger, |
| 956 | .pointer = azx_pcm_pointer, |
| 957 | .wall_clock = azx_get_wallclock_tstamp, |
| 958 | .mmap = azx_pcm_mmap, |
| 959 | .page = snd_pcm_sgbuf_ops_page, |
| 960 | }; |
| 961 | |
| 962 | static void azx_pcm_free(struct snd_pcm *pcm) |
| 963 | { |
| 964 | struct azx_pcm *apcm = pcm->private_data; |
| 965 | if (apcm) { |
| 966 | list_del(&apcm->list); |
| 967 | kfree(apcm); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | #define MAX_PREALLOC_SIZE (32 * 1024 * 1024) |
| 972 | |
| 973 | int azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec, |
| 974 | struct hda_pcm *cpcm) |
| 975 | { |
| 976 | struct azx *chip = bus->private_data; |
| 977 | struct snd_pcm *pcm; |
| 978 | struct azx_pcm *apcm; |
| 979 | int pcm_dev = cpcm->device; |
| 980 | unsigned int size; |
| 981 | int s, err; |
| 982 | |
| 983 | list_for_each_entry(apcm, &chip->pcm_list, list) { |
| 984 | if (apcm->pcm->device == pcm_dev) { |
| 985 | dev_err(chip->card->dev, "PCM %d already exists\n", |
| 986 | pcm_dev); |
| 987 | return -EBUSY; |
| 988 | } |
| 989 | } |
| 990 | err = snd_pcm_new(chip->card, cpcm->name, pcm_dev, |
| 991 | cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams, |
| 992 | cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams, |
| 993 | &pcm); |
| 994 | if (err < 0) |
| 995 | return err; |
| 996 | strlcpy(pcm->name, cpcm->name, sizeof(pcm->name)); |
| 997 | apcm = kzalloc(sizeof(*apcm), GFP_KERNEL); |
| 998 | if (apcm == NULL) |
| 999 | return -ENOMEM; |
| 1000 | apcm->chip = chip; |
| 1001 | apcm->pcm = pcm; |
| 1002 | apcm->codec = codec; |
| 1003 | pcm->private_data = apcm; |
| 1004 | pcm->private_free = azx_pcm_free; |
| 1005 | if (cpcm->pcm_type == HDA_PCM_TYPE_MODEM) |
| 1006 | pcm->dev_class = SNDRV_PCM_CLASS_MODEM; |
| 1007 | list_add_tail(&apcm->list, &chip->pcm_list); |
| 1008 | cpcm->pcm = pcm; |
| 1009 | for (s = 0; s < 2; s++) { |
| 1010 | apcm->hinfo[s] = &cpcm->stream[s]; |
| 1011 | if (cpcm->stream[s].substreams) |
| 1012 | snd_pcm_set_ops(pcm, s, &azx_pcm_ops); |
| 1013 | } |
| 1014 | /* buffer pre-allocation */ |
| 1015 | size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024; |
| 1016 | if (size > MAX_PREALLOC_SIZE) |
| 1017 | size = MAX_PREALLOC_SIZE; |
| 1018 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG, |
| 1019 | chip->card->dev, |
| 1020 | size, MAX_PREALLOC_SIZE); |
| 1021 | /* link to codec */ |
| 1022 | pcm->dev = &codec->dev; |
| 1023 | return 0; |
| 1024 | } |
| 1025 | EXPORT_SYMBOL_GPL(azx_attach_pcm_stream); |
| 1026 | |
Dylan Reid | 6e85ddd | 2014-02-28 15:41:25 -0800 | [diff] [blame] | 1027 | /* |
| 1028 | * CORB / RIRB interface |
| 1029 | */ |
Dylan Reid | f19c3ec | 2014-02-28 15:41:26 -0800 | [diff] [blame] | 1030 | static int azx_alloc_cmd_io(struct azx *chip) |
Dylan Reid | 6e85ddd | 2014-02-28 15:41:25 -0800 | [diff] [blame] | 1031 | { |
| 1032 | int err; |
| 1033 | |
| 1034 | /* single page (at least 4096 bytes) must suffice for both ringbuffes */ |
| 1035 | err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV, |
| 1036 | PAGE_SIZE, &chip->rb); |
| 1037 | if (err < 0) |
| 1038 | dev_err(chip->card->dev, "cannot allocate CORB/RIRB\n"); |
| 1039 | return err; |
| 1040 | } |
| 1041 | EXPORT_SYMBOL_GPL(azx_alloc_cmd_io); |
| 1042 | |
Dylan Reid | f43923f | 2014-02-28 15:41:27 -0800 | [diff] [blame] | 1043 | static void azx_init_cmd_io(struct azx *chip) |
Dylan Reid | 6e85ddd | 2014-02-28 15:41:25 -0800 | [diff] [blame] | 1044 | { |
| 1045 | int timeout; |
| 1046 | |
| 1047 | spin_lock_irq(&chip->reg_lock); |
| 1048 | /* CORB set up */ |
| 1049 | chip->corb.addr = chip->rb.addr; |
| 1050 | chip->corb.buf = (u32 *)chip->rb.area; |
| 1051 | azx_writel(chip, CORBLBASE, (u32)chip->corb.addr); |
| 1052 | azx_writel(chip, CORBUBASE, upper_32_bits(chip->corb.addr)); |
| 1053 | |
| 1054 | /* set the corb size to 256 entries (ULI requires explicitly) */ |
| 1055 | azx_writeb(chip, CORBSIZE, 0x02); |
| 1056 | /* set the corb write pointer to 0 */ |
| 1057 | azx_writew(chip, CORBWP, 0); |
| 1058 | |
| 1059 | /* reset the corb hw read pointer */ |
| 1060 | azx_writew(chip, CORBRP, ICH6_CORBRP_RST); |
| 1061 | for (timeout = 1000; timeout > 0; timeout--) { |
| 1062 | if ((azx_readw(chip, CORBRP) & ICH6_CORBRP_RST) == ICH6_CORBRP_RST) |
| 1063 | break; |
| 1064 | udelay(1); |
| 1065 | } |
| 1066 | if (timeout <= 0) |
| 1067 | dev_err(chip->card->dev, "CORB reset timeout#1, CORBRP = %d\n", |
| 1068 | azx_readw(chip, CORBRP)); |
| 1069 | |
| 1070 | azx_writew(chip, CORBRP, 0); |
| 1071 | for (timeout = 1000; timeout > 0; timeout--) { |
| 1072 | if (azx_readw(chip, CORBRP) == 0) |
| 1073 | break; |
| 1074 | udelay(1); |
| 1075 | } |
| 1076 | if (timeout <= 0) |
| 1077 | dev_err(chip->card->dev, "CORB reset timeout#2, CORBRP = %d\n", |
| 1078 | azx_readw(chip, CORBRP)); |
| 1079 | |
| 1080 | /* enable corb dma */ |
| 1081 | azx_writeb(chip, CORBCTL, ICH6_CORBCTL_RUN); |
| 1082 | |
| 1083 | /* RIRB set up */ |
| 1084 | chip->rirb.addr = chip->rb.addr + 2048; |
| 1085 | chip->rirb.buf = (u32 *)(chip->rb.area + 2048); |
| 1086 | chip->rirb.wp = chip->rirb.rp = 0; |
| 1087 | memset(chip->rirb.cmds, 0, sizeof(chip->rirb.cmds)); |
| 1088 | azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr); |
| 1089 | azx_writel(chip, RIRBUBASE, upper_32_bits(chip->rirb.addr)); |
| 1090 | |
| 1091 | /* set the rirb size to 256 entries (ULI requires explicitly) */ |
| 1092 | azx_writeb(chip, RIRBSIZE, 0x02); |
| 1093 | /* reset the rirb hw write pointer */ |
| 1094 | azx_writew(chip, RIRBWP, ICH6_RIRBWP_RST); |
| 1095 | /* set N=1, get RIRB response interrupt for new entry */ |
| 1096 | if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND) |
| 1097 | azx_writew(chip, RINTCNT, 0xc0); |
| 1098 | else |
| 1099 | azx_writew(chip, RINTCNT, 1); |
| 1100 | /* enable rirb dma and response irq */ |
| 1101 | azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN | ICH6_RBCTL_IRQ_EN); |
| 1102 | spin_unlock_irq(&chip->reg_lock); |
| 1103 | } |
| 1104 | EXPORT_SYMBOL_GPL(azx_init_cmd_io); |
| 1105 | |
Dylan Reid | f43923f | 2014-02-28 15:41:27 -0800 | [diff] [blame] | 1106 | static void azx_free_cmd_io(struct azx *chip) |
Dylan Reid | 6e85ddd | 2014-02-28 15:41:25 -0800 | [diff] [blame] | 1107 | { |
| 1108 | spin_lock_irq(&chip->reg_lock); |
| 1109 | /* disable ringbuffer DMAs */ |
| 1110 | azx_writeb(chip, RIRBCTL, 0); |
| 1111 | azx_writeb(chip, CORBCTL, 0); |
| 1112 | spin_unlock_irq(&chip->reg_lock); |
| 1113 | } |
| 1114 | EXPORT_SYMBOL_GPL(azx_free_cmd_io); |
| 1115 | |
| 1116 | static unsigned int azx_command_addr(u32 cmd) |
| 1117 | { |
| 1118 | unsigned int addr = cmd >> 28; |
| 1119 | |
| 1120 | if (addr >= AZX_MAX_CODECS) { |
| 1121 | snd_BUG(); |
| 1122 | addr = 0; |
| 1123 | } |
| 1124 | |
| 1125 | return addr; |
| 1126 | } |
| 1127 | |
| 1128 | /* send a command */ |
| 1129 | static int azx_corb_send_cmd(struct hda_bus *bus, u32 val) |
| 1130 | { |
| 1131 | struct azx *chip = bus->private_data; |
| 1132 | unsigned int addr = azx_command_addr(val); |
| 1133 | unsigned int wp, rp; |
| 1134 | |
| 1135 | spin_lock_irq(&chip->reg_lock); |
| 1136 | |
| 1137 | /* add command to corb */ |
| 1138 | wp = azx_readw(chip, CORBWP); |
| 1139 | if (wp == 0xffff) { |
| 1140 | /* something wrong, controller likely turned to D3 */ |
| 1141 | spin_unlock_irq(&chip->reg_lock); |
| 1142 | return -EIO; |
| 1143 | } |
| 1144 | wp++; |
| 1145 | wp %= ICH6_MAX_CORB_ENTRIES; |
| 1146 | |
| 1147 | rp = azx_readw(chip, CORBRP); |
| 1148 | if (wp == rp) { |
| 1149 | /* oops, it's full */ |
| 1150 | spin_unlock_irq(&chip->reg_lock); |
| 1151 | return -EAGAIN; |
| 1152 | } |
| 1153 | |
| 1154 | chip->rirb.cmds[addr]++; |
| 1155 | chip->corb.buf[wp] = cpu_to_le32(val); |
| 1156 | azx_writew(chip, CORBWP, wp); |
| 1157 | |
| 1158 | spin_unlock_irq(&chip->reg_lock); |
| 1159 | |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | #define ICH6_RIRB_EX_UNSOL_EV (1<<4) |
| 1164 | |
| 1165 | /* retrieve RIRB entry - called from interrupt handler */ |
Dylan Reid | f0b1df8 | 2014-02-28 15:41:29 -0800 | [diff] [blame^] | 1166 | static void azx_update_rirb(struct azx *chip) |
Dylan Reid | 6e85ddd | 2014-02-28 15:41:25 -0800 | [diff] [blame] | 1167 | { |
| 1168 | unsigned int rp, wp; |
| 1169 | unsigned int addr; |
| 1170 | u32 res, res_ex; |
| 1171 | |
| 1172 | wp = azx_readw(chip, RIRBWP); |
| 1173 | if (wp == 0xffff) { |
| 1174 | /* something wrong, controller likely turned to D3 */ |
| 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | if (wp == chip->rirb.wp) |
| 1179 | return; |
| 1180 | chip->rirb.wp = wp; |
| 1181 | |
| 1182 | while (chip->rirb.rp != wp) { |
| 1183 | chip->rirb.rp++; |
| 1184 | chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES; |
| 1185 | |
| 1186 | rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */ |
| 1187 | res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]); |
| 1188 | res = le32_to_cpu(chip->rirb.buf[rp]); |
| 1189 | addr = res_ex & 0xf; |
| 1190 | if ((addr >= AZX_MAX_CODECS) || !(chip->codec_mask & (1 << addr))) { |
| 1191 | dev_err(chip->card->dev, "spurious response %#x:%#x, rp = %d, wp = %d", |
| 1192 | res, res_ex, |
| 1193 | chip->rirb.rp, wp); |
| 1194 | snd_BUG(); |
| 1195 | } |
| 1196 | else if (res_ex & ICH6_RIRB_EX_UNSOL_EV) |
| 1197 | snd_hda_queue_unsol_event(chip->bus, res, res_ex); |
| 1198 | else if (chip->rirb.cmds[addr]) { |
| 1199 | chip->rirb.res[addr] = res; |
| 1200 | smp_wmb(); |
| 1201 | chip->rirb.cmds[addr]--; |
| 1202 | } else if (printk_ratelimit()) { |
| 1203 | dev_err(chip->card->dev, "spurious response %#x:%#x, last cmd=%#08x\n", |
| 1204 | res, res_ex, |
| 1205 | chip->last_cmd[addr]); |
| 1206 | } |
| 1207 | } |
| 1208 | } |
Dylan Reid | 6e85ddd | 2014-02-28 15:41:25 -0800 | [diff] [blame] | 1209 | |
| 1210 | /* receive a response */ |
| 1211 | static unsigned int azx_rirb_get_response(struct hda_bus *bus, |
| 1212 | unsigned int addr) |
| 1213 | { |
| 1214 | struct azx *chip = bus->private_data; |
| 1215 | unsigned long timeout; |
| 1216 | unsigned long loopcounter; |
| 1217 | int do_poll = 0; |
| 1218 | |
| 1219 | again: |
| 1220 | timeout = jiffies + msecs_to_jiffies(1000); |
| 1221 | |
| 1222 | for (loopcounter = 0;; loopcounter++) { |
| 1223 | if (chip->polling_mode || do_poll) { |
| 1224 | spin_lock_irq(&chip->reg_lock); |
| 1225 | azx_update_rirb(chip); |
| 1226 | spin_unlock_irq(&chip->reg_lock); |
| 1227 | } |
| 1228 | if (!chip->rirb.cmds[addr]) { |
| 1229 | smp_rmb(); |
| 1230 | bus->rirb_error = 0; |
| 1231 | |
| 1232 | if (!do_poll) |
| 1233 | chip->poll_count = 0; |
| 1234 | return chip->rirb.res[addr]; /* the last value */ |
| 1235 | } |
| 1236 | if (time_after(jiffies, timeout)) |
| 1237 | break; |
| 1238 | if (bus->needs_damn_long_delay || loopcounter > 3000) |
| 1239 | msleep(2); /* temporary workaround */ |
| 1240 | else { |
| 1241 | udelay(10); |
| 1242 | cond_resched(); |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | if (!bus->no_response_fallback) |
| 1247 | return -1; |
| 1248 | |
| 1249 | if (!chip->polling_mode && chip->poll_count < 2) { |
| 1250 | dev_dbg(chip->card->dev, |
| 1251 | "azx_get_response timeout, polling the codec once: last cmd=0x%08x\n", |
| 1252 | chip->last_cmd[addr]); |
| 1253 | do_poll = 1; |
| 1254 | chip->poll_count++; |
| 1255 | goto again; |
| 1256 | } |
| 1257 | |
| 1258 | |
| 1259 | if (!chip->polling_mode) { |
| 1260 | dev_warn(chip->card->dev, |
| 1261 | "azx_get_response timeout, switching to polling mode: last cmd=0x%08x\n", |
| 1262 | chip->last_cmd[addr]); |
| 1263 | chip->polling_mode = 1; |
| 1264 | goto again; |
| 1265 | } |
| 1266 | |
| 1267 | if (chip->msi) { |
| 1268 | dev_warn(chip->card->dev, |
| 1269 | "No response from codec, disabling MSI: last cmd=0x%08x\n", |
| 1270 | chip->last_cmd[addr]); |
| 1271 | if (chip->ops->disable_msi_reset_irq(chip) && |
| 1272 | chip->ops->disable_msi_reset_irq(chip) < 0) { |
| 1273 | bus->rirb_error = 1; |
| 1274 | return -1; |
| 1275 | } |
| 1276 | goto again; |
| 1277 | } |
| 1278 | |
| 1279 | if (chip->probing) { |
| 1280 | /* If this critical timeout happens during the codec probing |
| 1281 | * phase, this is likely an access to a non-existing codec |
| 1282 | * slot. Better to return an error and reset the system. |
| 1283 | */ |
| 1284 | return -1; |
| 1285 | } |
| 1286 | |
| 1287 | /* a fatal communication error; need either to reset or to fallback |
| 1288 | * to the single_cmd mode |
| 1289 | */ |
| 1290 | bus->rirb_error = 1; |
| 1291 | if (bus->allow_bus_reset && !bus->response_reset && !bus->in_reset) { |
| 1292 | bus->response_reset = 1; |
| 1293 | return -1; /* give a chance to retry */ |
| 1294 | } |
| 1295 | |
| 1296 | dev_err(chip->card->dev, |
| 1297 | "azx_get_response timeout, switching to single_cmd mode: last cmd=0x%08x\n", |
| 1298 | chip->last_cmd[addr]); |
| 1299 | chip->single_cmd = 1; |
| 1300 | bus->response_reset = 0; |
| 1301 | /* release CORB/RIRB */ |
| 1302 | azx_free_cmd_io(chip); |
| 1303 | /* disable unsolicited responses */ |
| 1304 | azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_UNSOL); |
| 1305 | return -1; |
| 1306 | } |
| 1307 | |
| 1308 | /* |
| 1309 | * Use the single immediate command instead of CORB/RIRB for simplicity |
| 1310 | * |
| 1311 | * Note: according to Intel, this is not preferred use. The command was |
| 1312 | * intended for the BIOS only, and may get confused with unsolicited |
| 1313 | * responses. So, we shouldn't use it for normal operation from the |
| 1314 | * driver. |
| 1315 | * I left the codes, however, for debugging/testing purposes. |
| 1316 | */ |
| 1317 | |
| 1318 | /* receive a response */ |
| 1319 | static int azx_single_wait_for_response(struct azx *chip, unsigned int addr) |
| 1320 | { |
| 1321 | int timeout = 50; |
| 1322 | |
| 1323 | while (timeout--) { |
| 1324 | /* check IRV busy bit */ |
| 1325 | if (azx_readw(chip, IRS) & ICH6_IRS_VALID) { |
| 1326 | /* reuse rirb.res as the response return value */ |
| 1327 | chip->rirb.res[addr] = azx_readl(chip, IR); |
| 1328 | return 0; |
| 1329 | } |
| 1330 | udelay(1); |
| 1331 | } |
| 1332 | if (printk_ratelimit()) |
| 1333 | dev_dbg(chip->card->dev, "get_response timeout: IRS=0x%x\n", |
| 1334 | azx_readw(chip, IRS)); |
| 1335 | chip->rirb.res[addr] = -1; |
| 1336 | return -EIO; |
| 1337 | } |
| 1338 | |
| 1339 | /* send a command */ |
| 1340 | static int azx_single_send_cmd(struct hda_bus *bus, u32 val) |
| 1341 | { |
| 1342 | struct azx *chip = bus->private_data; |
| 1343 | unsigned int addr = azx_command_addr(val); |
| 1344 | int timeout = 50; |
| 1345 | |
| 1346 | bus->rirb_error = 0; |
| 1347 | while (timeout--) { |
| 1348 | /* check ICB busy bit */ |
| 1349 | if (!((azx_readw(chip, IRS) & ICH6_IRS_BUSY))) { |
| 1350 | /* Clear IRV valid bit */ |
| 1351 | azx_writew(chip, IRS, azx_readw(chip, IRS) | |
| 1352 | ICH6_IRS_VALID); |
| 1353 | azx_writel(chip, IC, val); |
| 1354 | azx_writew(chip, IRS, azx_readw(chip, IRS) | |
| 1355 | ICH6_IRS_BUSY); |
| 1356 | return azx_single_wait_for_response(chip, addr); |
| 1357 | } |
| 1358 | udelay(1); |
| 1359 | } |
| 1360 | if (printk_ratelimit()) |
| 1361 | dev_dbg(chip->card->dev, |
| 1362 | "send_cmd timeout: IRS=0x%x, val=0x%x\n", |
| 1363 | azx_readw(chip, IRS), val); |
| 1364 | return -EIO; |
| 1365 | } |
| 1366 | |
| 1367 | /* receive a response */ |
| 1368 | static unsigned int azx_single_get_response(struct hda_bus *bus, |
| 1369 | unsigned int addr) |
| 1370 | { |
| 1371 | struct azx *chip = bus->private_data; |
| 1372 | return chip->rirb.res[addr]; |
| 1373 | } |
| 1374 | |
| 1375 | /* |
| 1376 | * The below are the main callbacks from hda_codec. |
| 1377 | * |
| 1378 | * They are just the skeleton to call sub-callbacks according to the |
| 1379 | * current setting of chip->single_cmd. |
| 1380 | */ |
| 1381 | |
| 1382 | /* send a command */ |
| 1383 | int azx_send_cmd(struct hda_bus *bus, unsigned int val) |
| 1384 | { |
| 1385 | struct azx *chip = bus->private_data; |
| 1386 | |
| 1387 | if (chip->disabled) |
| 1388 | return 0; |
| 1389 | chip->last_cmd[azx_command_addr(val)] = val; |
| 1390 | if (chip->single_cmd) |
| 1391 | return azx_single_send_cmd(bus, val); |
| 1392 | else |
| 1393 | return azx_corb_send_cmd(bus, val); |
| 1394 | } |
| 1395 | EXPORT_SYMBOL_GPL(azx_send_cmd); |
| 1396 | |
| 1397 | /* get a response */ |
| 1398 | unsigned int azx_get_response(struct hda_bus *bus, |
| 1399 | unsigned int addr) |
| 1400 | { |
| 1401 | struct azx *chip = bus->private_data; |
| 1402 | if (chip->disabled) |
| 1403 | return 0; |
| 1404 | if (chip->single_cmd) |
| 1405 | return azx_single_get_response(bus, addr); |
| 1406 | else |
| 1407 | return azx_rirb_get_response(bus, addr); |
| 1408 | } |
| 1409 | EXPORT_SYMBOL_GPL(azx_get_response); |
| 1410 | |
Dylan Reid | 2b5fd6c | 2014-02-28 15:41:24 -0800 | [diff] [blame] | 1411 | #ifdef CONFIG_SND_HDA_DSP_LOADER |
| 1412 | /* |
| 1413 | * DSP loading code (e.g. for CA0132) |
| 1414 | */ |
| 1415 | |
| 1416 | /* use the first stream for loading DSP */ |
| 1417 | static struct azx_dev * |
| 1418 | azx_get_dsp_loader_dev(struct azx *chip) |
| 1419 | { |
| 1420 | return &chip->azx_dev[chip->playback_index_offset]; |
| 1421 | } |
| 1422 | |
| 1423 | int azx_load_dsp_prepare(struct hda_bus *bus, unsigned int format, |
| 1424 | unsigned int byte_size, |
| 1425 | struct snd_dma_buffer *bufp) |
| 1426 | { |
| 1427 | u32 *bdl; |
| 1428 | struct azx *chip = bus->private_data; |
| 1429 | struct azx_dev *azx_dev; |
| 1430 | int err; |
| 1431 | |
| 1432 | azx_dev = azx_get_dsp_loader_dev(chip); |
| 1433 | |
| 1434 | dsp_lock(azx_dev); |
| 1435 | spin_lock_irq(&chip->reg_lock); |
| 1436 | if (azx_dev->running || azx_dev->locked) { |
| 1437 | spin_unlock_irq(&chip->reg_lock); |
| 1438 | err = -EBUSY; |
| 1439 | goto unlock; |
| 1440 | } |
| 1441 | azx_dev->prepared = 0; |
| 1442 | chip->saved_azx_dev = *azx_dev; |
| 1443 | azx_dev->locked = 1; |
| 1444 | spin_unlock_irq(&chip->reg_lock); |
| 1445 | |
| 1446 | err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV_SG, |
| 1447 | byte_size, bufp); |
| 1448 | if (err < 0) |
| 1449 | goto err_alloc; |
| 1450 | |
| 1451 | azx_dev->bufsize = byte_size; |
| 1452 | azx_dev->period_bytes = byte_size; |
| 1453 | azx_dev->format_val = format; |
| 1454 | |
| 1455 | azx_stream_reset(chip, azx_dev); |
| 1456 | |
| 1457 | /* reset BDL address */ |
| 1458 | azx_sd_writel(chip, azx_dev, SD_BDLPL, 0); |
| 1459 | azx_sd_writel(chip, azx_dev, SD_BDLPU, 0); |
| 1460 | |
| 1461 | azx_dev->frags = 0; |
| 1462 | bdl = (u32 *)azx_dev->bdl.area; |
| 1463 | err = setup_bdle(chip, bufp, azx_dev, &bdl, 0, byte_size, 0); |
| 1464 | if (err < 0) |
| 1465 | goto error; |
| 1466 | |
| 1467 | azx_setup_controller(chip, azx_dev); |
| 1468 | dsp_unlock(azx_dev); |
| 1469 | return azx_dev->stream_tag; |
| 1470 | |
| 1471 | error: |
| 1472 | chip->ops->dma_free_pages(chip, bufp); |
| 1473 | err_alloc: |
| 1474 | spin_lock_irq(&chip->reg_lock); |
| 1475 | if (azx_dev->opened) |
| 1476 | *azx_dev = chip->saved_azx_dev; |
| 1477 | azx_dev->locked = 0; |
| 1478 | spin_unlock_irq(&chip->reg_lock); |
| 1479 | unlock: |
| 1480 | dsp_unlock(azx_dev); |
| 1481 | return err; |
| 1482 | } |
| 1483 | EXPORT_SYMBOL_GPL(azx_load_dsp_prepare); |
| 1484 | |
| 1485 | void azx_load_dsp_trigger(struct hda_bus *bus, bool start) |
| 1486 | { |
| 1487 | struct azx *chip = bus->private_data; |
| 1488 | struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip); |
| 1489 | |
| 1490 | if (start) |
| 1491 | azx_stream_start(chip, azx_dev); |
| 1492 | else |
| 1493 | azx_stream_stop(chip, azx_dev); |
| 1494 | azx_dev->running = start; |
| 1495 | } |
| 1496 | EXPORT_SYMBOL_GPL(azx_load_dsp_trigger); |
| 1497 | |
| 1498 | void azx_load_dsp_cleanup(struct hda_bus *bus, |
| 1499 | struct snd_dma_buffer *dmab) |
| 1500 | { |
| 1501 | struct azx *chip = bus->private_data; |
| 1502 | struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip); |
| 1503 | |
| 1504 | if (!dmab->area || !azx_dev->locked) |
| 1505 | return; |
| 1506 | |
| 1507 | dsp_lock(azx_dev); |
| 1508 | /* reset BDL address */ |
| 1509 | azx_sd_writel(chip, azx_dev, SD_BDLPL, 0); |
| 1510 | azx_sd_writel(chip, azx_dev, SD_BDLPU, 0); |
| 1511 | azx_sd_writel(chip, azx_dev, SD_CTL, 0); |
| 1512 | azx_dev->bufsize = 0; |
| 1513 | azx_dev->period_bytes = 0; |
| 1514 | azx_dev->format_val = 0; |
| 1515 | |
| 1516 | chip->ops->dma_free_pages(chip, dmab); |
| 1517 | dmab->area = NULL; |
| 1518 | |
| 1519 | spin_lock_irq(&chip->reg_lock); |
| 1520 | if (azx_dev->opened) |
| 1521 | *azx_dev = chip->saved_azx_dev; |
| 1522 | azx_dev->locked = 0; |
| 1523 | spin_unlock_irq(&chip->reg_lock); |
| 1524 | dsp_unlock(azx_dev); |
| 1525 | } |
| 1526 | EXPORT_SYMBOL_GPL(azx_load_dsp_cleanup); |
| 1527 | #endif /* CONFIG_SND_HDA_DSP_LOADER */ |
| 1528 | |
Dylan Reid | 6790899 | 2014-02-28 15:41:23 -0800 | [diff] [blame] | 1529 | int azx_alloc_stream_pages(struct azx *chip) |
| 1530 | { |
| 1531 | int i, err; |
| 1532 | struct snd_card *card = chip->card; |
| 1533 | |
| 1534 | for (i = 0; i < chip->num_streams; i++) { |
| 1535 | dsp_lock_init(&chip->azx_dev[i]); |
| 1536 | /* allocate memory for the BDL for each stream */ |
| 1537 | err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV, |
| 1538 | BDL_SIZE, |
| 1539 | &chip->azx_dev[i].bdl); |
| 1540 | if (err < 0) { |
| 1541 | dev_err(card->dev, "cannot allocate BDL\n"); |
| 1542 | return -ENOMEM; |
| 1543 | } |
| 1544 | } |
| 1545 | /* allocate memory for the position buffer */ |
| 1546 | err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV, |
| 1547 | chip->num_streams * 8, &chip->posbuf); |
| 1548 | if (err < 0) { |
| 1549 | dev_err(card->dev, "cannot allocate posbuf\n"); |
| 1550 | return -ENOMEM; |
| 1551 | } |
Dylan Reid | f19c3ec | 2014-02-28 15:41:26 -0800 | [diff] [blame] | 1552 | |
| 1553 | /* allocate CORB/RIRB */ |
| 1554 | err = azx_alloc_cmd_io(chip); |
| 1555 | if (err < 0) |
| 1556 | return err; |
Dylan Reid | 6790899 | 2014-02-28 15:41:23 -0800 | [diff] [blame] | 1557 | return 0; |
| 1558 | } |
| 1559 | EXPORT_SYMBOL_GPL(azx_alloc_stream_pages); |
| 1560 | |
| 1561 | void azx_free_stream_pages(struct azx *chip) |
| 1562 | { |
| 1563 | int i; |
| 1564 | if (chip->azx_dev) { |
| 1565 | for (i = 0; i < chip->num_streams; i++) |
| 1566 | if (chip->azx_dev[i].bdl.area) |
| 1567 | chip->ops->dma_free_pages( |
| 1568 | chip, &chip->azx_dev[i].bdl); |
| 1569 | } |
| 1570 | if (chip->rb.area) |
| 1571 | chip->ops->dma_free_pages(chip, &chip->rb); |
| 1572 | if (chip->posbuf.area) |
| 1573 | chip->ops->dma_free_pages(chip, &chip->posbuf); |
| 1574 | } |
| 1575 | EXPORT_SYMBOL_GPL(azx_free_stream_pages); |
| 1576 | |
Dylan Reid | f43923f | 2014-02-28 15:41:27 -0800 | [diff] [blame] | 1577 | /* |
| 1578 | * Lowlevel interface |
| 1579 | */ |
| 1580 | |
| 1581 | /* enter link reset */ |
| 1582 | void azx_enter_link_reset(struct azx *chip) |
| 1583 | { |
| 1584 | unsigned long timeout; |
| 1585 | |
| 1586 | /* reset controller */ |
| 1587 | azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_RESET); |
| 1588 | |
| 1589 | timeout = jiffies + msecs_to_jiffies(100); |
| 1590 | while ((azx_readb(chip, GCTL) & ICH6_GCTL_RESET) && |
| 1591 | time_before(jiffies, timeout)) |
| 1592 | usleep_range(500, 1000); |
| 1593 | } |
| 1594 | EXPORT_SYMBOL_GPL(azx_enter_link_reset); |
| 1595 | |
| 1596 | /* exit link reset */ |
| 1597 | static void azx_exit_link_reset(struct azx *chip) |
| 1598 | { |
| 1599 | unsigned long timeout; |
| 1600 | |
| 1601 | azx_writeb(chip, GCTL, azx_readb(chip, GCTL) | ICH6_GCTL_RESET); |
| 1602 | |
| 1603 | timeout = jiffies + msecs_to_jiffies(100); |
| 1604 | while (!azx_readb(chip, GCTL) && |
| 1605 | time_before(jiffies, timeout)) |
| 1606 | usleep_range(500, 1000); |
| 1607 | } |
| 1608 | |
| 1609 | /* reset codec link */ |
| 1610 | static int azx_reset(struct azx *chip, int full_reset) |
| 1611 | { |
| 1612 | if (!full_reset) |
| 1613 | goto __skip; |
| 1614 | |
| 1615 | /* clear STATESTS */ |
| 1616 | azx_writew(chip, STATESTS, STATESTS_INT_MASK); |
| 1617 | |
| 1618 | /* reset controller */ |
| 1619 | azx_enter_link_reset(chip); |
| 1620 | |
| 1621 | /* delay for >= 100us for codec PLL to settle per spec |
| 1622 | * Rev 0.9 section 5.5.1 |
| 1623 | */ |
| 1624 | usleep_range(500, 1000); |
| 1625 | |
| 1626 | /* Bring controller out of reset */ |
| 1627 | azx_exit_link_reset(chip); |
| 1628 | |
| 1629 | /* Brent Chartrand said to wait >= 540us for codecs to initialize */ |
| 1630 | usleep_range(1000, 1200); |
| 1631 | |
| 1632 | __skip: |
| 1633 | /* check to see if controller is ready */ |
| 1634 | if (!azx_readb(chip, GCTL)) { |
| 1635 | dev_dbg(chip->card->dev, "azx_reset: controller not ready!\n"); |
| 1636 | return -EBUSY; |
| 1637 | } |
| 1638 | |
| 1639 | /* Accept unsolicited responses */ |
| 1640 | if (!chip->single_cmd) |
| 1641 | azx_writel(chip, GCTL, azx_readl(chip, GCTL) | |
| 1642 | ICH6_GCTL_UNSOL); |
| 1643 | |
| 1644 | /* detect codecs */ |
| 1645 | if (!chip->codec_mask) { |
| 1646 | chip->codec_mask = azx_readw(chip, STATESTS); |
| 1647 | dev_dbg(chip->card->dev, "codec_mask = 0x%x\n", |
| 1648 | chip->codec_mask); |
| 1649 | } |
| 1650 | |
| 1651 | return 0; |
| 1652 | } |
| 1653 | |
| 1654 | /* enable interrupts */ |
| 1655 | static void azx_int_enable(struct azx *chip) |
| 1656 | { |
| 1657 | /* enable controller CIE and GIE */ |
| 1658 | azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) | |
| 1659 | ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN); |
| 1660 | } |
| 1661 | |
| 1662 | /* disable interrupts */ |
| 1663 | static void azx_int_disable(struct azx *chip) |
| 1664 | { |
| 1665 | int i; |
| 1666 | |
| 1667 | /* disable interrupts in stream descriptor */ |
| 1668 | for (i = 0; i < chip->num_streams; i++) { |
| 1669 | struct azx_dev *azx_dev = &chip->azx_dev[i]; |
| 1670 | azx_sd_writeb(chip, azx_dev, SD_CTL, |
| 1671 | azx_sd_readb(chip, azx_dev, SD_CTL) & |
| 1672 | ~SD_INT_MASK); |
| 1673 | } |
| 1674 | |
| 1675 | /* disable SIE for all streams */ |
| 1676 | azx_writeb(chip, INTCTL, 0); |
| 1677 | |
| 1678 | /* disable controller CIE and GIE */ |
| 1679 | azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) & |
| 1680 | ~(ICH6_INT_CTRL_EN | ICH6_INT_GLOBAL_EN)); |
| 1681 | } |
| 1682 | |
| 1683 | /* clear interrupts */ |
| 1684 | static void azx_int_clear(struct azx *chip) |
| 1685 | { |
| 1686 | int i; |
| 1687 | |
| 1688 | /* clear stream status */ |
| 1689 | for (i = 0; i < chip->num_streams; i++) { |
| 1690 | struct azx_dev *azx_dev = &chip->azx_dev[i]; |
| 1691 | azx_sd_writeb(chip, azx_dev, SD_STS, SD_INT_MASK); |
| 1692 | } |
| 1693 | |
| 1694 | /* clear STATESTS */ |
| 1695 | azx_writew(chip, STATESTS, STATESTS_INT_MASK); |
| 1696 | |
| 1697 | /* clear rirb status */ |
| 1698 | azx_writeb(chip, RIRBSTS, RIRB_INT_MASK); |
| 1699 | |
| 1700 | /* clear int status */ |
| 1701 | azx_writel(chip, INTSTS, ICH6_INT_CTRL_EN | ICH6_INT_ALL_STREAM); |
| 1702 | } |
| 1703 | |
| 1704 | /* |
| 1705 | * reset and start the controller registers |
| 1706 | */ |
| 1707 | void azx_init_chip(struct azx *chip, int full_reset) |
| 1708 | { |
| 1709 | if (chip->initialized) |
| 1710 | return; |
| 1711 | |
| 1712 | /* reset controller */ |
| 1713 | azx_reset(chip, full_reset); |
| 1714 | |
| 1715 | /* initialize interrupts */ |
| 1716 | azx_int_clear(chip); |
| 1717 | azx_int_enable(chip); |
| 1718 | |
| 1719 | /* initialize the codec command I/O */ |
| 1720 | if (!chip->single_cmd) |
| 1721 | azx_init_cmd_io(chip); |
| 1722 | |
| 1723 | /* program the position buffer */ |
| 1724 | azx_writel(chip, DPLBASE, (u32)chip->posbuf.addr); |
| 1725 | azx_writel(chip, DPUBASE, upper_32_bits(chip->posbuf.addr)); |
| 1726 | |
| 1727 | chip->initialized = 1; |
| 1728 | } |
| 1729 | EXPORT_SYMBOL_GPL(azx_init_chip); |
| 1730 | |
| 1731 | void azx_stop_chip(struct azx *chip) |
| 1732 | { |
| 1733 | if (!chip->initialized) |
| 1734 | return; |
| 1735 | |
| 1736 | /* disable interrupts */ |
| 1737 | azx_int_disable(chip); |
| 1738 | azx_int_clear(chip); |
| 1739 | |
| 1740 | /* disable CORB/RIRB */ |
| 1741 | azx_free_cmd_io(chip); |
| 1742 | |
| 1743 | /* disable position buffer */ |
| 1744 | azx_writel(chip, DPLBASE, 0); |
| 1745 | azx_writel(chip, DPUBASE, 0); |
| 1746 | |
| 1747 | chip->initialized = 0; |
| 1748 | } |
| 1749 | |
Dylan Reid | f0b1df8 | 2014-02-28 15:41:29 -0800 | [diff] [blame^] | 1750 | /* |
| 1751 | * interrupt handler |
| 1752 | */ |
| 1753 | irqreturn_t azx_interrupt(int irq, void *dev_id) |
| 1754 | { |
| 1755 | struct azx *chip = dev_id; |
| 1756 | struct azx_dev *azx_dev; |
| 1757 | u32 status; |
| 1758 | u8 sd_status; |
| 1759 | int i; |
| 1760 | |
| 1761 | #ifdef CONFIG_PM_RUNTIME |
| 1762 | if (chip->driver_caps & AZX_DCAPS_PM_RUNTIME) |
| 1763 | if (chip->card->dev->power.runtime_status != RPM_ACTIVE) |
| 1764 | return IRQ_NONE; |
| 1765 | #endif |
| 1766 | |
| 1767 | spin_lock(&chip->reg_lock); |
| 1768 | |
| 1769 | if (chip->disabled) { |
| 1770 | spin_unlock(&chip->reg_lock); |
| 1771 | return IRQ_NONE; |
| 1772 | } |
| 1773 | |
| 1774 | status = azx_readl(chip, INTSTS); |
| 1775 | if (status == 0 || status == 0xffffffff) { |
| 1776 | spin_unlock(&chip->reg_lock); |
| 1777 | return IRQ_NONE; |
| 1778 | } |
| 1779 | |
| 1780 | for (i = 0; i < chip->num_streams; i++) { |
| 1781 | azx_dev = &chip->azx_dev[i]; |
| 1782 | if (status & azx_dev->sd_int_sta_mask) { |
| 1783 | sd_status = azx_sd_readb(chip, azx_dev, SD_STS); |
| 1784 | azx_sd_writeb(chip, azx_dev, SD_STS, SD_INT_MASK); |
| 1785 | if (!azx_dev->substream || !azx_dev->running || |
| 1786 | !(sd_status & SD_INT_COMPLETE)) |
| 1787 | continue; |
| 1788 | /* check whether this IRQ is really acceptable */ |
| 1789 | if (!chip->ops->position_check || |
| 1790 | chip->ops->position_check(chip, azx_dev)) { |
| 1791 | spin_unlock(&chip->reg_lock); |
| 1792 | snd_pcm_period_elapsed(azx_dev->substream); |
| 1793 | spin_lock(&chip->reg_lock); |
| 1794 | } |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | /* clear rirb int */ |
| 1799 | status = azx_readb(chip, RIRBSTS); |
| 1800 | if (status & RIRB_INT_MASK) { |
| 1801 | if (status & RIRB_INT_RESPONSE) { |
| 1802 | if (chip->driver_caps & AZX_DCAPS_RIRB_PRE_DELAY) |
| 1803 | udelay(80); |
| 1804 | azx_update_rirb(chip); |
| 1805 | } |
| 1806 | azx_writeb(chip, RIRBSTS, RIRB_INT_MASK); |
| 1807 | } |
| 1808 | |
| 1809 | spin_unlock(&chip->reg_lock); |
| 1810 | |
| 1811 | return IRQ_HANDLED; |
| 1812 | } |
| 1813 | EXPORT_SYMBOL_GPL(azx_interrupt); |
| 1814 | |
Dylan Reid | 05e8487 | 2014-02-28 15:41:22 -0800 | [diff] [blame] | 1815 | MODULE_LICENSE("GPL"); |
| 1816 | MODULE_DESCRIPTION("Common HDA driver funcitons"); |