Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
d43f3010 TI |
2 | /* |
3 | * Support for Digigram Lola PCI-e boards | |
4 | * | |
5 | * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de> | |
d43f3010 TI |
6 | */ |
7 | ||
8 | #include <linux/kernel.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/dma-mapping.h> | |
11 | #include <linux/pci.h> | |
f044785d | 12 | #include <linux/delay.h> |
d43f3010 TI |
13 | #include <sound/core.h> |
14 | #include <sound/pcm.h> | |
15 | #include "lola.h" | |
16 | ||
333ff397 | 17 | #define LOLA_MAX_BDL_ENTRIES 8 |
d43f3010 | 18 | #define LOLA_MAX_BUF_SIZE (1024*1024*1024) |
333ff397 TI |
19 | #define LOLA_BDL_ENTRY_SIZE (16 * 16) |
20 | ||
d43f3010 TI |
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 | ||
d43f3010 TI |
83 | static void wait_for_srst_clear(struct lola *chip, struct lola_stream *str) |
84 | { | |
333ff397 | 85 | unsigned long end_time = jiffies + msecs_to_jiffies(200); |
d43f3010 TI |
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 | } | |
f58e2fce | 93 | dev_warn(chip->card->dev, "SRST not clear (stream %d)\n", str->dsd); |
d43f3010 TI |
94 | } |
95 | ||
0f8f56c9 TI |
96 | static int lola_stream_wait_for_fifo(struct lola *chip, |
97 | struct lola_stream *str, | |
98 | bool ready) | |
d43f3010 | 99 | { |
0f8f56c9 TI |
100 | unsigned int val = ready ? LOLA_DSD_STS_FIFORDY : 0; |
101 | unsigned long end_time = jiffies + msecs_to_jiffies(200); | |
d43f3010 | 102 | while (time_before(jiffies, end_time)) { |
0f8f56c9 TI |
103 | unsigned int reg = lola_dsd_read(chip, str->dsd, STS); |
104 | if ((reg & LOLA_DSD_STS_FIFORDY) == val) | |
d43f3010 TI |
105 | return 0; |
106 | msleep(1); | |
107 | } | |
f58e2fce | 108 | dev_warn(chip->card->dev, "FIFO not ready (stream %d)\n", str->dsd); |
d43f3010 TI |
109 | return -EIO; |
110 | } | |
111 | ||
c7aad3c3 TI |
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) | |
0f8f56c9 | 118 | { |
c7aad3c3 TI |
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 | } | |
f58e2fce | 146 | dev_warn(chip->card->dev, "FIFO not ready (pending %d)\n", pending - 1); |
c7aad3c3 TI |
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; | |
0f8f56c9 | 155 | |
c7aad3c3 TI |
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) | |
7e79f226 TI |
163 | lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRUN | |
164 | LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE); | |
c7aad3c3 TI |
165 | } |
166 | lola_sync_wait_for_fifo(chip, substream, true); | |
167 | } | |
168 | ||
169 | static void lola_stream_reset(struct lola *chip, struct lola_stream *str) | |
170 | { | |
171 | if (str->prepared) { | |
172 | if (str->paused) | |
173 | lola_sync_pause(chip, str->substream); | |
174 | str->prepared = 0; | |
0f8f56c9 TI |
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 | ||
5cf84795 | 187 | static const struct snd_pcm_hardware lola_pcm_hw = { |
d43f3010 TI |
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), | |
0f8f56c9 TI |
197 | .rates = SNDRV_PCM_RATE_8000_192000, |
198 | .rate_min = 8000, | |
199 | .rate_max = 192000, | |
d43f3010 TI |
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, | |
0f8f56c9 | 206 | .periods_max = LOLA_MAX_BDL_ENTRIES, |
d43f3010 TI |
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; | |
d43f3010 TI |
216 | |
217 | mutex_lock(&chip->open_mutex); | |
218 | if (str->opened) { | |
219 | mutex_unlock(&chip->open_mutex); | |
220 | return -EBUSY; | |
221 | } | |
d43f3010 TI |
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; | |
7e79f226 TI |
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++; | |
d43f3010 | 236 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
7e79f226 | 237 | /* period size = multiple of chip->granularity (8, 16 or 32 frames)*/ |
0f8f56c9 | 238 | snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
8bd172dc | 239 | chip->granularity); |
0f8f56c9 | 240 | snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
8bd172dc | 241 | chip->granularity); |
d43f3010 TI |
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 | } | |
7e79f226 TI |
269 | if (--chip->ref_count_rate == 0) { |
270 | /* release sample rate */ | |
271 | chip->sample_rate = 0; | |
272 | } | |
d43f3010 TI |
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; | |
285 | return snd_pcm_lib_malloc_pages(substream, | |
286 | params_buffer_bytes(hw_params)); | |
287 | } | |
288 | ||
289 | static int lola_pcm_hw_free(struct snd_pcm_substream *substream) | |
290 | { | |
291 | struct lola *chip = snd_pcm_substream_chip(substream); | |
292 | struct lola_pcm *pcm = lola_get_pcm(substream); | |
293 | struct lola_stream *str = lola_get_stream(substream); | |
294 | ||
295 | mutex_lock(&chip->open_mutex); | |
296 | lola_stream_reset(chip, str); | |
297 | lola_cleanup_slave_streams(pcm, str); | |
298 | mutex_unlock(&chip->open_mutex); | |
299 | return snd_pcm_lib_free_pages(substream); | |
300 | } | |
301 | ||
302 | /* | |
303 | * set up a BDL entry | |
304 | */ | |
305 | static int setup_bdle(struct snd_pcm_substream *substream, | |
0d9a26fc | 306 | struct lola_stream *str, __le32 **bdlp, |
d43f3010 TI |
307 | int ofs, int size) |
308 | { | |
0d9a26fc | 309 | __le32 *bdl = *bdlp; |
d43f3010 TI |
310 | |
311 | while (size > 0) { | |
312 | dma_addr_t addr; | |
313 | int chunk; | |
314 | ||
315 | if (str->frags >= LOLA_MAX_BDL_ENTRIES) | |
316 | return -EINVAL; | |
317 | ||
972505cc | 318 | addr = snd_pcm_sgbuf_get_addr(substream, ofs); |
d43f3010 TI |
319 | /* program the address field of the BDL entry */ |
320 | bdl[0] = cpu_to_le32((u32)addr); | |
321 | bdl[1] = cpu_to_le32(upper_32_bits(addr)); | |
322 | /* program the size field of the BDL entry */ | |
972505cc | 323 | chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size); |
d43f3010 TI |
324 | bdl[2] = cpu_to_le32(chunk); |
325 | /* program the IOC to enable interrupt | |
326 | * only when the whole fragment is processed | |
327 | */ | |
328 | size -= chunk; | |
329 | bdl[3] = size ? 0 : cpu_to_le32(0x01); | |
330 | bdl += 4; | |
331 | str->frags++; | |
332 | ofs += chunk; | |
333 | } | |
334 | *bdlp = bdl; | |
335 | return ofs; | |
336 | } | |
337 | ||
338 | /* | |
339 | * set up BDL entries | |
340 | */ | |
333ff397 | 341 | static int lola_setup_periods(struct lola *chip, struct lola_pcm *pcm, |
d43f3010 TI |
342 | struct snd_pcm_substream *substream, |
343 | struct lola_stream *str) | |
344 | { | |
0d9a26fc | 345 | __le32 *bdl; |
d43f3010 TI |
346 | int i, ofs, periods, period_bytes; |
347 | ||
348 | period_bytes = str->period_bytes; | |
349 | periods = str->bufsize / period_bytes; | |
350 | ||
351 | /* program the initial BDL entries */ | |
0d9a26fc | 352 | bdl = (__le32 *)(pcm->bdl.area + LOLA_BDL_ENTRY_SIZE * str->index); |
d43f3010 TI |
353 | ofs = 0; |
354 | str->frags = 0; | |
355 | for (i = 0; i < periods; i++) { | |
356 | ofs = setup_bdle(substream, str, &bdl, ofs, period_bytes); | |
357 | if (ofs < 0) | |
358 | goto error; | |
359 | } | |
360 | return 0; | |
361 | ||
362 | error: | |
f58e2fce | 363 | dev_err(chip->card->dev, "Too many BDL entries: buffer=%d, period=%d\n", |
d43f3010 TI |
364 | str->bufsize, period_bytes); |
365 | return -EINVAL; | |
366 | } | |
367 | ||
368 | static unsigned int lola_get_format_verb(struct snd_pcm_substream *substream) | |
369 | { | |
370 | unsigned int verb; | |
371 | ||
372 | switch (substream->runtime->format) { | |
373 | case SNDRV_PCM_FORMAT_S16_LE: | |
374 | verb = 0x00000000; | |
375 | break; | |
376 | case SNDRV_PCM_FORMAT_S24_LE: | |
377 | verb = 0x00000200; | |
378 | break; | |
379 | case SNDRV_PCM_FORMAT_S32_LE: | |
380 | verb = 0x00000300; | |
381 | break; | |
382 | case SNDRV_PCM_FORMAT_FLOAT_LE: | |
383 | verb = 0x00001300; | |
384 | break; | |
385 | default: | |
386 | return 0; | |
387 | } | |
388 | verb |= substream->runtime->channels; | |
389 | return verb; | |
390 | } | |
391 | ||
392 | static int lola_set_stream_config(struct lola *chip, | |
393 | struct lola_stream *str, | |
394 | int channels) | |
395 | { | |
396 | int i, err; | |
397 | unsigned int verb, val; | |
398 | ||
399 | /* set format info for all channels | |
400 | * (with only one command for the first channel) | |
401 | */ | |
402 | err = lola_codec_read(chip, str->nid, LOLA_VERB_SET_STREAM_FORMAT, | |
403 | str->format_verb, 0, &val, NULL); | |
404 | if (err < 0) { | |
f58e2fce | 405 | dev_err(chip->card->dev, "Cannot set stream format 0x%x\n", |
d43f3010 TI |
406 | str->format_verb); |
407 | return err; | |
408 | } | |
409 | ||
410 | /* update stream - channel config */ | |
411 | for (i = 0; i < channels; i++) { | |
412 | verb = (str->index << 6) | i; | |
413 | err = lola_codec_read(chip, str[i].nid, | |
414 | LOLA_VERB_SET_CHANNEL_STREAMID, 0, verb, | |
415 | &val, NULL); | |
416 | if (err < 0) { | |
f58e2fce TI |
417 | dev_err(chip->card->dev, |
418 | "Cannot set stream channel %d\n", i); | |
d43f3010 TI |
419 | return err; |
420 | } | |
421 | } | |
422 | return 0; | |
423 | } | |
424 | ||
425 | /* | |
426 | * set up the SD for streaming | |
427 | */ | |
333ff397 TI |
428 | static int lola_setup_controller(struct lola *chip, struct lola_pcm *pcm, |
429 | struct lola_stream *str) | |
d43f3010 | 430 | { |
333ff397 | 431 | dma_addr_t bdl; |
0f8f56c9 TI |
432 | |
433 | if (str->prepared) | |
434 | return -EINVAL; | |
435 | ||
d43f3010 | 436 | /* set up BDL */ |
333ff397 TI |
437 | bdl = pcm->bdl.addr + LOLA_BDL_ENTRY_SIZE * str->index; |
438 | lola_dsd_write(chip, str->dsd, BDPL, (u32)bdl); | |
439 | lola_dsd_write(chip, str->dsd, BDPU, upper_32_bits(bdl)); | |
d43f3010 TI |
440 | /* program the stream LVI (last valid index) of the BDL */ |
441 | lola_dsd_write(chip, str->dsd, LVI, str->frags - 1); | |
0f8f56c9 | 442 | lola_stream_clear_pending_irq(chip, str); |
d43f3010 | 443 | |
0f8f56c9 TI |
444 | lola_dsd_write(chip, str->dsd, CTL, |
445 | LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE | LOLA_DSD_CTL_SRUN); | |
446 | ||
447 | str->prepared = 1; | |
448 | ||
449 | return lola_stream_wait_for_fifo(chip, str, true); | |
d43f3010 TI |
450 | } |
451 | ||
452 | static int lola_pcm_prepare(struct snd_pcm_substream *substream) | |
453 | { | |
454 | struct lola *chip = snd_pcm_substream_chip(substream); | |
455 | struct lola_pcm *pcm = lola_get_pcm(substream); | |
456 | struct lola_stream *str = lola_get_stream(substream); | |
457 | struct snd_pcm_runtime *runtime = substream->runtime; | |
458 | unsigned int bufsize, period_bytes, format_verb; | |
459 | int i, err; | |
460 | ||
461 | mutex_lock(&chip->open_mutex); | |
462 | lola_stream_reset(chip, str); | |
463 | lola_cleanup_slave_streams(pcm, str); | |
2db30020 | 464 | if (str->index + runtime->channels > pcm->num_streams) { |
d43f3010 TI |
465 | mutex_unlock(&chip->open_mutex); |
466 | return -EINVAL; | |
467 | } | |
468 | for (i = 1; i < runtime->channels; i++) { | |
469 | str[i].master = str; | |
470 | str[i].opened = 1; | |
471 | } | |
472 | mutex_unlock(&chip->open_mutex); | |
473 | ||
474 | bufsize = snd_pcm_lib_buffer_bytes(substream); | |
475 | period_bytes = snd_pcm_lib_period_bytes(substream); | |
476 | format_verb = lola_get_format_verb(substream); | |
477 | ||
0f8f56c9 TI |
478 | str->bufsize = bufsize; |
479 | str->period_bytes = period_bytes; | |
480 | str->format_verb = format_verb; | |
481 | ||
482 | err = lola_setup_periods(chip, pcm, substream, str); | |
483 | if (err < 0) | |
484 | return err; | |
d43f3010 | 485 | |
7e79f226 TI |
486 | err = lola_set_sample_rate(chip, runtime->rate); |
487 | if (err < 0) | |
488 | return err; | |
489 | chip->sample_rate = runtime->rate; /* sample rate gets locked */ | |
490 | ||
d43f3010 TI |
491 | err = lola_set_stream_config(chip, str, runtime->channels); |
492 | if (err < 0) | |
493 | return err; | |
494 | ||
0f8f56c9 TI |
495 | err = lola_setup_controller(chip, pcm, str); |
496 | if (err < 0) { | |
497 | lola_stream_reset(chip, str); | |
498 | return err; | |
499 | } | |
500 | ||
501 | return 0; | |
d43f3010 TI |
502 | } |
503 | ||
504 | static int lola_pcm_trigger(struct snd_pcm_substream *substream, int cmd) | |
505 | { | |
506 | struct lola *chip = snd_pcm_substream_chip(substream); | |
507 | struct lola_stream *str; | |
508 | struct snd_pcm_substream *s; | |
509 | unsigned int start; | |
510 | unsigned int tstamp; | |
7e79f226 | 511 | bool sync_streams; |
d43f3010 TI |
512 | |
513 | switch (cmd) { | |
514 | case SNDRV_PCM_TRIGGER_START: | |
515 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
516 | case SNDRV_PCM_TRIGGER_RESUME: | |
517 | start = 1; | |
518 | break; | |
519 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
520 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
521 | case SNDRV_PCM_TRIGGER_STOP: | |
522 | start = 0; | |
523 | break; | |
524 | default: | |
525 | return -EINVAL; | |
526 | } | |
527 | ||
7e79f226 TI |
528 | /* |
529 | * sample correct synchronization is only needed starting several | |
2db30020 | 530 | * streams. On stop or if only one stream do as quick as possible |
7e79f226 TI |
531 | */ |
532 | sync_streams = (start && snd_pcm_stream_linked(substream)); | |
533 | tstamp = lola_get_tstamp(chip, !sync_streams); | |
d43f3010 TI |
534 | spin_lock(&chip->reg_lock); |
535 | snd_pcm_group_for_each_entry(s, substream) { | |
536 | if (s->pcm->card != substream->pcm->card) | |
537 | continue; | |
538 | str = lola_get_stream(s); | |
539 | if (start) | |
540 | lola_stream_start(chip, str, tstamp); | |
541 | else | |
542 | lola_stream_stop(chip, str, tstamp); | |
543 | str->running = start; | |
7e79f226 | 544 | str->paused = !start; |
d43f3010 TI |
545 | snd_pcm_trigger_done(s, substream); |
546 | } | |
547 | spin_unlock(&chip->reg_lock); | |
548 | return 0; | |
549 | } | |
550 | ||
551 | static snd_pcm_uframes_t lola_pcm_pointer(struct snd_pcm_substream *substream) | |
552 | { | |
553 | struct lola *chip = snd_pcm_substream_chip(substream); | |
554 | struct lola_stream *str = lola_get_stream(substream); | |
555 | unsigned int pos = lola_dsd_read(chip, str->dsd, LPIB); | |
556 | ||
557 | if (pos >= str->bufsize) | |
558 | pos = 0; | |
559 | return bytes_to_frames(substream->runtime, pos); | |
560 | } | |
561 | ||
562 | void lola_pcm_update(struct lola *chip, struct lola_pcm *pcm, unsigned int bits) | |
563 | { | |
564 | int i; | |
565 | ||
566 | for (i = 0; bits && i < pcm->num_streams; i++) { | |
567 | if (bits & (1 << i)) { | |
568 | struct lola_stream *str = &pcm->streams[i]; | |
569 | if (str->substream && str->running) | |
570 | snd_pcm_period_elapsed(str->substream); | |
571 | bits &= ~(1 << i); | |
572 | } | |
573 | } | |
574 | } | |
575 | ||
6769e988 | 576 | static const struct snd_pcm_ops lola_pcm_ops = { |
d43f3010 TI |
577 | .open = lola_pcm_open, |
578 | .close = lola_pcm_close, | |
579 | .ioctl = snd_pcm_lib_ioctl, | |
580 | .hw_params = lola_pcm_hw_params, | |
581 | .hw_free = lola_pcm_hw_free, | |
582 | .prepare = lola_pcm_prepare, | |
583 | .trigger = lola_pcm_trigger, | |
584 | .pointer = lola_pcm_pointer, | |
d43f3010 TI |
585 | }; |
586 | ||
e23e7a14 | 587 | int lola_create_pcm(struct lola *chip) |
d43f3010 TI |
588 | { |
589 | struct snd_pcm *pcm; | |
590 | int i, err; | |
591 | ||
333ff397 TI |
592 | for (i = 0; i < 2; i++) { |
593 | err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, | |
6974f8ad | 594 | &chip->pci->dev, |
333ff397 TI |
595 | PAGE_SIZE, &chip->pcm[i].bdl); |
596 | if (err < 0) | |
597 | return err; | |
598 | } | |
599 | ||
d43f3010 TI |
600 | err = snd_pcm_new(chip->card, "Digigram Lola", 0, |
601 | chip->pcm[SNDRV_PCM_STREAM_PLAYBACK].num_streams, | |
602 | chip->pcm[SNDRV_PCM_STREAM_CAPTURE].num_streams, | |
603 | &pcm); | |
604 | if (err < 0) | |
605 | return err; | |
606 | strlcpy(pcm->name, "Digigram Lola", sizeof(pcm->name)); | |
607 | pcm->private_data = chip; | |
608 | for (i = 0; i < 2; i++) { | |
609 | if (chip->pcm[i].num_streams) | |
610 | snd_pcm_set_ops(pcm, i, &lola_pcm_ops); | |
611 | } | |
612 | /* buffer pre-allocation */ | |
972505cc | 613 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG, |
6974f8ad | 614 | &chip->pci->dev, |
d43f3010 TI |
615 | 1024 * 64, 32 * 1024 * 1024); |
616 | return 0; | |
617 | } | |
618 | ||
619 | void lola_free_pcm(struct lola *chip) | |
620 | { | |
333ff397 TI |
621 | snd_dma_free_pages(&chip->pcm[0].bdl); |
622 | snd_dma_free_pages(&chip->pcm[1].bdl); | |
d43f3010 TI |
623 | } |
624 | ||
625 | /* | |
626 | */ | |
627 | ||
628 | static int lola_init_stream(struct lola *chip, struct lola_stream *str, | |
629 | int idx, int nid, int dir) | |
630 | { | |
631 | unsigned int val; | |
632 | int err; | |
633 | ||
634 | str->nid = nid; | |
635 | str->index = idx; | |
636 | str->dsd = idx; | |
637 | if (dir == PLAY) | |
638 | str->dsd += MAX_STREAM_IN_COUNT; | |
639 | err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val); | |
640 | if (err < 0) { | |
f58e2fce | 641 | dev_err(chip->card->dev, "Can't read wcaps for 0x%x\n", nid); |
d43f3010 TI |
642 | return err; |
643 | } | |
644 | if (dir == PLAY) { | |
645 | /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) */ | |
646 | if ((val & 0x00f00dff) != 0x00000010) { | |
f58e2fce TI |
647 | dev_err(chip->card->dev, |
648 | "Invalid wcaps 0x%x for 0x%x\n", | |
d43f3010 TI |
649 | val, nid); |
650 | return -EINVAL; | |
651 | } | |
652 | } else { | |
653 | /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) | |
654 | * (bug : ignore bit8: Conn list = 0/1) | |
655 | */ | |
656 | if ((val & 0x00f00cff) != 0x00100010) { | |
f58e2fce TI |
657 | dev_err(chip->card->dev, |
658 | "Invalid wcaps 0x%x for 0x%x\n", | |
d43f3010 TI |
659 | val, nid); |
660 | return -EINVAL; | |
661 | } | |
662 | /* test bit9:DIGITAL and bit12:SRC_PRESENT*/ | |
663 | if ((val & 0x00001200) == 0x00001200) | |
664 | chip->input_src_caps_mask |= (1 << idx); | |
665 | } | |
666 | ||
667 | err = lola_read_param(chip, nid, LOLA_PAR_STREAM_FORMATS, &val); | |
668 | if (err < 0) { | |
f58e2fce | 669 | dev_err(chip->card->dev, "Can't read FORMATS 0x%x\n", nid); |
d43f3010 TI |
670 | return err; |
671 | } | |
672 | val &= 3; | |
673 | if (val == 3) | |
674 | str->can_float = true; | |
675 | if (!(val & 1)) { | |
f58e2fce TI |
676 | dev_err(chip->card->dev, |
677 | "Invalid formats 0x%x for 0x%x", val, nid); | |
d43f3010 TI |
678 | return -EINVAL; |
679 | } | |
680 | return 0; | |
681 | } | |
682 | ||
e23e7a14 | 683 | int lola_init_pcm(struct lola *chip, int dir, int *nidp) |
d43f3010 TI |
684 | { |
685 | struct lola_pcm *pcm = &chip->pcm[dir]; | |
686 | int i, nid, err; | |
687 | ||
688 | nid = *nidp; | |
689 | for (i = 0; i < pcm->num_streams; i++, nid++) { | |
690 | err = lola_init_stream(chip, &pcm->streams[i], i, nid, dir); | |
691 | if (err < 0) | |
692 | return err; | |
693 | } | |
694 | *nidp = nid; | |
695 | return 0; | |
696 | } |