Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Support for Digigram Lola PCI-e boards |
| 4 | * |
| 5 | * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de> |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/dma-mapping.h> |
| 11 | #include <linux/pci.h> |
Takashi Iwai | f044785 | 2011-05-03 18:21:01 +0200 | [diff] [blame] | 12 | #include <linux/delay.h> |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 13 | #include <sound/core.h> |
| 14 | #include <sound/pcm.h> |
| 15 | #include "lola.h" |
| 16 | |
Takashi Iwai | 333ff39 | 2011-05-03 16:41:02 +0200 | [diff] [blame] | 17 | #define LOLA_MAX_BDL_ENTRIES 8 |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 18 | #define LOLA_MAX_BUF_SIZE (1024*1024*1024) |
Takashi Iwai | 333ff39 | 2011-05-03 16:41:02 +0200 | [diff] [blame] | 19 | #define LOLA_BDL_ENTRY_SIZE (16 * 16) |
| 20 | |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 21 | static struct lola_pcm *lola_get_pcm(struct snd_pcm_substream *substream) |
| 22 | { |
| 23 | struct lola *chip = snd_pcm_substream_chip(substream); |
| 24 | return &chip->pcm[substream->stream]; |
| 25 | } |
| 26 | |
| 27 | static struct lola_stream *lola_get_stream(struct snd_pcm_substream *substream) |
| 28 | { |
| 29 | struct lola_pcm *pcm = lola_get_pcm(substream); |
| 30 | unsigned int idx = substream->number; |
| 31 | return &pcm->streams[idx]; |
| 32 | } |
| 33 | |
| 34 | static unsigned int lola_get_lrc(struct lola *chip) |
| 35 | { |
| 36 | return lola_readl(chip, BAR1, LRC); |
| 37 | } |
| 38 | |
| 39 | static unsigned int lola_get_tstamp(struct lola *chip, bool quick_no_sync) |
| 40 | { |
| 41 | unsigned int tstamp = lola_get_lrc(chip) >> 8; |
| 42 | if (chip->granularity) { |
| 43 | unsigned int wait_banks = quick_no_sync ? 0 : 8; |
| 44 | tstamp += (wait_banks + 1) * chip->granularity - 1; |
| 45 | tstamp -= tstamp % chip->granularity; |
| 46 | } |
| 47 | return tstamp << 8; |
| 48 | } |
| 49 | |
| 50 | /* clear any pending interrupt status */ |
| 51 | static void lola_stream_clear_pending_irq(struct lola *chip, |
| 52 | struct lola_stream *str) |
| 53 | { |
| 54 | unsigned int val = lola_dsd_read(chip, str->dsd, STS); |
| 55 | val &= LOLA_DSD_STS_DESE | LOLA_DSD_STS_BCIS; |
| 56 | if (val) |
| 57 | lola_dsd_write(chip, str->dsd, STS, val); |
| 58 | } |
| 59 | |
| 60 | static void lola_stream_start(struct lola *chip, struct lola_stream *str, |
| 61 | unsigned int tstamp) |
| 62 | { |
| 63 | lola_stream_clear_pending_irq(chip, str); |
| 64 | lola_dsd_write(chip, str->dsd, CTL, |
| 65 | LOLA_DSD_CTL_SRUN | |
| 66 | LOLA_DSD_CTL_IOCE | |
| 67 | LOLA_DSD_CTL_DEIE | |
| 68 | LOLA_DSD_CTL_VLRCV | |
| 69 | tstamp); |
| 70 | } |
| 71 | |
| 72 | static void lola_stream_stop(struct lola *chip, struct lola_stream *str, |
| 73 | unsigned int tstamp) |
| 74 | { |
| 75 | lola_dsd_write(chip, str->dsd, CTL, |
| 76 | LOLA_DSD_CTL_IOCE | |
| 77 | LOLA_DSD_CTL_DEIE | |
| 78 | LOLA_DSD_CTL_VLRCV | |
| 79 | tstamp); |
| 80 | lola_stream_clear_pending_irq(chip, str); |
| 81 | } |
| 82 | |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 83 | static void wait_for_srst_clear(struct lola *chip, struct lola_stream *str) |
| 84 | { |
Takashi Iwai | 333ff39 | 2011-05-03 16:41:02 +0200 | [diff] [blame] | 85 | unsigned long end_time = jiffies + msecs_to_jiffies(200); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 86 | while (time_before(jiffies, end_time)) { |
| 87 | unsigned int val; |
| 88 | val = lola_dsd_read(chip, str->dsd, CTL); |
| 89 | if (!(val & LOLA_DSD_CTL_SRST)) |
| 90 | return; |
| 91 | msleep(1); |
| 92 | } |
Takashi Iwai | f58e2fc | 2014-02-25 17:23:57 +0100 | [diff] [blame] | 93 | dev_warn(chip->card->dev, "SRST not clear (stream %d)\n", str->dsd); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 96 | static int lola_stream_wait_for_fifo(struct lola *chip, |
| 97 | struct lola_stream *str, |
| 98 | bool ready) |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 99 | { |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 100 | unsigned int val = ready ? LOLA_DSD_STS_FIFORDY : 0; |
| 101 | unsigned long end_time = jiffies + msecs_to_jiffies(200); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 102 | while (time_before(jiffies, end_time)) { |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 103 | unsigned int reg = lola_dsd_read(chip, str->dsd, STS); |
| 104 | if ((reg & LOLA_DSD_STS_FIFORDY) == val) |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 105 | return 0; |
| 106 | msleep(1); |
| 107 | } |
Takashi Iwai | f58e2fc | 2014-02-25 17:23:57 +0100 | [diff] [blame] | 108 | dev_warn(chip->card->dev, "FIFO not ready (stream %d)\n", str->dsd); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 109 | return -EIO; |
| 110 | } |
| 111 | |
Takashi Iwai | c7aad3c | 2011-05-03 17:02:35 +0200 | [diff] [blame] | 112 | /* sync for FIFO ready/empty for all linked streams; |
| 113 | * clear paused flag when FIFO gets ready again |
| 114 | */ |
| 115 | static int lola_sync_wait_for_fifo(struct lola *chip, |
| 116 | struct snd_pcm_substream *substream, |
| 117 | bool ready) |
| 118 | { |
| 119 | unsigned int val = ready ? LOLA_DSD_STS_FIFORDY : 0; |
| 120 | unsigned long end_time = jiffies + msecs_to_jiffies(200); |
| 121 | struct snd_pcm_substream *s; |
| 122 | int pending = 0; |
| 123 | |
| 124 | while (time_before(jiffies, end_time)) { |
| 125 | pending = 0; |
| 126 | snd_pcm_group_for_each_entry(s, substream) { |
| 127 | struct lola_stream *str; |
| 128 | if (s->pcm->card != substream->pcm->card) |
| 129 | continue; |
| 130 | str = lola_get_stream(s); |
| 131 | if (str->prepared && str->paused) { |
| 132 | unsigned int reg; |
| 133 | reg = lola_dsd_read(chip, str->dsd, STS); |
| 134 | if ((reg & LOLA_DSD_STS_FIFORDY) != val) { |
| 135 | pending = str->dsd + 1; |
| 136 | break; |
| 137 | } |
| 138 | if (ready) |
| 139 | str->paused = 0; |
| 140 | } |
| 141 | } |
| 142 | if (!pending) |
| 143 | return 0; |
| 144 | msleep(1); |
| 145 | } |
Takashi Iwai | f58e2fc | 2014-02-25 17:23:57 +0100 | [diff] [blame] | 146 | dev_warn(chip->card->dev, "FIFO not ready (pending %d)\n", pending - 1); |
Takashi Iwai | c7aad3c | 2011-05-03 17:02:35 +0200 | [diff] [blame] | 147 | return -EIO; |
| 148 | } |
| 149 | |
| 150 | /* finish pause - prepare for a new resume */ |
| 151 | static void lola_sync_pause(struct lola *chip, |
| 152 | struct snd_pcm_substream *substream) |
| 153 | { |
| 154 | struct snd_pcm_substream *s; |
| 155 | |
| 156 | lola_sync_wait_for_fifo(chip, substream, false); |
| 157 | snd_pcm_group_for_each_entry(s, substream) { |
| 158 | struct lola_stream *str; |
| 159 | if (s->pcm->card != substream->pcm->card) |
| 160 | continue; |
| 161 | str = lola_get_stream(s); |
| 162 | if (str->paused && str->prepared) |
| 163 | lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRUN | |
| 164 | LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE); |
| 165 | } |
| 166 | lola_sync_wait_for_fifo(chip, substream, true); |
| 167 | } |
| 168 | |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 169 | static void lola_stream_reset(struct lola *chip, struct lola_stream *str) |
| 170 | { |
| 171 | if (str->prepared) { |
Takashi Iwai | c7aad3c | 2011-05-03 17:02:35 +0200 | [diff] [blame] | 172 | if (str->paused) |
| 173 | lola_sync_pause(chip, str->substream); |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 174 | str->prepared = 0; |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 175 | lola_dsd_write(chip, str->dsd, CTL, |
| 176 | LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE); |
| 177 | lola_stream_wait_for_fifo(chip, str, false); |
| 178 | lola_stream_clear_pending_irq(chip, str); |
| 179 | lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRST); |
| 180 | lola_dsd_write(chip, str->dsd, LVI, 0); |
| 181 | lola_dsd_write(chip, str->dsd, BDPU, 0); |
| 182 | lola_dsd_write(chip, str->dsd, BDPL, 0); |
| 183 | wait_for_srst_clear(chip, str); |
| 184 | } |
| 185 | } |
| 186 | |
Bhumika Goyal | 5cf8479 | 2017-08-12 21:01:19 +0530 | [diff] [blame] | 187 | static const struct snd_pcm_hardware lola_pcm_hw = { |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 188 | .info = (SNDRV_PCM_INFO_MMAP | |
| 189 | SNDRV_PCM_INFO_INTERLEAVED | |
| 190 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 191 | SNDRV_PCM_INFO_MMAP_VALID | |
| 192 | SNDRV_PCM_INFO_PAUSE), |
| 193 | .formats = (SNDRV_PCM_FMTBIT_S16_LE | |
| 194 | SNDRV_PCM_FMTBIT_S24_LE | |
| 195 | SNDRV_PCM_FMTBIT_S32_LE | |
| 196 | SNDRV_PCM_FMTBIT_FLOAT_LE), |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 197 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 198 | .rate_min = 8000, |
| 199 | .rate_max = 192000, |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 200 | .channels_min = 1, |
| 201 | .channels_max = 2, |
| 202 | .buffer_bytes_max = LOLA_MAX_BUF_SIZE, |
| 203 | .period_bytes_min = 128, |
| 204 | .period_bytes_max = LOLA_MAX_BUF_SIZE / 2, |
| 205 | .periods_min = 2, |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 206 | .periods_max = LOLA_MAX_BDL_ENTRIES, |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 207 | .fifo_size = 0, |
| 208 | }; |
| 209 | |
| 210 | static int lola_pcm_open(struct snd_pcm_substream *substream) |
| 211 | { |
| 212 | struct lola *chip = snd_pcm_substream_chip(substream); |
| 213 | struct lola_pcm *pcm = lola_get_pcm(substream); |
| 214 | struct lola_stream *str = lola_get_stream(substream); |
| 215 | struct snd_pcm_runtime *runtime = substream->runtime; |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 216 | |
| 217 | mutex_lock(&chip->open_mutex); |
| 218 | if (str->opened) { |
| 219 | mutex_unlock(&chip->open_mutex); |
| 220 | return -EBUSY; |
| 221 | } |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 222 | str->substream = substream; |
| 223 | str->master = NULL; |
| 224 | str->opened = 1; |
| 225 | runtime->hw = lola_pcm_hw; |
| 226 | runtime->hw.channels_max = pcm->num_streams - str->index; |
Takashi Iwai | 7e79f22 | 2011-05-03 16:59:27 +0200 | [diff] [blame] | 227 | if (chip->sample_rate) { |
| 228 | /* sample rate is locked */ |
| 229 | runtime->hw.rate_min = chip->sample_rate; |
| 230 | runtime->hw.rate_max = chip->sample_rate; |
| 231 | } else { |
| 232 | runtime->hw.rate_min = chip->sample_rate_min; |
| 233 | runtime->hw.rate_max = chip->sample_rate_max; |
| 234 | } |
| 235 | chip->ref_count_rate++; |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 236 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
Takashi Iwai | 7e79f22 | 2011-05-03 16:59:27 +0200 | [diff] [blame] | 237 | /* period size = multiple of chip->granularity (8, 16 or 32 frames)*/ |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 238 | snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
Takashi Iwai | 8bd172d | 2011-05-03 16:51:56 +0200 | [diff] [blame] | 239 | chip->granularity); |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 240 | snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
Takashi Iwai | 8bd172d | 2011-05-03 16:51:56 +0200 | [diff] [blame] | 241 | chip->granularity); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 242 | mutex_unlock(&chip->open_mutex); |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static void lola_cleanup_slave_streams(struct lola_pcm *pcm, |
| 247 | struct lola_stream *str) |
| 248 | { |
| 249 | int i; |
| 250 | for (i = str->index + 1; i < pcm->num_streams; i++) { |
| 251 | struct lola_stream *s = &pcm->streams[i]; |
| 252 | if (s->master != str) |
| 253 | break; |
| 254 | s->master = NULL; |
| 255 | s->opened = 0; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | static int lola_pcm_close(struct snd_pcm_substream *substream) |
| 260 | { |
| 261 | struct lola *chip = snd_pcm_substream_chip(substream); |
| 262 | struct lola_stream *str = lola_get_stream(substream); |
| 263 | |
| 264 | mutex_lock(&chip->open_mutex); |
| 265 | if (str->substream == substream) { |
| 266 | str->substream = NULL; |
| 267 | str->opened = 0; |
| 268 | } |
Takashi Iwai | 7e79f22 | 2011-05-03 16:59:27 +0200 | [diff] [blame] | 269 | if (--chip->ref_count_rate == 0) { |
| 270 | /* release sample rate */ |
| 271 | chip->sample_rate = 0; |
| 272 | } |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 273 | mutex_unlock(&chip->open_mutex); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static int lola_pcm_hw_params(struct snd_pcm_substream *substream, |
| 278 | struct snd_pcm_hw_params *hw_params) |
| 279 | { |
| 280 | struct lola_stream *str = lola_get_stream(substream); |
| 281 | |
| 282 | str->bufsize = 0; |
| 283 | str->period_bytes = 0; |
| 284 | str->format_verb = 0; |
Takashi Iwai | 224a40c | 2019-12-09 10:49:16 +0100 | [diff] [blame] | 285 | return 0; |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static int lola_pcm_hw_free(struct snd_pcm_substream *substream) |
| 289 | { |
| 290 | struct lola *chip = snd_pcm_substream_chip(substream); |
| 291 | struct lola_pcm *pcm = lola_get_pcm(substream); |
| 292 | struct lola_stream *str = lola_get_stream(substream); |
| 293 | |
| 294 | mutex_lock(&chip->open_mutex); |
| 295 | lola_stream_reset(chip, str); |
| 296 | lola_cleanup_slave_streams(pcm, str); |
| 297 | mutex_unlock(&chip->open_mutex); |
Takashi Iwai | 224a40c | 2019-12-09 10:49:16 +0100 | [diff] [blame] | 298 | return 0; |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
| 302 | * set up a BDL entry |
| 303 | */ |
| 304 | static int setup_bdle(struct snd_pcm_substream *substream, |
Takashi Iwai | 0d9a26f | 2018-07-25 23:24:06 +0200 | [diff] [blame] | 305 | struct lola_stream *str, __le32 **bdlp, |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 306 | int ofs, int size) |
| 307 | { |
Takashi Iwai | 0d9a26f | 2018-07-25 23:24:06 +0200 | [diff] [blame] | 308 | __le32 *bdl = *bdlp; |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 309 | |
| 310 | while (size > 0) { |
| 311 | dma_addr_t addr; |
| 312 | int chunk; |
| 313 | |
| 314 | if (str->frags >= LOLA_MAX_BDL_ENTRIES) |
| 315 | return -EINVAL; |
| 316 | |
Takashi Iwai | 972505c | 2011-05-03 16:50:51 +0200 | [diff] [blame] | 317 | addr = snd_pcm_sgbuf_get_addr(substream, ofs); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 318 | /* program the address field of the BDL entry */ |
| 319 | bdl[0] = cpu_to_le32((u32)addr); |
| 320 | bdl[1] = cpu_to_le32(upper_32_bits(addr)); |
| 321 | /* program the size field of the BDL entry */ |
Takashi Iwai | 972505c | 2011-05-03 16:50:51 +0200 | [diff] [blame] | 322 | chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 323 | bdl[2] = cpu_to_le32(chunk); |
| 324 | /* program the IOC to enable interrupt |
| 325 | * only when the whole fragment is processed |
| 326 | */ |
| 327 | size -= chunk; |
| 328 | bdl[3] = size ? 0 : cpu_to_le32(0x01); |
| 329 | bdl += 4; |
| 330 | str->frags++; |
| 331 | ofs += chunk; |
| 332 | } |
| 333 | *bdlp = bdl; |
| 334 | return ofs; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * set up BDL entries |
| 339 | */ |
Takashi Iwai | 333ff39 | 2011-05-03 16:41:02 +0200 | [diff] [blame] | 340 | static int lola_setup_periods(struct lola *chip, struct lola_pcm *pcm, |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 341 | struct snd_pcm_substream *substream, |
| 342 | struct lola_stream *str) |
| 343 | { |
Takashi Iwai | 0d9a26f | 2018-07-25 23:24:06 +0200 | [diff] [blame] | 344 | __le32 *bdl; |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 345 | int i, ofs, periods, period_bytes; |
| 346 | |
| 347 | period_bytes = str->period_bytes; |
| 348 | periods = str->bufsize / period_bytes; |
| 349 | |
| 350 | /* program the initial BDL entries */ |
Takashi Iwai | 098fe3d | 2021-07-15 09:59:02 +0200 | [diff] [blame] | 351 | bdl = (__le32 *)(pcm->bdl->area + LOLA_BDL_ENTRY_SIZE * str->index); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 352 | ofs = 0; |
| 353 | str->frags = 0; |
| 354 | for (i = 0; i < periods; i++) { |
| 355 | ofs = setup_bdle(substream, str, &bdl, ofs, period_bytes); |
| 356 | if (ofs < 0) |
| 357 | goto error; |
| 358 | } |
| 359 | return 0; |
| 360 | |
| 361 | error: |
Takashi Iwai | f58e2fc | 2014-02-25 17:23:57 +0100 | [diff] [blame] | 362 | dev_err(chip->card->dev, "Too many BDL entries: buffer=%d, period=%d\n", |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 363 | str->bufsize, period_bytes); |
| 364 | return -EINVAL; |
| 365 | } |
| 366 | |
| 367 | static unsigned int lola_get_format_verb(struct snd_pcm_substream *substream) |
| 368 | { |
| 369 | unsigned int verb; |
| 370 | |
| 371 | switch (substream->runtime->format) { |
| 372 | case SNDRV_PCM_FORMAT_S16_LE: |
| 373 | verb = 0x00000000; |
| 374 | break; |
| 375 | case SNDRV_PCM_FORMAT_S24_LE: |
| 376 | verb = 0x00000200; |
| 377 | break; |
| 378 | case SNDRV_PCM_FORMAT_S32_LE: |
| 379 | verb = 0x00000300; |
| 380 | break; |
| 381 | case SNDRV_PCM_FORMAT_FLOAT_LE: |
| 382 | verb = 0x00001300; |
| 383 | break; |
| 384 | default: |
| 385 | return 0; |
| 386 | } |
| 387 | verb |= substream->runtime->channels; |
| 388 | return verb; |
| 389 | } |
| 390 | |
| 391 | static int lola_set_stream_config(struct lola *chip, |
| 392 | struct lola_stream *str, |
| 393 | int channels) |
| 394 | { |
| 395 | int i, err; |
| 396 | unsigned int verb, val; |
| 397 | |
| 398 | /* set format info for all channels |
| 399 | * (with only one command for the first channel) |
| 400 | */ |
| 401 | err = lola_codec_read(chip, str->nid, LOLA_VERB_SET_STREAM_FORMAT, |
| 402 | str->format_verb, 0, &val, NULL); |
| 403 | if (err < 0) { |
Takashi Iwai | f58e2fc | 2014-02-25 17:23:57 +0100 | [diff] [blame] | 404 | dev_err(chip->card->dev, "Cannot set stream format 0x%x\n", |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 405 | str->format_verb); |
| 406 | return err; |
| 407 | } |
| 408 | |
| 409 | /* update stream - channel config */ |
| 410 | for (i = 0; i < channels; i++) { |
| 411 | verb = (str->index << 6) | i; |
| 412 | err = lola_codec_read(chip, str[i].nid, |
| 413 | LOLA_VERB_SET_CHANNEL_STREAMID, 0, verb, |
| 414 | &val, NULL); |
| 415 | if (err < 0) { |
Takashi Iwai | f58e2fc | 2014-02-25 17:23:57 +0100 | [diff] [blame] | 416 | dev_err(chip->card->dev, |
| 417 | "Cannot set stream channel %d\n", i); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 418 | return err; |
| 419 | } |
| 420 | } |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * set up the SD for streaming |
| 426 | */ |
Takashi Iwai | 333ff39 | 2011-05-03 16:41:02 +0200 | [diff] [blame] | 427 | static int lola_setup_controller(struct lola *chip, struct lola_pcm *pcm, |
| 428 | struct lola_stream *str) |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 429 | { |
Takashi Iwai | 333ff39 | 2011-05-03 16:41:02 +0200 | [diff] [blame] | 430 | dma_addr_t bdl; |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 431 | |
| 432 | if (str->prepared) |
| 433 | return -EINVAL; |
| 434 | |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 435 | /* set up BDL */ |
Takashi Iwai | 098fe3d | 2021-07-15 09:59:02 +0200 | [diff] [blame] | 436 | bdl = pcm->bdl->addr + LOLA_BDL_ENTRY_SIZE * str->index; |
Takashi Iwai | 333ff39 | 2011-05-03 16:41:02 +0200 | [diff] [blame] | 437 | lola_dsd_write(chip, str->dsd, BDPL, (u32)bdl); |
| 438 | lola_dsd_write(chip, str->dsd, BDPU, upper_32_bits(bdl)); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 439 | /* program the stream LVI (last valid index) of the BDL */ |
| 440 | lola_dsd_write(chip, str->dsd, LVI, str->frags - 1); |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 441 | lola_stream_clear_pending_irq(chip, str); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 442 | |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 443 | lola_dsd_write(chip, str->dsd, CTL, |
| 444 | LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE | LOLA_DSD_CTL_SRUN); |
| 445 | |
| 446 | str->prepared = 1; |
| 447 | |
| 448 | return lola_stream_wait_for_fifo(chip, str, true); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static int lola_pcm_prepare(struct snd_pcm_substream *substream) |
| 452 | { |
| 453 | struct lola *chip = snd_pcm_substream_chip(substream); |
| 454 | struct lola_pcm *pcm = lola_get_pcm(substream); |
| 455 | struct lola_stream *str = lola_get_stream(substream); |
| 456 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 457 | unsigned int bufsize, period_bytes, format_verb; |
| 458 | int i, err; |
| 459 | |
| 460 | mutex_lock(&chip->open_mutex); |
| 461 | lola_stream_reset(chip, str); |
| 462 | lola_cleanup_slave_streams(pcm, str); |
Takashi Iwai | 2db3002 | 2011-05-03 17:05:08 +0200 | [diff] [blame] | 463 | if (str->index + runtime->channels > pcm->num_streams) { |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 464 | mutex_unlock(&chip->open_mutex); |
| 465 | return -EINVAL; |
| 466 | } |
| 467 | for (i = 1; i < runtime->channels; i++) { |
| 468 | str[i].master = str; |
| 469 | str[i].opened = 1; |
| 470 | } |
| 471 | mutex_unlock(&chip->open_mutex); |
| 472 | |
| 473 | bufsize = snd_pcm_lib_buffer_bytes(substream); |
| 474 | period_bytes = snd_pcm_lib_period_bytes(substream); |
| 475 | format_verb = lola_get_format_verb(substream); |
| 476 | |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 477 | str->bufsize = bufsize; |
| 478 | str->period_bytes = period_bytes; |
| 479 | str->format_verb = format_verb; |
| 480 | |
| 481 | err = lola_setup_periods(chip, pcm, substream, str); |
| 482 | if (err < 0) |
| 483 | return err; |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 484 | |
Takashi Iwai | 7e79f22 | 2011-05-03 16:59:27 +0200 | [diff] [blame] | 485 | err = lola_set_sample_rate(chip, runtime->rate); |
| 486 | if (err < 0) |
| 487 | return err; |
| 488 | chip->sample_rate = runtime->rate; /* sample rate gets locked */ |
| 489 | |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 490 | err = lola_set_stream_config(chip, str, runtime->channels); |
| 491 | if (err < 0) |
| 492 | return err; |
| 493 | |
Takashi Iwai | 0f8f56c | 2011-05-03 16:47:03 +0200 | [diff] [blame] | 494 | err = lola_setup_controller(chip, pcm, str); |
| 495 | if (err < 0) { |
| 496 | lola_stream_reset(chip, str); |
| 497 | return err; |
| 498 | } |
| 499 | |
| 500 | return 0; |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static int lola_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 504 | { |
| 505 | struct lola *chip = snd_pcm_substream_chip(substream); |
| 506 | struct lola_stream *str; |
| 507 | struct snd_pcm_substream *s; |
| 508 | unsigned int start; |
| 509 | unsigned int tstamp; |
Takashi Iwai | 7e79f22 | 2011-05-03 16:59:27 +0200 | [diff] [blame] | 510 | bool sync_streams; |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 511 | |
| 512 | switch (cmd) { |
| 513 | case SNDRV_PCM_TRIGGER_START: |
| 514 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 515 | case SNDRV_PCM_TRIGGER_RESUME: |
| 516 | start = 1; |
| 517 | break; |
| 518 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 519 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 520 | case SNDRV_PCM_TRIGGER_STOP: |
| 521 | start = 0; |
| 522 | break; |
| 523 | default: |
| 524 | return -EINVAL; |
| 525 | } |
| 526 | |
Takashi Iwai | 7e79f22 | 2011-05-03 16:59:27 +0200 | [diff] [blame] | 527 | /* |
| 528 | * sample correct synchronization is only needed starting several |
Takashi Iwai | 2db3002 | 2011-05-03 17:05:08 +0200 | [diff] [blame] | 529 | * streams. On stop or if only one stream do as quick as possible |
Takashi Iwai | 7e79f22 | 2011-05-03 16:59:27 +0200 | [diff] [blame] | 530 | */ |
| 531 | sync_streams = (start && snd_pcm_stream_linked(substream)); |
| 532 | tstamp = lola_get_tstamp(chip, !sync_streams); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 533 | spin_lock(&chip->reg_lock); |
| 534 | snd_pcm_group_for_each_entry(s, substream) { |
| 535 | if (s->pcm->card != substream->pcm->card) |
| 536 | continue; |
| 537 | str = lola_get_stream(s); |
| 538 | if (start) |
| 539 | lola_stream_start(chip, str, tstamp); |
| 540 | else |
| 541 | lola_stream_stop(chip, str, tstamp); |
| 542 | str->running = start; |
Takashi Iwai | 7e79f22 | 2011-05-03 16:59:27 +0200 | [diff] [blame] | 543 | str->paused = !start; |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 544 | snd_pcm_trigger_done(s, substream); |
| 545 | } |
| 546 | spin_unlock(&chip->reg_lock); |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | static snd_pcm_uframes_t lola_pcm_pointer(struct snd_pcm_substream *substream) |
| 551 | { |
| 552 | struct lola *chip = snd_pcm_substream_chip(substream); |
| 553 | struct lola_stream *str = lola_get_stream(substream); |
| 554 | unsigned int pos = lola_dsd_read(chip, str->dsd, LPIB); |
| 555 | |
| 556 | if (pos >= str->bufsize) |
| 557 | pos = 0; |
| 558 | return bytes_to_frames(substream->runtime, pos); |
| 559 | } |
| 560 | |
| 561 | void lola_pcm_update(struct lola *chip, struct lola_pcm *pcm, unsigned int bits) |
| 562 | { |
| 563 | int i; |
| 564 | |
| 565 | for (i = 0; bits && i < pcm->num_streams; i++) { |
| 566 | if (bits & (1 << i)) { |
| 567 | struct lola_stream *str = &pcm->streams[i]; |
| 568 | if (str->substream && str->running) |
| 569 | snd_pcm_period_elapsed(str->substream); |
| 570 | bits &= ~(1 << i); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
Julia Lawall | 6769e988 | 2016-09-02 00:13:10 +0200 | [diff] [blame] | 575 | static const struct snd_pcm_ops lola_pcm_ops = { |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 576 | .open = lola_pcm_open, |
| 577 | .close = lola_pcm_close, |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 578 | .hw_params = lola_pcm_hw_params, |
| 579 | .hw_free = lola_pcm_hw_free, |
| 580 | .prepare = lola_pcm_prepare, |
| 581 | .trigger = lola_pcm_trigger, |
| 582 | .pointer = lola_pcm_pointer, |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 583 | }; |
| 584 | |
Bill Pemberton | e23e7a1 | 2012-12-06 12:35:10 -0500 | [diff] [blame] | 585 | int lola_create_pcm(struct lola *chip) |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 586 | { |
| 587 | struct snd_pcm *pcm; |
| 588 | int i, err; |
| 589 | |
Takashi Iwai | 333ff39 | 2011-05-03 16:41:02 +0200 | [diff] [blame] | 590 | for (i = 0; i < 2; i++) { |
Takashi Iwai | 098fe3d | 2021-07-15 09:59:02 +0200 | [diff] [blame] | 591 | chip->pcm[i].bdl = |
| 592 | snd_devm_alloc_pages(&chip->pci->dev, SNDRV_DMA_TYPE_DEV, |
| 593 | PAGE_SIZE); |
| 594 | if (!chip->pcm[i].bdl) |
| 595 | return -ENOMEM; |
Takashi Iwai | 333ff39 | 2011-05-03 16:41:02 +0200 | [diff] [blame] | 596 | } |
| 597 | |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 598 | err = snd_pcm_new(chip->card, "Digigram Lola", 0, |
| 599 | chip->pcm[SNDRV_PCM_STREAM_PLAYBACK].num_streams, |
| 600 | chip->pcm[SNDRV_PCM_STREAM_CAPTURE].num_streams, |
| 601 | &pcm); |
| 602 | if (err < 0) |
| 603 | return err; |
Joe Perches | 75b1a8f | 2021-01-04 09:17:34 -0800 | [diff] [blame] | 604 | strscpy(pcm->name, "Digigram Lola", sizeof(pcm->name)); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 605 | pcm->private_data = chip; |
| 606 | for (i = 0; i < 2; i++) { |
| 607 | if (chip->pcm[i].num_streams) |
| 608 | snd_pcm_set_ops(pcm, i, &lola_pcm_ops); |
| 609 | } |
| 610 | /* buffer pre-allocation */ |
Takashi Iwai | 224a40c | 2019-12-09 10:49:16 +0100 | [diff] [blame] | 611 | snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG, |
| 612 | &chip->pci->dev, |
| 613 | 1024 * 64, 32 * 1024 * 1024); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 614 | return 0; |
| 615 | } |
| 616 | |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 617 | /* |
| 618 | */ |
| 619 | |
| 620 | static int lola_init_stream(struct lola *chip, struct lola_stream *str, |
| 621 | int idx, int nid, int dir) |
| 622 | { |
| 623 | unsigned int val; |
| 624 | int err; |
| 625 | |
| 626 | str->nid = nid; |
| 627 | str->index = idx; |
| 628 | str->dsd = idx; |
| 629 | if (dir == PLAY) |
| 630 | str->dsd += MAX_STREAM_IN_COUNT; |
| 631 | err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val); |
| 632 | if (err < 0) { |
Takashi Iwai | f58e2fc | 2014-02-25 17:23:57 +0100 | [diff] [blame] | 633 | dev_err(chip->card->dev, "Can't read wcaps for 0x%x\n", nid); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 634 | return err; |
| 635 | } |
| 636 | if (dir == PLAY) { |
| 637 | /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) */ |
| 638 | if ((val & 0x00f00dff) != 0x00000010) { |
Takashi Iwai | f58e2fc | 2014-02-25 17:23:57 +0100 | [diff] [blame] | 639 | dev_err(chip->card->dev, |
| 640 | "Invalid wcaps 0x%x for 0x%x\n", |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 641 | val, nid); |
| 642 | return -EINVAL; |
| 643 | } |
| 644 | } else { |
| 645 | /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) |
| 646 | * (bug : ignore bit8: Conn list = 0/1) |
| 647 | */ |
| 648 | if ((val & 0x00f00cff) != 0x00100010) { |
Takashi Iwai | f58e2fc | 2014-02-25 17:23:57 +0100 | [diff] [blame] | 649 | dev_err(chip->card->dev, |
| 650 | "Invalid wcaps 0x%x for 0x%x\n", |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 651 | val, nid); |
| 652 | return -EINVAL; |
| 653 | } |
| 654 | /* test bit9:DIGITAL and bit12:SRC_PRESENT*/ |
| 655 | if ((val & 0x00001200) == 0x00001200) |
| 656 | chip->input_src_caps_mask |= (1 << idx); |
| 657 | } |
| 658 | |
| 659 | err = lola_read_param(chip, nid, LOLA_PAR_STREAM_FORMATS, &val); |
| 660 | if (err < 0) { |
Takashi Iwai | f58e2fc | 2014-02-25 17:23:57 +0100 | [diff] [blame] | 661 | dev_err(chip->card->dev, "Can't read FORMATS 0x%x\n", nid); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 662 | return err; |
| 663 | } |
| 664 | val &= 3; |
| 665 | if (val == 3) |
| 666 | str->can_float = true; |
| 667 | if (!(val & 1)) { |
Takashi Iwai | f58e2fc | 2014-02-25 17:23:57 +0100 | [diff] [blame] | 668 | dev_err(chip->card->dev, |
| 669 | "Invalid formats 0x%x for 0x%x", val, nid); |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 670 | return -EINVAL; |
| 671 | } |
| 672 | return 0; |
| 673 | } |
| 674 | |
Bill Pemberton | e23e7a1 | 2012-12-06 12:35:10 -0500 | [diff] [blame] | 675 | int lola_init_pcm(struct lola *chip, int dir, int *nidp) |
Takashi Iwai | d43f3010 | 2011-05-03 16:14:46 +0200 | [diff] [blame] | 676 | { |
| 677 | struct lola_pcm *pcm = &chip->pcm[dir]; |
| 678 | int i, nid, err; |
| 679 | |
| 680 | nid = *nidp; |
| 681 | for (i = 0; i < pcm->num_streams; i++, nid++) { |
| 682 | err = lola_init_stream(chip, &pcm->streams[i], i, nid, dir); |
| 683 | if (err < 0) |
| 684 | return err; |
| 685 | } |
| 686 | *nidp = nid; |
| 687 | return 0; |
| 688 | } |