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