1 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Vortex PCM ALSA driver.
8 * Supports ADB and WT DMA. Unfortunately, WT channels do not run yet.
9 * It remains stuck,and DMA transfers do not happen.
11 #include <sound/asoundef.h>
12 #include <linux/time.h>
13 #include <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
18 #define VORTEX_PCM_TYPE(x) (x->name[40])
20 /* hardware definition */
21 static const struct snd_pcm_hardware snd_vortex_playback_hw_adb = {
23 (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
24 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
25 SNDRV_PCM_INFO_MMAP_VALID),
27 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
28 SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
29 .rates = SNDRV_PCM_RATE_CONTINUOUS,
34 .buffer_bytes_max = 0x10000,
35 .period_bytes_min = 0x20,
36 .period_bytes_max = 0x1000,
42 static const struct snd_pcm_hardware snd_vortex_playback_hw_a3d = {
44 (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
45 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
46 SNDRV_PCM_INFO_MMAP_VALID),
48 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
49 SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
50 .rates = SNDRV_PCM_RATE_CONTINUOUS,
55 .buffer_bytes_max = 0x10000,
56 .period_bytes_min = 0x100,
57 .period_bytes_max = 0x1000,
62 static const struct snd_pcm_hardware snd_vortex_playback_hw_spdif = {
64 (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
65 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
66 SNDRV_PCM_INFO_MMAP_VALID),
68 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
69 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE | SNDRV_PCM_FMTBIT_MU_LAW |
70 SNDRV_PCM_FMTBIT_A_LAW,
72 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
77 .buffer_bytes_max = 0x10000,
78 .period_bytes_min = 0x100,
79 .period_bytes_max = 0x1000,
85 static const struct snd_pcm_hardware snd_vortex_playback_hw_wt = {
86 .info = (SNDRV_PCM_INFO_MMAP |
87 SNDRV_PCM_INFO_INTERLEAVED |
88 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
89 .formats = SNDRV_PCM_FMTBIT_S16_LE,
90 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS, // SNDRV_PCM_RATE_48000,
95 .buffer_bytes_max = 0x10000,
96 .period_bytes_min = 0x0400,
97 .period_bytes_max = 0x1000,
103 static const unsigned int au8830_channels[3] = {
107 static const struct snd_pcm_hw_constraint_list hw_constraints_au8830_channels = {
108 .count = ARRAY_SIZE(au8830_channels),
109 .list = au8830_channels,
114 static void vortex_notify_pcm_vol_change(struct snd_card *card,
115 struct snd_kcontrol *kctl, int activate)
118 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
120 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
121 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
122 SNDRV_CTL_EVENT_MASK_INFO, &(kctl->id));
126 static int snd_vortex_pcm_open(struct snd_pcm_substream *substream)
128 vortex_t *vortex = snd_pcm_substream_chip(substream);
129 struct snd_pcm_runtime *runtime = substream->runtime;
132 /* Force equal size periods */
134 snd_pcm_hw_constraint_integer(runtime,
135 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
137 /* Avoid PAGE_SIZE boundary to fall inside of a period. */
139 snd_pcm_hw_constraint_pow2(runtime, 0,
140 SNDRV_PCM_HW_PARAM_PERIOD_BYTES)) < 0)
143 snd_pcm_hw_constraint_step(runtime, 0,
144 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 64);
146 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
148 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_A3D) {
149 runtime->hw = snd_vortex_playback_hw_a3d;
152 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_SPDIF) {
153 runtime->hw = snd_vortex_playback_hw_spdif;
154 switch (vortex->spdif_sr) {
156 runtime->hw.rates = SNDRV_PCM_RATE_32000;
159 runtime->hw.rates = SNDRV_PCM_RATE_44100;
162 runtime->hw.rates = SNDRV_PCM_RATE_48000;
166 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB
167 || VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_I2S)
168 runtime->hw = snd_vortex_playback_hw_adb;
170 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
171 VORTEX_IS_QUAD(vortex) &&
172 VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB) {
173 runtime->hw.channels_max = 4;
174 snd_pcm_hw_constraint_list(runtime, 0,
175 SNDRV_PCM_HW_PARAM_CHANNELS,
176 &hw_constraints_au8830_channels);
179 substream->runtime->private_data = NULL;
183 runtime->hw = snd_vortex_playback_hw_wt;
184 substream->runtime->private_data = NULL;
191 static int snd_vortex_pcm_close(struct snd_pcm_substream *substream)
193 //vortex_t *chip = snd_pcm_substream_chip(substream);
194 stream_t *stream = (stream_t *) substream->runtime->private_data;
196 // the hardware-specific codes will be here
197 if (stream != NULL) {
198 stream->substream = NULL;
201 substream->runtime->private_data = NULL;
205 /* hw_params callback */
207 snd_vortex_pcm_hw_params(struct snd_pcm_substream *substream,
208 struct snd_pcm_hw_params *hw_params)
210 vortex_t *chip = snd_pcm_substream_chip(substream);
211 stream_t *stream = (stream_t *) (substream->runtime->private_data);
214 // Alloc buffer memory.
216 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
218 dev_err(chip->card->dev, "Vortex: pcm page alloc failed!\n");
222 pr_info( "Vortex: periods %d, period_bytes %d, channels = %d\n", params_periods(hw_params),
223 params_period_bytes(hw_params), params_channels(hw_params));
225 spin_lock_irq(&chip->lock);
226 // Make audio routes and config buffer DMA.
227 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
228 int dma, type = VORTEX_PCM_TYPE(substream->pcm);
229 /* Dealloc any routes. */
231 vortex_adb_allocroute(chip, stream->dma,
232 stream->nr_ch, stream->dir,
237 vortex_adb_allocroute(chip, -1,
238 params_channels(hw_params),
239 substream->stream, type,
242 spin_unlock_irq(&chip->lock);
245 stream = substream->runtime->private_data = &chip->dma_adb[dma];
246 stream->substream = substream;
248 vortex_adbdma_setbuffers(chip, dma,
249 params_period_bytes(hw_params),
250 params_periods(hw_params));
251 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB) {
252 chip->pcm_vol[substream->number].active = 1;
253 vortex_notify_pcm_vol_change(chip->card,
254 chip->pcm_vol[substream->number].kctl, 1);
259 /* if (stream != NULL)
260 vortex_wt_allocroute(chip, substream->number, 0); */
261 vortex_wt_allocroute(chip, substream->number,
262 params_channels(hw_params));
263 stream = substream->runtime->private_data =
264 &chip->dma_wt[substream->number];
265 stream->dma = substream->number;
266 stream->substream = substream;
267 vortex_wtdma_setbuffers(chip, substream->number,
268 params_period_bytes(hw_params),
269 params_periods(hw_params));
272 spin_unlock_irq(&chip->lock);
276 /* hw_free callback */
277 static int snd_vortex_pcm_hw_free(struct snd_pcm_substream *substream)
279 vortex_t *chip = snd_pcm_substream_chip(substream);
280 stream_t *stream = (stream_t *) (substream->runtime->private_data);
282 spin_lock_irq(&chip->lock);
283 // Delete audio routes.
284 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
285 if (stream != NULL) {
286 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB) {
287 chip->pcm_vol[substream->number].active = 0;
288 vortex_notify_pcm_vol_change(chip->card,
289 chip->pcm_vol[substream->number].kctl,
292 vortex_adb_allocroute(chip, stream->dma,
293 stream->nr_ch, stream->dir,
301 vortex_wt_allocroute(chip, stream->dma, 0);
304 substream->runtime->private_data = NULL;
305 spin_unlock_irq(&chip->lock);
307 return snd_pcm_lib_free_pages(substream);
310 /* prepare callback */
311 static int snd_vortex_pcm_prepare(struct snd_pcm_substream *substream)
313 vortex_t *chip = snd_pcm_substream_chip(substream);
314 struct snd_pcm_runtime *runtime = substream->runtime;
315 stream_t *stream = (stream_t *) substream->runtime->private_data;
316 int dma = stream->dma, fmt, dir;
318 // set up the hardware with the current configuration.
319 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
323 fmt = vortex_alsafmt_aspfmt(runtime->format, chip);
324 spin_lock_irq(&chip->lock);
325 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
326 vortex_adbdma_setmode(chip, dma, 1, dir, fmt,
327 runtime->channels == 1 ? 0 : 1, 0);
328 vortex_adbdma_setstartbuffer(chip, dma, 0);
329 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_SPDIF)
330 vortex_adb_setsrc(chip, dma, runtime->rate, dir);
334 vortex_wtdma_setmode(chip, dma, 1, fmt, 0, 0);
335 // FIXME: Set rate (i guess using vortex_wt_writereg() somehow).
336 vortex_wtdma_setstartbuffer(chip, dma, 0);
339 spin_unlock_irq(&chip->lock);
343 /* trigger callback */
344 static int snd_vortex_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
346 vortex_t *chip = snd_pcm_substream_chip(substream);
347 stream_t *stream = (stream_t *) substream->runtime->private_data;
348 int dma = stream->dma;
350 spin_lock(&chip->lock);
352 case SNDRV_PCM_TRIGGER_START:
353 // do something to start the PCM engine
354 //printk(KERN_INFO "vortex: start %d\n", dma);
355 stream->fifo_enabled = 1;
356 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
357 vortex_adbdma_resetup(chip, dma);
358 vortex_adbdma_startfifo(chip, dma);
362 dev_info(chip->card->dev, "wt start %d\n", dma);
363 vortex_wtdma_startfifo(chip, dma);
367 case SNDRV_PCM_TRIGGER_STOP:
368 // do something to stop the PCM engine
369 //printk(KERN_INFO "vortex: stop %d\n", dma);
370 stream->fifo_enabled = 0;
371 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
372 vortex_adbdma_stopfifo(chip, dma);
375 dev_info(chip->card->dev, "wt stop %d\n", dma);
376 vortex_wtdma_stopfifo(chip, dma);
380 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
381 //printk(KERN_INFO "vortex: pause %d\n", dma);
382 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
383 vortex_adbdma_pausefifo(chip, dma);
386 vortex_wtdma_pausefifo(chip, dma);
389 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
390 //printk(KERN_INFO "vortex: resume %d\n", dma);
391 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
392 vortex_adbdma_resumefifo(chip, dma);
395 vortex_wtdma_resumefifo(chip, dma);
399 spin_unlock(&chip->lock);
402 spin_unlock(&chip->lock);
406 /* pointer callback */
407 static snd_pcm_uframes_t snd_vortex_pcm_pointer(struct snd_pcm_substream *substream)
409 vortex_t *chip = snd_pcm_substream_chip(substream);
410 stream_t *stream = (stream_t *) substream->runtime->private_data;
411 int dma = stream->dma;
412 snd_pcm_uframes_t current_ptr = 0;
414 spin_lock(&chip->lock);
415 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
416 current_ptr = vortex_adbdma_getlinearpos(chip, dma);
419 current_ptr = vortex_wtdma_getlinearpos(chip, dma);
421 //printk(KERN_INFO "vortex: pointer = 0x%x\n", current_ptr);
422 spin_unlock(&chip->lock);
423 current_ptr = bytes_to_frames(substream->runtime, current_ptr);
424 if (current_ptr >= substream->runtime->buffer_size)
430 static const struct snd_pcm_ops snd_vortex_playback_ops = {
431 .open = snd_vortex_pcm_open,
432 .close = snd_vortex_pcm_close,
433 .ioctl = snd_pcm_lib_ioctl,
434 .hw_params = snd_vortex_pcm_hw_params,
435 .hw_free = snd_vortex_pcm_hw_free,
436 .prepare = snd_vortex_pcm_prepare,
437 .trigger = snd_vortex_pcm_trigger,
438 .pointer = snd_vortex_pcm_pointer,
439 .page = snd_pcm_sgbuf_ops_page,
443 * definitions of capture are omitted here...
446 static char *vortex_pcm_prettyname[VORTEX_PCM_LAST] = {
453 static char *vortex_pcm_name[VORTEX_PCM_LAST] = {
463 static int snd_vortex_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
465 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
470 static int snd_vortex_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
472 ucontrol->value.iec958.status[0] = 0xff;
473 ucontrol->value.iec958.status[1] = 0xff;
474 ucontrol->value.iec958.status[2] = 0xff;
475 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
479 static int snd_vortex_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
481 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
482 ucontrol->value.iec958.status[0] = 0x00;
483 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL|IEC958_AES1_CON_DIGDIGCONV_ID;
484 ucontrol->value.iec958.status[2] = 0x00;
485 switch (vortex->spdif_sr) {
486 case 32000: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_32000; break;
487 case 44100: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_44100; break;
488 case 48000: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000; break;
493 static int snd_vortex_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
495 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
496 int spdif_sr = 48000;
497 switch (ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) {
498 case IEC958_AES3_CON_FS_32000: spdif_sr = 32000; break;
499 case IEC958_AES3_CON_FS_44100: spdif_sr = 44100; break;
500 case IEC958_AES3_CON_FS_48000: spdif_sr = 48000; break;
502 if (spdif_sr == vortex->spdif_sr)
504 vortex->spdif_sr = spdif_sr;
505 vortex_spdif_init(vortex, vortex->spdif_sr, 1);
510 static struct snd_kcontrol_new snd_vortex_mixer_spdif[] = {
512 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
513 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
514 .info = snd_vortex_spdif_info,
515 .get = snd_vortex_spdif_get,
516 .put = snd_vortex_spdif_put,
519 .access = SNDRV_CTL_ELEM_ACCESS_READ,
520 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
521 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
522 .info = snd_vortex_spdif_info,
523 .get = snd_vortex_spdif_mask_get
527 /* subdevice PCM Volume control */
529 static int snd_vortex_pcm_vol_info(struct snd_kcontrol *kcontrol,
530 struct snd_ctl_elem_info *uinfo)
532 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
533 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
534 uinfo->count = (VORTEX_IS_QUAD(vortex) ? 4 : 2);
535 uinfo->value.integer.min = -128;
536 uinfo->value.integer.max = 32;
540 static int snd_vortex_pcm_vol_get(struct snd_kcontrol *kcontrol,
541 struct snd_ctl_elem_value *ucontrol)
544 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
545 int subdev = kcontrol->id.subdevice;
546 struct pcm_vol *p = &vortex->pcm_vol[subdev];
547 int max_chn = (VORTEX_IS_QUAD(vortex) ? 4 : 2);
548 for (i = 0; i < max_chn; i++)
549 ucontrol->value.integer.value[i] = p->vol[i];
553 static int snd_vortex_pcm_vol_put(struct snd_kcontrol *kcontrol,
554 struct snd_ctl_elem_value *ucontrol)
560 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
561 int subdev = kcontrol->id.subdevice;
562 struct pcm_vol *p = &vortex->pcm_vol[subdev];
563 int max_chn = (VORTEX_IS_QUAD(vortex) ? 4 : 2);
564 for (i = 0; i < max_chn; i++) {
565 if (p->vol[i] != ucontrol->value.integer.value[i]) {
566 p->vol[i] = ucontrol->value.integer.value[i];
568 switch (vortex->dma_adb[p->dma].nr_ch) {
574 mixin = p->mixin[(i < 2) ? i : (i - 2)];
581 vortex_mix_setinputvolumebyte(vortex,
582 vortex->mixplayb[i], mixin, vol);
590 static const DECLARE_TLV_DB_MINMAX(vortex_pcm_vol_db_scale, -9600, 2400);
592 static const struct snd_kcontrol_new snd_vortex_pcm_vol = {
593 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
594 .name = "PCM Playback Volume",
595 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
596 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
597 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
598 .info = snd_vortex_pcm_vol_info,
599 .get = snd_vortex_pcm_vol_get,
600 .put = snd_vortex_pcm_vol_put,
601 .tlv = { .p = vortex_pcm_vol_db_scale },
604 /* create a pcm device */
605 static int snd_vortex_new_pcm(vortex_t *chip, int idx, int nr)
608 struct snd_kcontrol *kctl;
612 if (!chip || idx < 0 || idx >= VORTEX_PCM_LAST)
615 /* idx indicates which kind of PCM device. ADB, SPDIF, I2S and A3D share the
616 * same dma engine. WT uses it own separate dma engine which can't capture. */
617 if (idx == VORTEX_PCM_ADB)
621 err = snd_pcm_new(chip->card, vortex_pcm_prettyname[idx], idx, nr,
625 snprintf(pcm->name, sizeof(pcm->name),
626 "%s %s", CARD_NAME_SHORT, vortex_pcm_name[idx]);
627 chip->pcm[idx] = pcm;
628 // This is an evil hack, but it saves a lot of duplicated code.
629 VORTEX_PCM_TYPE(pcm) = idx;
630 pcm->private_data = chip;
632 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
633 &snd_vortex_playback_ops);
634 if (idx == VORTEX_PCM_ADB)
635 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
636 &snd_vortex_playback_ops);
638 /* pre-allocation of Scatter-Gather buffers */
640 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
641 snd_dma_pci_data(chip->pci_dev),
644 switch (VORTEX_PCM_TYPE(pcm)) {
646 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
648 VORTEX_IS_QUAD(chip) ? 4 : 2,
652 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
653 snd_pcm_std_chmaps, 2, 0, NULL);
659 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
660 snd_pcm_std_chmaps, 1, 0, NULL);
667 if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_SPDIF) {
668 for (i = 0; i < ARRAY_SIZE(snd_vortex_mixer_spdif); i++) {
669 kctl = snd_ctl_new1(&snd_vortex_mixer_spdif[i], chip);
672 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
676 if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_ADB) {
677 for (i = 0; i < NR_PCM; i++) {
678 chip->pcm_vol[i].active = 0;
679 chip->pcm_vol[i].dma = -1;
680 kctl = snd_ctl_new1(&snd_vortex_pcm_vol, chip);
683 chip->pcm_vol[i].kctl = kctl;
685 kctl->id.subdevice = i;
686 err = snd_ctl_add(chip->card, kctl);