Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / sound / pci / au88x0 / au88x0_pcm.c
blob526c6c5ecf7b658f7e1acb9d011ff437f9919c61
1 /*
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.
23 #include <sound/asoundef.h>
24 #include <linux/time.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include "au88x0.h"
30 #define VORTEX_PCM_TYPE(x) (x->name[40])
32 /* hardware definition */
33 static struct snd_pcm_hardware snd_vortex_playback_hw_adb = {
34 .info =
35 (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
36 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
37 SNDRV_PCM_INFO_MMAP_VALID),
38 .formats =
39 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
40 SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
41 .rates = SNDRV_PCM_RATE_CONTINUOUS,
42 .rate_min = 5000,
43 .rate_max = 48000,
44 .channels_min = 1,
45 #ifdef CHIP_AU8830
46 .channels_max = 4,
47 #else
48 .channels_max = 2,
49 #endif
50 .buffer_bytes_max = 0x10000,
51 .period_bytes_min = 0x1,
52 .period_bytes_max = 0x1000,
53 .periods_min = 2,
54 .periods_max = 32,
57 #ifndef CHIP_AU8820
58 static struct snd_pcm_hardware snd_vortex_playback_hw_a3d = {
59 .info =
60 (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
61 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
62 SNDRV_PCM_INFO_MMAP_VALID),
63 .formats =
64 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
65 SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
66 .rates = SNDRV_PCM_RATE_CONTINUOUS,
67 .rate_min = 5000,
68 .rate_max = 48000,
69 .channels_min = 1,
70 .channels_max = 1,
71 .buffer_bytes_max = 0x10000,
72 .period_bytes_min = 0x100,
73 .period_bytes_max = 0x1000,
74 .periods_min = 2,
75 .periods_max = 64,
77 #endif
78 static struct snd_pcm_hardware snd_vortex_playback_hw_spdif = {
79 .info =
80 (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
81 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
82 SNDRV_PCM_INFO_MMAP_VALID),
83 .formats =
84 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
85 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE | SNDRV_PCM_FMTBIT_MU_LAW |
86 SNDRV_PCM_FMTBIT_A_LAW,
87 .rates =
88 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
89 .rate_min = 32000,
90 .rate_max = 48000,
91 .channels_min = 1,
92 .channels_max = 2,
93 .buffer_bytes_max = 0x10000,
94 .period_bytes_min = 0x100,
95 .period_bytes_max = 0x1000,
96 .periods_min = 2,
97 .periods_max = 64,
100 #ifndef CHIP_AU8810
101 static struct snd_pcm_hardware snd_vortex_playback_hw_wt = {
102 .info = (SNDRV_PCM_INFO_MMAP |
103 SNDRV_PCM_INFO_INTERLEAVED |
104 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
105 .formats = SNDRV_PCM_FMTBIT_S16_LE,
106 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS, // SNDRV_PCM_RATE_48000,
107 .rate_min = 8000,
108 .rate_max = 48000,
109 .channels_min = 1,
110 .channels_max = 2,
111 .buffer_bytes_max = 0x10000,
112 .period_bytes_min = 0x0400,
113 .period_bytes_max = 0x1000,
114 .periods_min = 2,
115 .periods_max = 64,
117 #endif
118 /* open callback */
119 static int snd_vortex_pcm_open(struct snd_pcm_substream *substream)
121 vortex_t *vortex = snd_pcm_substream_chip(substream);
122 struct snd_pcm_runtime *runtime = substream->runtime;
123 int err;
125 /* Force equal size periods */
126 if ((err =
127 snd_pcm_hw_constraint_integer(runtime,
128 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
129 return err;
130 /* Avoid PAGE_SIZE boundary to fall inside of a period. */
131 if ((err =
132 snd_pcm_hw_constraint_pow2(runtime, 0,
133 SNDRV_PCM_HW_PARAM_PERIOD_BYTES)) < 0)
134 return err;
136 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
137 #ifndef CHIP_AU8820
138 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_A3D) {
139 runtime->hw = snd_vortex_playback_hw_a3d;
141 #endif
142 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_SPDIF) {
143 runtime->hw = snd_vortex_playback_hw_spdif;
144 switch (vortex->spdif_sr) {
145 case 32000:
146 runtime->hw.rates = SNDRV_PCM_RATE_32000;
147 break;
148 case 44100:
149 runtime->hw.rates = SNDRV_PCM_RATE_44100;
150 break;
151 case 48000:
152 runtime->hw.rates = SNDRV_PCM_RATE_48000;
153 break;
156 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB
157 || VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_I2S)
158 runtime->hw = snd_vortex_playback_hw_adb;
159 substream->runtime->private_data = NULL;
161 #ifndef CHIP_AU8810
162 else {
163 runtime->hw = snd_vortex_playback_hw_wt;
164 substream->runtime->private_data = NULL;
166 #endif
167 return 0;
170 /* close callback */
171 static int snd_vortex_pcm_close(struct snd_pcm_substream *substream)
173 //vortex_t *chip = snd_pcm_substream_chip(substream);
174 stream_t *stream = (stream_t *) substream->runtime->private_data;
176 // the hardware-specific codes will be here
177 if (stream != NULL) {
178 stream->substream = NULL;
179 stream->nr_ch = 0;
181 substream->runtime->private_data = NULL;
182 return 0;
185 /* hw_params callback */
186 static int
187 snd_vortex_pcm_hw_params(struct snd_pcm_substream *substream,
188 struct snd_pcm_hw_params *hw_params)
190 vortex_t *chip = snd_pcm_substream_chip(substream);
191 stream_t *stream = (stream_t *) (substream->runtime->private_data);
192 struct snd_sg_buf *sgbuf;
193 int err;
195 // Alloc buffer memory.
196 err =
197 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
198 if (err < 0) {
199 printk(KERN_ERR "Vortex: pcm page alloc failed!\n");
200 return err;
202 //sgbuf = (struct snd_sg_buf *) substream->runtime->dma_private;
203 sgbuf = snd_pcm_substream_sgbuf(substream);
205 printk(KERN_INFO "Vortex: periods %d, period_bytes %d, channels = %d\n", params_periods(hw_params),
206 params_period_bytes(hw_params), params_channels(hw_params));
208 spin_lock_irq(&chip->lock);
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. */
213 if (stream != NULL)
214 vortex_adb_allocroute(chip, stream->dma,
215 stream->nr_ch, stream->dir,
216 stream->type);
217 /* Alloc routes. */
218 dma =
219 vortex_adb_allocroute(chip, -1,
220 params_channels(hw_params),
221 substream->stream, type);
222 if (dma < 0) {
223 spin_unlock_irq(&chip->lock);
224 return dma;
226 stream = substream->runtime->private_data = &chip->dma_adb[dma];
227 stream->substream = substream;
228 /* Setup Buffers. */
229 vortex_adbdma_setbuffers(chip, dma, sgbuf,
230 params_period_bytes(hw_params),
231 params_periods(hw_params));
233 #ifndef CHIP_AU8810
234 else {
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->dma = substream->number;
242 stream->substream = substream;
243 vortex_wtdma_setbuffers(chip, substream->number, sgbuf,
244 params_period_bytes(hw_params),
245 params_periods(hw_params));
247 #endif
248 spin_unlock_irq(&chip->lock);
249 return 0;
252 /* hw_free callback */
253 static int snd_vortex_pcm_hw_free(struct snd_pcm_substream *substream)
255 vortex_t *chip = snd_pcm_substream_chip(substream);
256 stream_t *stream = (stream_t *) (substream->runtime->private_data);
258 spin_lock_irq(&chip->lock);
259 // Delete audio routes.
260 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
261 if (stream != NULL)
262 vortex_adb_allocroute(chip, stream->dma,
263 stream->nr_ch, stream->dir,
264 stream->type);
266 #ifndef CHIP_AU8810
267 else {
268 if (stream != NULL)
269 vortex_wt_allocroute(chip, stream->dma, 0);
271 #endif
272 substream->runtime->private_data = NULL;
273 spin_unlock_irq(&chip->lock);
275 return snd_pcm_lib_free_pages(substream);
278 /* prepare callback */
279 static int snd_vortex_pcm_prepare(struct snd_pcm_substream *substream)
281 vortex_t *chip = snd_pcm_substream_chip(substream);
282 struct snd_pcm_runtime *runtime = substream->runtime;
283 stream_t *stream = (stream_t *) substream->runtime->private_data;
284 int dma = stream->dma, fmt, dir;
286 // set up the hardware with the current configuration.
287 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
288 dir = 1;
289 else
290 dir = 0;
291 fmt = vortex_alsafmt_aspfmt(runtime->format);
292 spin_lock_irq(&chip->lock);
293 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
294 vortex_adbdma_setmode(chip, dma, 1, dir, fmt, 0 /*? */ ,
296 vortex_adbdma_setstartbuffer(chip, dma, 0);
297 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_SPDIF)
298 vortex_adb_setsrc(chip, dma, runtime->rate, dir);
300 #ifndef CHIP_AU8810
301 else {
302 vortex_wtdma_setmode(chip, dma, 1, fmt, 0, 0);
303 // FIXME: Set rate (i guess using vortex_wt_writereg() somehow).
304 vortex_wtdma_setstartbuffer(chip, dma, 0);
306 #endif
307 spin_unlock_irq(&chip->lock);
308 return 0;
311 /* trigger callback */
312 static int snd_vortex_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
314 vortex_t *chip = snd_pcm_substream_chip(substream);
315 stream_t *stream = (stream_t *) substream->runtime->private_data;
316 int dma = stream->dma;
318 spin_lock(&chip->lock);
319 switch (cmd) {
320 case SNDRV_PCM_TRIGGER_START:
321 // do something to start the PCM engine
322 //printk(KERN_INFO "vortex: start %d\n", dma);
323 stream->fifo_enabled = 1;
324 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
325 vortex_adbdma_resetup(chip, dma);
326 vortex_adbdma_startfifo(chip, dma);
328 #ifndef CHIP_AU8810
329 else {
330 printk(KERN_INFO "vortex: wt start %d\n", dma);
331 vortex_wtdma_startfifo(chip, dma);
333 #endif
334 break;
335 case SNDRV_PCM_TRIGGER_STOP:
336 // do something to stop the PCM engine
337 //printk(KERN_INFO "vortex: stop %d\n", dma);
338 stream->fifo_enabled = 0;
339 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
340 vortex_adbdma_pausefifo(chip, dma);
341 //vortex_adbdma_stopfifo(chip, dma);
342 #ifndef CHIP_AU8810
343 else {
344 printk(KERN_INFO "vortex: wt stop %d\n", dma);
345 vortex_wtdma_stopfifo(chip, dma);
347 #endif
348 break;
349 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
350 //printk(KERN_INFO "vortex: pause %d\n", dma);
351 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
352 vortex_adbdma_pausefifo(chip, dma);
353 #ifndef CHIP_AU8810
354 else
355 vortex_wtdma_pausefifo(chip, dma);
356 #endif
357 break;
358 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
359 //printk(KERN_INFO "vortex: resume %d\n", dma);
360 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
361 vortex_adbdma_resumefifo(chip, dma);
362 #ifndef CHIP_AU8810
363 else
364 vortex_wtdma_resumefifo(chip, dma);
365 #endif
366 break;
367 default:
368 spin_unlock(&chip->lock);
369 return -EINVAL;
371 spin_unlock(&chip->lock);
372 return 0;
375 /* pointer callback */
376 static snd_pcm_uframes_t snd_vortex_pcm_pointer(struct snd_pcm_substream *substream)
378 vortex_t *chip = snd_pcm_substream_chip(substream);
379 stream_t *stream = (stream_t *) substream->runtime->private_data;
380 int dma = stream->dma;
381 snd_pcm_uframes_t current_ptr = 0;
383 spin_lock(&chip->lock);
384 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
385 current_ptr = vortex_adbdma_getlinearpos(chip, dma);
386 #ifndef CHIP_AU8810
387 else
388 current_ptr = vortex_wtdma_getlinearpos(chip, dma);
389 #endif
390 //printk(KERN_INFO "vortex: pointer = 0x%x\n", current_ptr);
391 spin_unlock(&chip->lock);
392 return (bytes_to_frames(substream->runtime, current_ptr));
395 /* Page callback. */
397 static struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset) {
402 /* operators */
403 static struct snd_pcm_ops snd_vortex_playback_ops = {
404 .open = snd_vortex_pcm_open,
405 .close = snd_vortex_pcm_close,
406 .ioctl = snd_pcm_lib_ioctl,
407 .hw_params = snd_vortex_pcm_hw_params,
408 .hw_free = snd_vortex_pcm_hw_free,
409 .prepare = snd_vortex_pcm_prepare,
410 .trigger = snd_vortex_pcm_trigger,
411 .pointer = snd_vortex_pcm_pointer,
412 .page = snd_pcm_sgbuf_ops_page,
416 * definitions of capture are omitted here...
419 static char *vortex_pcm_prettyname[VORTEX_PCM_LAST] = {
420 "AU88x0 ADB",
421 "AU88x0 SPDIF",
422 "AU88x0 A3D",
423 "AU88x0 WT",
424 "AU88x0 I2S",
426 static char *vortex_pcm_name[VORTEX_PCM_LAST] = {
427 "adb",
428 "spdif",
429 "a3d",
430 "wt",
431 "i2s",
434 /* SPDIF kcontrol */
436 static int snd_vortex_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
438 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
439 uinfo->count = 1;
440 return 0;
443 static int snd_vortex_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
445 ucontrol->value.iec958.status[0] = 0xff;
446 ucontrol->value.iec958.status[1] = 0xff;
447 ucontrol->value.iec958.status[2] = 0xff;
448 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
449 return 0;
452 static int snd_vortex_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
454 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
455 ucontrol->value.iec958.status[0] = 0x00;
456 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL|IEC958_AES1_CON_DIGDIGCONV_ID;
457 ucontrol->value.iec958.status[2] = 0x00;
458 switch (vortex->spdif_sr) {
459 case 32000: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_32000; break;
460 case 44100: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_44100; break;
461 case 48000: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000; break;
463 return 0;
466 static int snd_vortex_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
468 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
469 int spdif_sr = 48000;
470 switch (ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) {
471 case IEC958_AES3_CON_FS_32000: spdif_sr = 32000; break;
472 case IEC958_AES3_CON_FS_44100: spdif_sr = 44100; break;
473 case IEC958_AES3_CON_FS_48000: spdif_sr = 48000; break;
475 if (spdif_sr == vortex->spdif_sr)
476 return 0;
477 vortex->spdif_sr = spdif_sr;
478 vortex_spdif_init(vortex, vortex->spdif_sr, 1);
479 return 1;
482 /* spdif controls */
483 static struct snd_kcontrol_new snd_vortex_mixer_spdif[] __devinitdata = {
485 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
486 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
487 .info = snd_vortex_spdif_info,
488 .get = snd_vortex_spdif_get,
489 .put = snd_vortex_spdif_put,
492 .access = SNDRV_CTL_ELEM_ACCESS_READ,
493 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
494 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
495 .info = snd_vortex_spdif_info,
496 .get = snd_vortex_spdif_mask_get
500 /* create a pcm device */
501 static int __devinit snd_vortex_new_pcm(vortex_t * chip, int idx, int nr)
503 struct snd_pcm *pcm;
504 struct snd_kcontrol *kctl;
505 int i;
506 int err, nr_capt;
508 if ((chip == 0) || (idx < 0) || (idx >= VORTEX_PCM_LAST))
509 return -ENODEV;
511 /* idx indicates which kind of PCM device. ADB, SPDIF, I2S and A3D share the
512 * same dma engine. WT uses it own separate dma engine whcih cant capture. */
513 if (idx == VORTEX_PCM_ADB)
514 nr_capt = nr;
515 else
516 nr_capt = 0;
517 if ((err =
518 snd_pcm_new(chip->card, vortex_pcm_prettyname[idx], idx, nr,
519 nr_capt, &pcm)) < 0)
520 return err;
521 strcpy(pcm->name, vortex_pcm_name[idx]);
522 chip->pcm[idx] = pcm;
523 // This is an evil hack, but it saves a lot of duplicated code.
524 VORTEX_PCM_TYPE(pcm) = idx;
525 pcm->private_data = chip;
526 /* set operators */
527 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
528 &snd_vortex_playback_ops);
529 if (idx == VORTEX_PCM_ADB)
530 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
531 &snd_vortex_playback_ops);
533 /* pre-allocation of Scatter-Gather buffers */
535 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
536 snd_dma_pci_data(chip->pci_dev),
537 0x10000, 0x10000);
539 if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_SPDIF) {
540 for (i = 0; i < ARRAY_SIZE(snd_vortex_mixer_spdif); i++) {
541 kctl = snd_ctl_new1(&snd_vortex_mixer_spdif[i], chip);
542 if (!kctl)
543 return -ENOMEM;
544 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
545 return err;
548 return 0;