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.
24 #include <sound/driver.h>
25 #include <linux/time.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
31 #define VORTEX_PCM_TYPE(x) (x->name[40])
33 /* hardware definition */
34 static snd_pcm_hardware_t snd_vortex_playback_hw_adb = {
36 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
37 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
38 SNDRV_PCM_INFO_MMAP_VALID),
40 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
41 SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
42 .rates = SNDRV_PCM_RATE_CONTINUOUS,
51 .buffer_bytes_max = 0x10000,
52 .period_bytes_min = 0x1,
53 .period_bytes_max = 0x1000,
59 static snd_pcm_hardware_t snd_vortex_playback_hw_a3d = {
61 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
62 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
63 SNDRV_PCM_INFO_MMAP_VALID),
65 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
66 SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
67 .rates = SNDRV_PCM_RATE_CONTINUOUS,
72 .buffer_bytes_max = 0x10000,
73 .period_bytes_min = 0x100,
74 .period_bytes_max = 0x1000,
79 static snd_pcm_hardware_t snd_vortex_playback_hw_spdif = {
81 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
82 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
83 SNDRV_PCM_INFO_MMAP_VALID),
85 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
86 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE | SNDRV_PCM_FMTBIT_MU_LAW |
87 SNDRV_PCM_FMTBIT_A_LAW,
89 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
94 .buffer_bytes_max = 0x10000,
95 .period_bytes_min = 0x100,
96 .period_bytes_max = 0x1000,
102 static snd_pcm_hardware_t snd_vortex_playback_hw_wt = {
103 .info = (SNDRV_PCM_INFO_MMAP |
104 SNDRV_PCM_INFO_INTERLEAVED |
105 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
106 .formats = SNDRV_PCM_FMTBIT_S16_LE,
107 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS, // SNDRV_PCM_RATE_48000,
112 .buffer_bytes_max = 0x10000,
113 .period_bytes_min = 0x0400,
114 .period_bytes_max = 0x1000,
120 static int snd_vortex_pcm_open(snd_pcm_substream_t * substream)
122 vortex_t *vortex = snd_pcm_substream_chip(substream);
123 snd_pcm_runtime_t *runtime = substream->runtime;
126 /* Force equal size periods */
128 snd_pcm_hw_constraint_integer(runtime,
129 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
131 /* Avoid PAGE_SIZE boundary to fall inside of a period. */
133 snd_pcm_hw_constraint_pow2(runtime, 0,
134 SNDRV_PCM_HW_PARAM_PERIOD_BYTES)) < 0)
137 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
139 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_A3D) {
140 runtime->hw = snd_vortex_playback_hw_a3d;
143 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_SPDIF) {
144 runtime->hw = snd_vortex_playback_hw_spdif;
145 switch (vortex->spdif_sr) {
147 runtime->hw.rates = SNDRV_PCM_RATE_32000;
150 runtime->hw.rates = SNDRV_PCM_RATE_44100;
153 runtime->hw.rates = SNDRV_PCM_RATE_48000;
157 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB
158 || VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_I2S)
159 runtime->hw = snd_vortex_playback_hw_adb;
160 substream->runtime->private_data = NULL;
164 runtime->hw = snd_vortex_playback_hw_wt;
165 substream->runtime->private_data = NULL;
172 static int snd_vortex_pcm_close(snd_pcm_substream_t * substream)
174 //vortex_t *chip = snd_pcm_substream_chip(substream);
175 stream_t *stream = (stream_t *) substream->runtime->private_data;
177 // the hardware-specific codes will be here
178 if (stream != NULL) {
179 stream->substream = NULL;
182 substream->runtime->private_data = NULL;
186 /* hw_params callback */
188 snd_vortex_pcm_hw_params(snd_pcm_substream_t * substream,
189 snd_pcm_hw_params_t * hw_params)
191 vortex_t *chip = snd_pcm_substream_chip(substream);
192 stream_t *stream = (stream_t *) (substream->runtime->private_data);
193 snd_pcm_sgbuf_t *sgbuf;
196 // Alloc buffer memory.
198 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
200 printk(KERN_ERR "Vortex: pcm page alloc failed!\n");
203 //sgbuf = (snd_pcm_sgbuf_t *) substream->runtime->dma_private;
204 sgbuf = snd_pcm_substream_sgbuf(substream);
206 printk(KERN_INFO "Vortex: periods %d, period_bytes %d, channels = %d\n", params_periods(hw_params),
207 params_period_bytes(hw_params), params_channels(hw_params));
209 // Make audio routes and config buffer DMA.
210 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
211 int dma, type = VORTEX_PCM_TYPE(substream->pcm);
212 /* Dealloc any routes. */
214 vortex_adb_allocroute(chip, stream->dma,
215 stream->nr_ch, stream->dir,
219 vortex_adb_allocroute(chip, -1,
220 params_channels(hw_params),
221 substream->stream, type);
224 stream = substream->runtime->private_data = &chip->dma_adb[dma];
225 stream->substream = substream;
227 vortex_adbdma_setbuffers(chip, dma, sgbuf,
228 params_period_bytes(hw_params),
229 params_periods(hw_params));
233 /* if (stream != NULL)
234 vortex_wt_allocroute(chip, substream->number, 0); */
235 vortex_wt_allocroute(chip, substream->number,
236 params_channels(hw_params));
237 stream = substream->runtime->private_data =
238 &chip->dma_wt[substream->number];
239 stream->dma = substream->number;
240 stream->substream = substream;
241 vortex_wtdma_setbuffers(chip, substream->number, sgbuf,
242 params_period_bytes(hw_params),
243 params_periods(hw_params));
249 /* hw_free callback */
250 static int snd_vortex_pcm_hw_free(snd_pcm_substream_t * substream)
252 vortex_t *chip = snd_pcm_substream_chip(substream);
253 stream_t *stream = (stream_t *) (substream->runtime->private_data);
255 // Delete audio routes.
256 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
258 vortex_adb_allocroute(chip, stream->dma,
259 stream->nr_ch, stream->dir,
265 vortex_wt_allocroute(chip, stream->dma, 0);
268 substream->runtime->private_data = NULL;
270 return snd_pcm_lib_free_pages(substream);
273 /* prepare callback */
274 static int snd_vortex_pcm_prepare(snd_pcm_substream_t * substream)
276 vortex_t *chip = snd_pcm_substream_chip(substream);
277 snd_pcm_runtime_t *runtime = substream->runtime;
278 stream_t *stream = (stream_t *) substream->runtime->private_data;
279 int dma = stream->dma, fmt, dir;
281 // set up the hardware with the current configuration.
282 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
286 fmt = vortex_alsafmt_aspfmt(runtime->format);
287 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
288 vortex_adbdma_setmode(chip, dma, 1, dir, fmt, 0 /*? */ ,
290 vortex_adbdma_setstartbuffer(chip, dma, 0);
291 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_SPDIF)
292 vortex_adb_setsrc(chip, dma, runtime->rate, dir);
296 vortex_wtdma_setmode(chip, dma, 1, fmt, 0, 0);
297 // FIXME: Set rate (i guess using vortex_wt_writereg() somehow).
298 vortex_wtdma_setstartbuffer(chip, dma, 0);
304 /* trigger callback */
305 static int snd_vortex_pcm_trigger(snd_pcm_substream_t * substream, int cmd)
307 vortex_t *chip = snd_pcm_substream_chip(substream);
308 stream_t *stream = (stream_t *) substream->runtime->private_data;
309 int dma = stream->dma;
312 case SNDRV_PCM_TRIGGER_START:
313 // do something to start the PCM engine
314 //printk(KERN_INFO "vortex: start %d\n", dma);
315 stream->fifo_enabled = 1;
316 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
317 vortex_adbdma_startfifo(chip, dma);
320 printk(KERN_INFO "vortex: wt start %d\n", dma);
321 vortex_wtdma_startfifo(chip, dma);
325 case SNDRV_PCM_TRIGGER_STOP:
326 // do something to stop the PCM engine
327 //printk(KERN_INFO "vortex: stop %d\n", dma);
328 stream->fifo_enabled = 0;
329 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
330 vortex_adbdma_pausefifo(chip, dma);
331 //vortex_adbdma_stopfifo(chip, dma);
334 printk(KERN_INFO "vortex: wt stop %d\n", dma);
335 vortex_wtdma_stopfifo(chip, dma);
339 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
340 //printk(KERN_INFO "vortex: pause %d\n", dma);
341 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
342 vortex_adbdma_pausefifo(chip, dma);
345 vortex_wtdma_pausefifo(chip, dma);
348 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
349 //printk(KERN_INFO "vortex: resume %d\n", dma);
350 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
351 vortex_adbdma_resumefifo(chip, dma);
354 vortex_wtdma_resumefifo(chip, dma);
363 /* pointer callback */
364 static snd_pcm_uframes_t snd_vortex_pcm_pointer(snd_pcm_substream_t * substream)
366 vortex_t *chip = snd_pcm_substream_chip(substream);
367 stream_t *stream = (stream_t *) substream->runtime->private_data;
368 int dma = stream->dma;
369 snd_pcm_uframes_t current_ptr = 0;
371 spin_lock(&chip->lock);
372 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
373 current_ptr = vortex_adbdma_getlinearpos(chip, dma);
376 current_ptr = vortex_wtdma_getlinearpos(chip, dma);
378 //printk(KERN_INFO "vortex: pointer = 0x%x\n", current_ptr);
379 spin_unlock(&chip->lock);
380 return (bytes_to_frames(substream->runtime, current_ptr));
385 static struct page *snd_pcm_sgbuf_ops_page(snd_pcm_substream_t *substream, unsigned long offset) {
391 static snd_pcm_ops_t snd_vortex_playback_ops = {
392 .open = snd_vortex_pcm_open,
393 .close = snd_vortex_pcm_close,
394 .ioctl = snd_pcm_lib_ioctl,
395 .hw_params = snd_vortex_pcm_hw_params,
396 .hw_free = snd_vortex_pcm_hw_free,
397 .prepare = snd_vortex_pcm_prepare,
398 .trigger = snd_vortex_pcm_trigger,
399 .pointer = snd_vortex_pcm_pointer,
400 .page = snd_pcm_sgbuf_ops_page,
404 * definitions of capture are omitted here...
407 static char *vortex_pcm_prettyname[VORTEX_PCM_LAST] = {
414 static char *vortex_pcm_name[VORTEX_PCM_LAST] = {
424 snd_vortex_spdif_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
426 static char *texts[] = { "32000", "44100", "48000" };
428 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
430 uinfo->value.enumerated.items = 3;
431 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
432 uinfo->value.enumerated.item =
433 uinfo->value.enumerated.items - 1;
434 strcpy(uinfo->value.enumerated.name,
435 texts[uinfo->value.enumerated.item]);
439 snd_vortex_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
441 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
443 if (vortex->spdif_sr == 32000)
444 ucontrol->value.enumerated.item[0] = 0;
445 if (vortex->spdif_sr == 44100)
446 ucontrol->value.enumerated.item[0] = 1;
447 if (vortex->spdif_sr == 48000)
448 ucontrol->value.enumerated.item[0] = 2;
452 snd_vortex_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
454 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
455 static unsigned int sr[3] = { 32000, 44100, 48000 };
457 //printk("vortex: spdif sr = %d\n", ucontrol->value.enumerated.item[0]);
458 vortex->spdif_sr = sr[ucontrol->value.enumerated.item[0] % 3];
459 vortex_spdif_init(vortex,
460 sr[ucontrol->value.enumerated.item[0] % 3], 1);
463 static snd_kcontrol_new_t vortex_spdif_kcontrol __devinitdata = {
464 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
467 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
469 .info = snd_vortex_spdif_info,
470 .get = snd_vortex_spdif_get,
471 .put = snd_vortex_spdif_put
474 /* create a pcm device */
475 static int __devinit snd_vortex_new_pcm(vortex_t * chip, int idx, int nr)
480 if ((chip == 0) || (idx < 0) || (idx > VORTEX_PCM_LAST))
483 /* idx indicates which kind of PCM device. ADB, SPDIF, I2S and A3D share the
484 * same dma engine. WT uses it own separate dma engine whcih cant capture. */
485 if (idx == VORTEX_PCM_ADB)
490 snd_pcm_new(chip->card, vortex_pcm_prettyname[idx], idx, nr,
493 strcpy(pcm->name, vortex_pcm_name[idx]);
494 chip->pcm[idx] = pcm;
495 // This is an evil hack, but it saves a lot of duplicated code.
496 VORTEX_PCM_TYPE(pcm) = idx;
497 pcm->private_data = chip;
499 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
500 &snd_vortex_playback_ops);
501 if (idx == VORTEX_PCM_ADB)
502 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
503 &snd_vortex_playback_ops);
505 /* pre-allocation of Scatter-Gather buffers */
507 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
508 snd_dma_pci_data(chip->pci_dev),
511 if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_SPDIF) {
512 snd_kcontrol_t *kcontrol;
515 snd_ctl_new1(&vortex_spdif_kcontrol, chip)) == NULL)
517 if ((err = snd_ctl_add(chip->card, kcontrol)) < 0)