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