ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / pci / au88x0 / au88x0_pcm.c
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.
6  *
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.
11  *
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.
15  */
16  
17 /*
18  * Vortex PCM ALSA driver.
19  *
20  * Supports ADB and WT DMA. Unfortunately, WT channels do not run yet.
21  * It remains stuck,and DMA transfers do not happen.
22  *
23  */
24
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>
30 #include "au88x0.h"
31
32 #define chip_t vortex_t
33 #define VORTEX_PCM_TYPE(x) (x->name[40])
34
35 /* hardware definition */
36 static snd_pcm_hardware_t snd_vortex_playback_hw_adb = {
37         .info =
38             (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
39              SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
40              SNDRV_PCM_INFO_MMAP_VALID),
41         .formats =
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,
45         .rate_min = 5000,
46         .rate_max = 48000,
47         .channels_min = 1,
48 #ifdef CHIP_AU8830
49         .channels_max = 4,
50 #else
51         .channels_max = 2,
52 #endif
53         .buffer_bytes_max = 0x10000,
54         .period_bytes_min = 0x1,
55         .period_bytes_max = 0x1000,
56         .periods_min = 2,
57         .periods_max = 32,
58 };
59
60 #ifndef CHIP_AU8820
61 static snd_pcm_hardware_t snd_vortex_playback_hw_a3d = {
62         .info =
63             (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
64              SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
65              SNDRV_PCM_INFO_MMAP_VALID),
66         .formats =
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,
70         .rate_min = 5000,
71         .rate_max = 48000,
72         .channels_min = 1,
73         .channels_max = 1,
74         .buffer_bytes_max = 0x10000,
75         .period_bytes_min = 0x100,
76         .period_bytes_max = 0x1000,
77         .periods_min = 2,
78         .periods_max = 64,
79 };
80 #endif
81 static snd_pcm_hardware_t snd_vortex_playback_hw_spdif = {
82         .info =
83             (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
84              SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
85              SNDRV_PCM_INFO_MMAP_VALID),
86         .formats =
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,
90         .rates =
91             SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
92         .rate_min = 32000,
93         .rate_max = 48000,
94         .channels_min = 1,
95         .channels_max = 2,
96         .buffer_bytes_max = 0x10000,
97         .period_bytes_min = 0x100,
98         .period_bytes_max = 0x1000,
99         .periods_min = 2,
100         .periods_max = 64,
101 };
102
103 #ifndef CHIP_AU8810
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,
110         .rate_min = 8000,
111         .rate_max = 48000,
112         .channels_min = 1,
113         .channels_max = 2,
114         .buffer_bytes_max = 0x10000,
115         .period_bytes_min = 0x0400,
116         .period_bytes_max = 0x1000,
117         .periods_min = 2,
118         .periods_max = 64,
119 };
120 #endif
121 /* open callback */
122 static int snd_vortex_pcm_open(snd_pcm_substream_t * substream)
123 {
124         vortex_t *vortex = snd_pcm_substream_chip(substream);
125         snd_pcm_runtime_t *runtime = substream->runtime;
126         int err;
127
128         /* Force equal size periods */
129         if ((err =
130              snd_pcm_hw_constraint_integer(runtime,
131                                            SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
132                 return err;
133         /* Avoid PAGE_SIZE boundary to fall inside of a period. */
134         if ((err =
135              snd_pcm_hw_constraint_pow2(runtime, 0,
136                                         SNDRV_PCM_HW_PARAM_PERIOD_BYTES)) < 0)
137                 return err;
138
139         if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
140 #ifndef CHIP_AU8820
141                 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_A3D) {
142                         runtime->hw = snd_vortex_playback_hw_a3d;
143                 }
144 #endif
145                 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_SPDIF) {
146                         runtime->hw = snd_vortex_playback_hw_spdif;
147                         switch (vortex->spdif_sr) {
148                         case 32000:
149                                 runtime->hw.rates = SNDRV_PCM_RATE_32000;
150                                 break;
151                         case 44100:
152                                 runtime->hw.rates = SNDRV_PCM_RATE_44100;
153                                 break;
154                         case 48000:
155                                 runtime->hw.rates = SNDRV_PCM_RATE_48000;
156                                 break;
157                         }
158                 }
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;
163         }
164 #ifndef CHIP_AU8810
165         else {
166                 runtime->hw = snd_vortex_playback_hw_wt;
167                 substream->runtime->private_data = NULL;
168         }
169 #endif
170         return 0;
171 }
172
173 /* close callback */
174 static int snd_vortex_pcm_close(snd_pcm_substream_t * substream)
175 {
176         //vortex_t *chip = snd_pcm_substream_chip(substream);
177         stream_t *stream = (stream_t *) substream->runtime->private_data;
178
179         // the hardware-specific codes will be here
180         if (stream != NULL) {
181                 stream->substream = NULL;
182                 stream->nr_ch = 0;
183         }
184         substream->runtime->private_data = NULL;
185         return 0;
186 }
187
188 /* hw_params callback */
189 static int
190 snd_vortex_pcm_hw_params(snd_pcm_substream_t * substream,
191                          snd_pcm_hw_params_t * hw_params)
192 {
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;
196         int err;
197
198         // Alloc buffer memory.
199         err =
200             snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
201         if (err < 0) {
202                 printk(KERN_ERR "Vortex: pcm page alloc failed!\n");
203                 return err;
204         }
205         //sgbuf = (snd_pcm_sgbuf_t *) substream->runtime->dma_private;
206         sgbuf = snd_pcm_substream_sgbuf(substream);
207         /*
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));
210          */
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. */
215                 if (stream != NULL)
216                         vortex_adb_allocroute(chip, stream->dma,
217                                               stream->nr_ch, stream->dir,
218                                               stream->type);
219                 /* Alloc routes. */
220                 dma =
221                     vortex_adb_allocroute(chip, -1,
222                                           params_channels(hw_params),
223                                           substream->stream, type);
224                 if (dma < 0)
225                         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));
232         }
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->substream = substream;
242                 vortex_wtdma_setbuffers(chip, substream->number, sgbuf,
243                                         params_period_bytes(hw_params),
244                                         params_periods(hw_params));
245         }
246 #endif
247         return 0;
248 }
249
250 /* hw_free callback */
251 static int snd_vortex_pcm_hw_free(snd_pcm_substream_t * substream)
252 {
253         chip_t *chip = snd_pcm_substream_chip(substream);
254         stream_t *stream = (stream_t *) (substream->runtime->private_data);
255
256         // Delete audio routes.
257         if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
258                 if (stream != NULL)
259                         vortex_adb_allocroute(chip, stream->dma,
260                                               stream->nr_ch, stream->dir,
261                                               stream->type);
262         }
263 #ifndef CHIP_AU8810
264         else {
265                 if (stream != NULL)
266                         vortex_wt_allocroute(chip, stream->dma, 0);
267         }
268 #endif
269         substream->runtime->private_data = NULL;
270
271         return snd_pcm_lib_free_pages(substream);
272 }
273
274 /* prepare callback */
275 static int snd_vortex_pcm_prepare(snd_pcm_substream_t * substream)
276 {
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;
281
282         // set up the hardware with the current configuration.
283         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
284                 dir = 1;
285         else
286                 dir = 0;
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 /*? */ ,
290                                       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);
294         }
295 #ifndef CHIP_AU8810
296         else {
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);
300         }
301 #endif
302         return 0;
303 }
304
305 /* trigger callback */
306 static int snd_vortex_pcm_trigger(snd_pcm_substream_t * substream, int cmd)
307 {
308         chip_t *chip = snd_pcm_substream_chip(substream);
309         stream_t *stream = (stream_t *) substream->runtime->private_data;
310         int dma = stream->dma;
311
312         switch (cmd) {
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);
319 #ifndef CHIP_AU8810
320                 else {
321                         printk(KERN_INFO "vortex: wt start %d\n", dma);
322                         vortex_wtdma_startfifo(chip, dma);
323                 }
324 #endif
325                 break;
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);
333 #ifndef CHIP_AU8810
334                 else {
335                         printk(KERN_INFO "vortex: wt stop %d\n", dma);
336                         vortex_wtdma_stopfifo(chip, dma);
337                 }
338 #endif
339                 break;
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);
344 #ifndef CHIP_AU8810
345                 else
346                         vortex_wtdma_pausefifo(chip, dma);
347 #endif
348                 break;
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);
353 #ifndef CHIP_AU8810
354                 else
355                         vortex_wtdma_resumefifo(chip, dma);
356 #endif
357                 break;
358         default:
359                 return -EINVAL;
360         }
361         return 0;
362 }
363
364 /* pointer callback */
365 static snd_pcm_uframes_t snd_vortex_pcm_pointer(snd_pcm_substream_t * substream)
366 {
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;
371
372         spin_lock(&chip->lock);
373         if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
374                 current_ptr = vortex_adbdma_getlinearpos(chip, dma);
375 #ifndef CHIP_AU8810
376         else
377                 current_ptr = vortex_wtdma_getlinearpos(chip, dma);
378 #endif
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));
382 }
383
384 /* Page callback. */
385 /*
386 static struct page *snd_pcm_sgbuf_ops_page(snd_pcm_substream_t *substream, unsigned long offset) {
387         
388         
389 }
390 */
391 /* operators */
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,
402 };
403
404 /*
405 *  definitions of capture are omitted here...
406 */
407
408 static char *vortex_pcm_prettyname[VORTEX_PCM_LAST] = {
409         "AU88x0 ADB",
410         "AU88x0 SPDIF",
411         "AU88x0 A3D",
412         "AU88x0 WT",
413         "AU88x0 I2S",
414 };
415 static char *vortex_pcm_name[VORTEX_PCM_LAST] = {
416         "adb",
417         "spdif",
418         "a3d",
419         "wt",
420         "i2s",
421 };
422
423 /* SPDIF kcontrol */
424 static int
425 snd_vortex_spdif_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
426 {
427         static char *texts[] = { "32000", "44100", "48000" };
428
429         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
430         uinfo->count = 1;
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]);
437         return 0;
438 }
439 static int
440 snd_vortex_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
441 {
442         vortex_t *vortex = snd_kcontrol_chip(kcontrol);
443
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;
450         return 0;
451 }
452 static int
453 snd_vortex_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
454 {
455         vortex_t *vortex = snd_kcontrol_chip(kcontrol);
456         static unsigned int sr[3] = { 32000, 44100, 48000 };
457
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);
462         return 1;
463 }
464 static snd_kcontrol_new_t vortex_spdif_kcontrol __devinitdata = {
465         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
466         .name = "SPDIF SR",
467         .index = 0,
468         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
469         .private_value = 0,
470         .info = snd_vortex_spdif_info,
471         .get = snd_vortex_spdif_get,
472         .put = snd_vortex_spdif_put
473 };
474
475 /* create a pcm device */
476 static int __devinit snd_vortex_new_pcm(vortex_t * chip, int idx, int nr)
477 {
478         snd_pcm_t *pcm;
479         int err, nr_capt;
480
481         if ((chip == 0) || (idx < 0) || (idx > VORTEX_PCM_LAST))
482                 return -ENODEV;
483
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)
487                 nr_capt = nr;
488         else
489                 nr_capt = 0;
490         if ((err =
491              snd_pcm_new(chip->card, vortex_pcm_prettyname[idx], idx, nr,
492                          nr_capt, &pcm)) < 0)
493                 return err;
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;
499         /* set operators */
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);
505
506         /* pre-allocation of Scatter-Gather buffers */
507         
508         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
509                                                                                   snd_dma_pci_data(chip->pci_dev),
510                                                                                   0x10000, 0x10000);
511         
512         // The above should be used, as soon as ALSA gets updated.
513         /*
514         snd_pcm_lib_preallocate_sg_pages_for_all(chip->pci_dev, pcm,
515                                                  0x10000, 0x10000);
516         */
517         if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_SPDIF) {
518                 snd_kcontrol_t *kcontrol;
519
520                 if ((kcontrol =
521                      snd_ctl_new1(&vortex_spdif_kcontrol, chip)) == NULL)
522                         return -ENOMEM;
523                 if ((err = snd_ctl_add(chip->card, kcontrol)) < 0)
524                         return err;
525         }
526         return 0;
527 }