2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Vortex PCM ALSA driver.
20 * Supports ADB and WT DMA. Unfortunately, WT channels do not run yet.
21 * It remains stuck,and DMA transfers do not happen.
25 #include <sound/driver.h>
26 #include <linux/time.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
32 #define chip_t vortex_t
33 #define VORTEX_PCM_TYPE(x) (x->name[40])
35 /* hardware definition */
36 static snd_pcm_hardware_t snd_vortex_playback_hw_adb = {
38 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
39 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
40 SNDRV_PCM_INFO_MMAP_VALID),
42 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
43 SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
44 .rates = SNDRV_PCM_RATE_CONTINUOUS,
53 .buffer_bytes_max = 0x10000,
54 .period_bytes_min = 0x1,
55 .period_bytes_max = 0x1000,
61 static snd_pcm_hardware_t snd_vortex_playback_hw_a3d = {
63 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
64 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
65 SNDRV_PCM_INFO_MMAP_VALID),
67 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
68 SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
69 .rates = SNDRV_PCM_RATE_CONTINUOUS,
74 .buffer_bytes_max = 0x10000,
75 .period_bytes_min = 0x100,
76 .period_bytes_max = 0x1000,
81 static snd_pcm_hardware_t snd_vortex_playback_hw_spdif = {
83 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
84 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
85 SNDRV_PCM_INFO_MMAP_VALID),
87 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
88 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE | SNDRV_PCM_FMTBIT_MU_LAW |
89 SNDRV_PCM_FMTBIT_A_LAW,
91 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
96 .buffer_bytes_max = 0x10000,
97 .period_bytes_min = 0x100,
98 .period_bytes_max = 0x1000,
104 static snd_pcm_hardware_t snd_vortex_playback_hw_wt = {
105 .info = (SNDRV_PCM_INFO_MMAP |
106 SNDRV_PCM_INFO_INTERLEAVED |
107 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
108 .formats = SNDRV_PCM_FMTBIT_S16_LE,
109 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS, // SNDRV_PCM_RATE_48000,
114 .buffer_bytes_max = 0x10000,
115 .period_bytes_min = 0x0400,
116 .period_bytes_max = 0x1000,
122 static int snd_vortex_pcm_open(snd_pcm_substream_t * substream)
124 vortex_t *vortex = snd_pcm_substream_chip(substream);
125 snd_pcm_runtime_t *runtime = substream->runtime;
128 /* Force equal size periods */
130 snd_pcm_hw_constraint_integer(runtime,
131 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
133 /* Avoid PAGE_SIZE boundary to fall inside of a period. */
135 snd_pcm_hw_constraint_pow2(runtime, 0,
136 SNDRV_PCM_HW_PARAM_PERIOD_BYTES)) < 0)
139 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
141 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_A3D) {
142 runtime->hw = snd_vortex_playback_hw_a3d;
145 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_SPDIF) {
146 runtime->hw = snd_vortex_playback_hw_spdif;
147 switch (vortex->spdif_sr) {
149 runtime->hw.rates = SNDRV_PCM_RATE_32000;
152 runtime->hw.rates = SNDRV_PCM_RATE_44100;
155 runtime->hw.rates = SNDRV_PCM_RATE_48000;
159 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB
160 || VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_I2S)
161 runtime->hw = snd_vortex_playback_hw_adb;
162 substream->runtime->private_data = NULL;
166 runtime->hw = snd_vortex_playback_hw_wt;
167 substream->runtime->private_data = NULL;
174 static int snd_vortex_pcm_close(snd_pcm_substream_t * substream)
176 //vortex_t *chip = snd_pcm_substream_chip(substream);
177 stream_t *stream = (stream_t *) substream->runtime->private_data;
179 // the hardware-specific codes will be here
180 if (stream != NULL) {
181 stream->substream = NULL;
184 substream->runtime->private_data = NULL;
188 /* hw_params callback */
190 snd_vortex_pcm_hw_params(snd_pcm_substream_t * substream,
191 snd_pcm_hw_params_t * hw_params)
193 chip_t *chip = snd_pcm_substream_chip(substream);
194 stream_t *stream = (stream_t *) (substream->runtime->private_data);
195 snd_pcm_sgbuf_t *sgbuf;
198 // Alloc buffer memory.
200 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
202 printk(KERN_ERR "Vortex: pcm page alloc failed!\n");
205 //sgbuf = (snd_pcm_sgbuf_t *) substream->runtime->dma_private;
206 sgbuf = snd_pcm_substream_sgbuf(substream);
208 printk(KERN_INFO "Vortex: periods %d, period_bytes %d, channels = %d\n", params_periods(hw_params),
209 params_period_bytes(hw_params), params_channels(hw_params));
211 // Make audio routes and config buffer DMA.
212 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
213 int dma, type = VORTEX_PCM_TYPE(substream->pcm);
214 /* Dealloc any routes. */
216 vortex_adb_allocroute(chip, stream->dma,
217 stream->nr_ch, stream->dir,
221 vortex_adb_allocroute(chip, -1,
222 params_channels(hw_params),
223 substream->stream, type);
226 stream = substream->runtime->private_data = &chip->dma_adb[dma];
227 stream->substream = substream;
229 vortex_adbdma_setbuffers(chip, dma, sgbuf,
230 params_period_bytes(hw_params),
231 params_periods(hw_params));
235 /*if (stream != NULL)
236 vortex_wt_allocroute(chip, substream->number, 0); */
237 vortex_wt_allocroute(chip, substream->number,
238 params_channels(hw_params));
239 stream = substream->runtime->private_data =
240 &chip->dma_wt[substream->number];
241 stream->substream = substream;
242 vortex_wtdma_setbuffers(chip, substream->number, sgbuf,
243 params_period_bytes(hw_params),
244 params_periods(hw_params));
250 /* hw_free callback */
251 static int snd_vortex_pcm_hw_free(snd_pcm_substream_t * substream)
253 chip_t *chip = snd_pcm_substream_chip(substream);
254 stream_t *stream = (stream_t *) (substream->runtime->private_data);
256 // Delete audio routes.
257 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
259 vortex_adb_allocroute(chip, stream->dma,
260 stream->nr_ch, stream->dir,
266 vortex_wt_allocroute(chip, stream->dma, 0);
269 substream->runtime->private_data = NULL;
271 return snd_pcm_lib_free_pages(substream);
274 /* prepare callback */
275 static int snd_vortex_pcm_prepare(snd_pcm_substream_t * substream)
277 vortex_t *chip = snd_pcm_substream_chip(substream);
278 snd_pcm_runtime_t *runtime = substream->runtime;
279 stream_t *stream = (stream_t *) substream->runtime->private_data;
280 int dma = stream->dma, fmt, dir;
282 // set up the hardware with the current configuration.
283 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
287 fmt = vortex_alsafmt_aspfmt(runtime->format);
288 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
289 vortex_adbdma_setmode(chip, dma, 1, dir, fmt, 0 /*? */ ,
291 vortex_adbdma_setstartbuffer(chip, dma, 0);
292 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_SPDIF)
293 vortex_adb_setsrc(chip, dma, runtime->rate, dir);
297 vortex_wtdma_setmode(chip, dma, 1, fmt, 0, 0);
298 // FIXME: Set rate (i guess using vortex_wt_writereg() somehow).
299 vortex_wtdma_setstartbuffer(chip, dma, 0);
305 /* trigger callback */
306 static int snd_vortex_pcm_trigger(snd_pcm_substream_t * substream, int cmd)
308 chip_t *chip = snd_pcm_substream_chip(substream);
309 stream_t *stream = (stream_t *) substream->runtime->private_data;
310 int dma = stream->dma;
313 case SNDRV_PCM_TRIGGER_START:
314 // do something to start the PCM engine
315 //printk(KERN_INFO "vortex: start %d\n", dma);
316 stream->fifo_enabled = 1;
317 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
318 vortex_adbdma_startfifo(chip, dma);
321 printk(KERN_INFO "vortex: wt start %d\n", dma);
322 vortex_wtdma_startfifo(chip, dma);
326 case SNDRV_PCM_TRIGGER_STOP:
327 // do something to stop the PCM engine
328 //printk(KERN_INFO "vortex: stop %d\n", dma)
329 stream->fifo_enabled = 0;
330 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
331 vortex_adbdma_pausefifo(chip, dma);
332 //vortex_adbdma_stopfifo(chip, dma);
335 printk(KERN_INFO "vortex: wt stop %d\n", dma);
336 vortex_wtdma_stopfifo(chip, dma);
340 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
341 //printk(KERN_INFO "vortex: pause %d\n", dma);
342 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
343 vortex_adbdma_pausefifo(chip, dma);
346 vortex_wtdma_pausefifo(chip, dma);
349 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
350 //printk(KERN_INFO "vortex: resume %d\n", dma);
351 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
352 vortex_adbdma_resumefifo(chip, dma);
355 vortex_wtdma_resumefifo(chip, dma);
364 /* pointer callback */
365 static snd_pcm_uframes_t snd_vortex_pcm_pointer(snd_pcm_substream_t * substream)
367 vortex_t *chip = snd_pcm_substream_chip(substream);
368 stream_t *stream = (stream_t *) substream->runtime->private_data;
369 int dma = stream->dma;
370 snd_pcm_uframes_t current_ptr = 0;
372 spin_lock(&chip->lock);
373 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
374 current_ptr = vortex_adbdma_getlinearpos(chip, dma);
377 current_ptr = vortex_wtdma_getlinearpos(chip, dma);
379 //printk(KERN_INFO "vortex: pointer = 0x%x\n", current_ptr);
380 spin_unlock(&chip->lock);
381 return (bytes_to_frames(substream->runtime, current_ptr));
386 static struct page *snd_pcm_sgbuf_ops_page(snd_pcm_substream_t *substream, unsigned long offset) {
392 static snd_pcm_ops_t snd_vortex_playback_ops = {
393 .open = snd_vortex_pcm_open,
394 .close = snd_vortex_pcm_close,
395 .ioctl = snd_pcm_lib_ioctl,
396 .hw_params = snd_vortex_pcm_hw_params,
397 .hw_free = snd_vortex_pcm_hw_free,
398 .prepare = snd_vortex_pcm_prepare,
399 .trigger = snd_vortex_pcm_trigger,
400 .pointer = snd_vortex_pcm_pointer,
401 .page = snd_pcm_sgbuf_ops_page,
405 * definitions of capture are omitted here...
408 static char *vortex_pcm_prettyname[VORTEX_PCM_LAST] = {
415 static char *vortex_pcm_name[VORTEX_PCM_LAST] = {
425 snd_vortex_spdif_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
427 static char *texts[] = { "32000", "44100", "48000" };
429 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
431 uinfo->value.enumerated.items = 3;
432 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
433 uinfo->value.enumerated.item =
434 uinfo->value.enumerated.items - 1;
435 strcpy(uinfo->value.enumerated.name,
436 texts[uinfo->value.enumerated.item]);
440 snd_vortex_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
442 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
444 if (vortex->spdif_sr == 32000)
445 ucontrol->value.enumerated.item[0] = 0;
446 if (vortex->spdif_sr == 44100)
447 ucontrol->value.enumerated.item[0] = 1;
448 if (vortex->spdif_sr == 48000)
449 ucontrol->value.enumerated.item[0] = 2;
453 snd_vortex_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
455 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
456 static unsigned int sr[3] = { 32000, 44100, 48000 };
458 //printk("vortex: spdif sr = %d\n", ucontrol->value.enumerated.item[0]);
459 vortex->spdif_sr = sr[ucontrol->value.enumerated.item[0] % 3];
460 vortex_spdif_init(vortex,
461 sr[ucontrol->value.enumerated.item[0] % 3], 1);
464 static snd_kcontrol_new_t vortex_spdif_kcontrol __devinitdata = {
465 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
468 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
470 .info = snd_vortex_spdif_info,
471 .get = snd_vortex_spdif_get,
472 .put = snd_vortex_spdif_put
475 /* create a pcm device */
476 static int __devinit snd_vortex_new_pcm(vortex_t * chip, int idx, int nr)
481 if ((chip == 0) || (idx < 0) || (idx > VORTEX_PCM_LAST))
484 /* idx indicates which kind of PCM device. ADB, SPDIF, I2S and A3D share the
485 * same dma engine. WT uses it own separate dma engine whcih cant capture. */
486 if (idx == VORTEX_PCM_ADB)
491 snd_pcm_new(chip->card, vortex_pcm_prettyname[idx], idx, nr,
494 strcpy(pcm->name, vortex_pcm_name[idx]);
495 chip->pcm[idx] = pcm;
496 // This is an evil hack, but it saves a lot of duplicated code.
497 VORTEX_PCM_TYPE(pcm) = idx;
498 pcm->private_data = chip;
500 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
501 &snd_vortex_playback_ops);
502 if (idx == VORTEX_PCM_ADB)
503 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
504 &snd_vortex_playback_ops);
506 /* pre-allocation of Scatter-Gather buffers */
508 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
509 snd_dma_pci_data(chip->pci_dev),
512 // The above should be used, as soon as ALSA gets updated.
514 snd_pcm_lib_preallocate_sg_pages_for_all(chip->pci_dev, pcm,
517 if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_SPDIF) {
518 snd_kcontrol_t *kcontrol;
521 snd_ctl_new1(&vortex_spdif_kcontrol, chip)) == NULL)
523 if ((err = snd_ctl_add(chip->card, kcontrol)) < 0)