patch-2_6_7-vs1_9_1_12
[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 #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>
29 #include "au88x0.h"
30
31 #define chip_t vortex_t
32 #define VORTEX_PCM_TYPE(x) (x->name[40])
33
34 /* hardware definition */
35 static snd_pcm_hardware_t snd_vortex_playback_hw_adb = {
36         .info =
37             (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
38              SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
39              SNDRV_PCM_INFO_MMAP_VALID),
40         .formats =
41             SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
42             SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
43         .rates = SNDRV_PCM_RATE_CONTINUOUS,
44         .rate_min = 5000,
45         .rate_max = 48000,
46         .channels_min = 1,
47 #ifdef CHIP_AU8830
48         .channels_max = 4,
49 #else
50         .channels_max = 2,
51 #endif
52         .buffer_bytes_max = 0x10000,
53         .period_bytes_min = 0x1,
54         .period_bytes_max = 0x1000,
55         .periods_min = 2,
56         .periods_max = 32,
57 };
58
59 #ifndef CHIP_AU8820
60 static snd_pcm_hardware_t snd_vortex_playback_hw_a3d = {
61         .info =
62             (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
63              SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
64              SNDRV_PCM_INFO_MMAP_VALID),
65         .formats =
66             SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
67             SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
68         .rates = SNDRV_PCM_RATE_CONTINUOUS,
69         .rate_min = 5000,
70         .rate_max = 48000,
71         .channels_min = 1,
72         .channels_max = 1,
73         .buffer_bytes_max = 0x10000,
74         .period_bytes_min = 0x100,
75         .period_bytes_max = 0x1000,
76         .periods_min = 2,
77         .periods_max = 64,
78 };
79 #endif
80 static snd_pcm_hardware_t snd_vortex_playback_hw_spdif = {
81         .info =
82             (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
83              SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
84              SNDRV_PCM_INFO_MMAP_VALID),
85         .formats =
86             SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
87             SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE | SNDRV_PCM_FMTBIT_MU_LAW |
88             SNDRV_PCM_FMTBIT_A_LAW,
89         .rates =
90             SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
91         .rate_min = 32000,
92         .rate_max = 48000,
93         .channels_min = 1,
94         .channels_max = 2,
95         .buffer_bytes_max = 0x10000,
96         .period_bytes_min = 0x100,
97         .period_bytes_max = 0x1000,
98         .periods_min = 2,
99         .periods_max = 64,
100 };
101
102 #ifndef CHIP_AU8810
103 static snd_pcm_hardware_t snd_vortex_playback_hw_wt = {
104         .info = (SNDRV_PCM_INFO_MMAP |
105                  SNDRV_PCM_INFO_INTERLEAVED |
106                  SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
107         .formats = SNDRV_PCM_FMTBIT_S16_LE,
108         .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS, // SNDRV_PCM_RATE_48000,
109         .rate_min = 8000,
110         .rate_max = 48000,
111         .channels_min = 1,
112         .channels_max = 2,
113         .buffer_bytes_max = 0x10000,
114         .period_bytes_min = 0x0400,
115         .period_bytes_max = 0x1000,
116         .periods_min = 2,
117         .periods_max = 64,
118 };
119 #endif
120 /* open callback */
121 static int snd_vortex_pcm_open(snd_pcm_substream_t * substream)
122 {
123         vortex_t *vortex = snd_pcm_substream_chip(substream);
124         snd_pcm_runtime_t *runtime = substream->runtime;
125         int err;
126         
127         /* Force equal size periods */
128         if ((err =
129              snd_pcm_hw_constraint_integer(runtime,
130                                            SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
131                 return err;
132         /* Avoid PAGE_SIZE boundary to fall inside of a period. */
133         if ((err =
134              snd_pcm_hw_constraint_pow2(runtime, 0,
135                                         SNDRV_PCM_HW_PARAM_PERIOD_BYTES)) < 0)
136                 return err;
137
138         if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
139 #ifndef CHIP_AU8820
140                 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_A3D) {
141                         runtime->hw = snd_vortex_playback_hw_a3d;
142                 }
143 #endif
144                 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_SPDIF) {
145                         runtime->hw = snd_vortex_playback_hw_spdif;
146                         switch (vortex->spdif_sr) {
147                         case 32000:
148                                 runtime->hw.rates = SNDRV_PCM_RATE_32000;
149                                 break;
150                         case 44100:
151                                 runtime->hw.rates = SNDRV_PCM_RATE_44100;
152                                 break;
153                         case 48000:
154                                 runtime->hw.rates = SNDRV_PCM_RATE_48000;
155                                 break;
156                         }
157                 }
158                 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB
159                     || VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_I2S)
160                         runtime->hw = snd_vortex_playback_hw_adb;
161                 substream->runtime->private_data = NULL;
162         }
163 #ifndef CHIP_AU8810
164         else {
165                 runtime->hw = snd_vortex_playback_hw_wt;
166                 substream->runtime->private_data = NULL;
167         }
168 #endif
169         return 0;
170 }
171
172 /* close callback */
173 static int snd_vortex_pcm_close(snd_pcm_substream_t * substream)
174 {
175         //vortex_t *chip = snd_pcm_substream_chip(substream);
176         stream_t *stream = (stream_t *) substream->runtime->private_data;
177
178         // the hardware-specific codes will be here
179         if (stream != NULL) {
180                 stream->substream = NULL;
181                 stream->nr_ch = 0;
182         }
183         substream->runtime->private_data = NULL;
184         return 0;
185 }
186
187 /* hw_params callback */
188 static int
189 snd_vortex_pcm_hw_params(snd_pcm_substream_t * substream,
190                          snd_pcm_hw_params_t * hw_params)
191 {
192         chip_t *chip = snd_pcm_substream_chip(substream);
193         stream_t *stream = (stream_t *) (substream->runtime->private_data);
194         snd_pcm_sgbuf_t *sgbuf;
195         int err;
196
197         // Alloc buffer memory.
198         err =
199             snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
200         if (err < 0) {
201                 printk(KERN_ERR "Vortex: pcm page alloc failed!\n");
202                 return err;
203         }
204         //sgbuf = (snd_pcm_sgbuf_t *) substream->runtime->dma_private;
205         sgbuf = snd_pcm_substream_sgbuf(substream);
206         /*
207            printk(KERN_INFO "Vortex: periods %d, period_bytes %d, channels = %d\n", params_periods(hw_params),
208            params_period_bytes(hw_params), params_channels(hw_params));
209          */
210         // Make audio routes and config buffer DMA.
211         if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
212                 int dma, type = VORTEX_PCM_TYPE(substream->pcm);
213                 /* Dealloc any routes. */
214                 if (stream != NULL)
215                         vortex_adb_allocroute(chip, stream->dma,
216                                               stream->nr_ch, stream->dir,
217                                               stream->type);
218                 /* Alloc routes. */
219                 dma =
220                     vortex_adb_allocroute(chip, -1,
221                                           params_channels(hw_params),
222                                           substream->stream, type);
223                 if (dma < 0)
224                         return dma;
225                 stream = substream->runtime->private_data = &chip->dma_adb[dma];
226                 stream->substream = substream;
227                 /* Setup Buffers. */
228                 vortex_adbdma_setbuffers(chip, dma, sgbuf,
229                                          params_period_bytes(hw_params),
230                                          params_periods(hw_params));
231         }
232 #ifndef CHIP_AU8810
233         else {
234                 /* if (stream != NULL)
235                    vortex_wt_allocroute(chip, substream->number, 0); */
236                 vortex_wt_allocroute(chip, substream->number,
237                                      params_channels(hw_params));
238                 stream = substream->runtime->private_data =
239                     &chip->dma_wt[substream->number];
240                 stream->dma = 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         if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_SPDIF) {
513                 snd_kcontrol_t *kcontrol;
514
515                 if ((kcontrol =
516                      snd_ctl_new1(&vortex_spdif_kcontrol, chip)) == NULL)
517                         return -ENOMEM;
518                 if ((err = snd_ctl_add(chip->card, kcontrol)) < 0)
519                         return err;
520         }
521         return 0;
522 }