upgrade to linux 2.6.10-1.12_FC2
[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 VORTEX_PCM_TYPE(x) (x->name[40])
32
33 /* hardware definition */
34 static snd_pcm_hardware_t snd_vortex_playback_hw_adb = {
35         .info =
36             (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
37              SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
38              SNDRV_PCM_INFO_MMAP_VALID),
39         .formats =
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,
43         .rate_min = 5000,
44         .rate_max = 48000,
45         .channels_min = 1,
46 #ifdef CHIP_AU8830
47         .channels_max = 4,
48 #else
49         .channels_max = 2,
50 #endif
51         .buffer_bytes_max = 0x10000,
52         .period_bytes_min = 0x1,
53         .period_bytes_max = 0x1000,
54         .periods_min = 2,
55         .periods_max = 32,
56 };
57
58 #ifndef CHIP_AU8820
59 static snd_pcm_hardware_t snd_vortex_playback_hw_a3d = {
60         .info =
61             (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
62              SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
63              SNDRV_PCM_INFO_MMAP_VALID),
64         .formats =
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,
68         .rate_min = 5000,
69         .rate_max = 48000,
70         .channels_min = 1,
71         .channels_max = 1,
72         .buffer_bytes_max = 0x10000,
73         .period_bytes_min = 0x100,
74         .period_bytes_max = 0x1000,
75         .periods_min = 2,
76         .periods_max = 64,
77 };
78 #endif
79 static snd_pcm_hardware_t snd_vortex_playback_hw_spdif = {
80         .info =
81             (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_RESUME |
82              SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
83              SNDRV_PCM_INFO_MMAP_VALID),
84         .formats =
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,
88         .rates =
89             SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
90         .rate_min = 32000,
91         .rate_max = 48000,
92         .channels_min = 1,
93         .channels_max = 2,
94         .buffer_bytes_max = 0x10000,
95         .period_bytes_min = 0x100,
96         .period_bytes_max = 0x1000,
97         .periods_min = 2,
98         .periods_max = 64,
99 };
100
101 #ifndef CHIP_AU8810
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,
108         .rate_min = 8000,
109         .rate_max = 48000,
110         .channels_min = 1,
111         .channels_max = 2,
112         .buffer_bytes_max = 0x10000,
113         .period_bytes_min = 0x0400,
114         .period_bytes_max = 0x1000,
115         .periods_min = 2,
116         .periods_max = 64,
117 };
118 #endif
119 /* open callback */
120 static int snd_vortex_pcm_open(snd_pcm_substream_t * substream)
121 {
122         vortex_t *vortex = snd_pcm_substream_chip(substream);
123         snd_pcm_runtime_t *runtime = substream->runtime;
124         int err;
125         
126         /* Force equal size periods */
127         if ((err =
128              snd_pcm_hw_constraint_integer(runtime,
129                                            SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
130                 return err;
131         /* Avoid PAGE_SIZE boundary to fall inside of a period. */
132         if ((err =
133              snd_pcm_hw_constraint_pow2(runtime, 0,
134                                         SNDRV_PCM_HW_PARAM_PERIOD_BYTES)) < 0)
135                 return err;
136
137         if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
138 #ifndef CHIP_AU8820
139                 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_A3D) {
140                         runtime->hw = snd_vortex_playback_hw_a3d;
141                 }
142 #endif
143                 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_SPDIF) {
144                         runtime->hw = snd_vortex_playback_hw_spdif;
145                         switch (vortex->spdif_sr) {
146                         case 32000:
147                                 runtime->hw.rates = SNDRV_PCM_RATE_32000;
148                                 break;
149                         case 44100:
150                                 runtime->hw.rates = SNDRV_PCM_RATE_44100;
151                                 break;
152                         case 48000:
153                                 runtime->hw.rates = SNDRV_PCM_RATE_48000;
154                                 break;
155                         }
156                 }
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;
161         }
162 #ifndef CHIP_AU8810
163         else {
164                 runtime->hw = snd_vortex_playback_hw_wt;
165                 substream->runtime->private_data = NULL;
166         }
167 #endif
168         return 0;
169 }
170
171 /* close callback */
172 static int snd_vortex_pcm_close(snd_pcm_substream_t * substream)
173 {
174         //vortex_t *chip = snd_pcm_substream_chip(substream);
175         stream_t *stream = (stream_t *) substream->runtime->private_data;
176
177         // the hardware-specific codes will be here
178         if (stream != NULL) {
179                 stream->substream = NULL;
180                 stream->nr_ch = 0;
181         }
182         substream->runtime->private_data = NULL;
183         return 0;
184 }
185
186 /* hw_params callback */
187 static int
188 snd_vortex_pcm_hw_params(snd_pcm_substream_t * substream,
189                          snd_pcm_hw_params_t * hw_params)
190 {
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;
194         int err;
195
196         // Alloc buffer memory.
197         err =
198             snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
199         if (err < 0) {
200                 printk(KERN_ERR "Vortex: pcm page alloc failed!\n");
201                 return err;
202         }
203         //sgbuf = (snd_pcm_sgbuf_t *) substream->runtime->dma_private;
204         sgbuf = snd_pcm_substream_sgbuf(substream);
205         /*
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));
208          */
209         spin_lock_irq(&chip->lock);
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         spin_unlock_irq(&chip->lock);
248         return 0;
249 }
250
251 /* hw_free callback */
252 static int snd_vortex_pcm_hw_free(snd_pcm_substream_t * substream)
253 {
254         vortex_t *chip = snd_pcm_substream_chip(substream);
255         stream_t *stream = (stream_t *) (substream->runtime->private_data);
256
257         spin_lock_irq(&chip->lock);
258         // Delete audio routes.
259         if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
260                 if (stream != NULL)
261                         vortex_adb_allocroute(chip, stream->dma,
262                                               stream->nr_ch, stream->dir,
263                                               stream->type);
264         }
265 #ifndef CHIP_AU8810
266         else {
267                 if (stream != NULL)
268                         vortex_wt_allocroute(chip, stream->dma, 0);
269         }
270 #endif
271         substream->runtime->private_data = NULL;
272         spin_unlock_irq(&chip->lock);
273
274         return snd_pcm_lib_free_pages(substream);
275 }
276
277 /* prepare callback */
278 static int snd_vortex_pcm_prepare(snd_pcm_substream_t * substream)
279 {
280         vortex_t *chip = snd_pcm_substream_chip(substream);
281         snd_pcm_runtime_t *runtime = substream->runtime;
282         stream_t *stream = (stream_t *) substream->runtime->private_data;
283         int dma = stream->dma, fmt, dir;
284
285         // set up the hardware with the current configuration.
286         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
287                 dir = 1;
288         else
289                 dir = 0;
290         fmt = vortex_alsafmt_aspfmt(runtime->format);
291         spin_lock_irq(&chip->lock);
292         if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
293                 vortex_adbdma_setmode(chip, dma, 1, dir, fmt, 0 /*? */ ,
294                                       0);
295                 vortex_adbdma_setstartbuffer(chip, dma, 0);
296                 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_SPDIF)
297                         vortex_adb_setsrc(chip, dma, runtime->rate, dir);
298         }
299 #ifndef CHIP_AU8810
300         else {
301                 vortex_wtdma_setmode(chip, dma, 1, fmt, 0, 0);
302                 // FIXME: Set rate (i guess using vortex_wt_writereg() somehow).
303                 vortex_wtdma_setstartbuffer(chip, dma, 0);
304         }
305 #endif
306         spin_unlock_irq(&chip->lock);
307         return 0;
308 }
309
310 /* trigger callback */
311 static int snd_vortex_pcm_trigger(snd_pcm_substream_t * substream, int cmd)
312 {
313         vortex_t *chip = snd_pcm_substream_chip(substream);
314         stream_t *stream = (stream_t *) substream->runtime->private_data;
315         int dma = stream->dma;
316
317         spin_lock(&chip->lock);
318         switch (cmd) {
319         case SNDRV_PCM_TRIGGER_START:
320                 // do something to start the PCM engine
321                 //printk(KERN_INFO "vortex: start %d\n", dma);
322                 stream->fifo_enabled = 1;
323                 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
324                         vortex_adbdma_startfifo(chip, dma);
325 #ifndef CHIP_AU8810
326                 else {
327                         printk(KERN_INFO "vortex: wt start %d\n", dma);
328                         vortex_wtdma_startfifo(chip, dma);
329                 }
330 #endif
331                 break;
332         case SNDRV_PCM_TRIGGER_STOP:
333                 // do something to stop the PCM engine
334                 //printk(KERN_INFO "vortex: stop %d\n", dma);
335                 stream->fifo_enabled = 0;
336                 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
337                         vortex_adbdma_pausefifo(chip, dma);
338                 //vortex_adbdma_stopfifo(chip, dma);
339 #ifndef CHIP_AU8810
340                 else {
341                         printk(KERN_INFO "vortex: wt stop %d\n", dma);
342                         vortex_wtdma_stopfifo(chip, dma);
343                 }
344 #endif
345                 break;
346         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
347                 //printk(KERN_INFO "vortex: pause %d\n", dma);
348                 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
349                         vortex_adbdma_pausefifo(chip, dma);
350 #ifndef CHIP_AU8810
351                 else
352                         vortex_wtdma_pausefifo(chip, dma);
353 #endif
354                 break;
355         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
356                 //printk(KERN_INFO "vortex: resume %d\n", dma);
357                 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
358                         vortex_adbdma_resumefifo(chip, dma);
359 #ifndef CHIP_AU8810
360                 else
361                         vortex_wtdma_resumefifo(chip, dma);
362 #endif
363                 break;
364         default:
365                 spin_unlock(&chip->lock);
366                 return -EINVAL;
367         }
368         spin_unlock(&chip->lock);
369         return 0;
370 }
371
372 /* pointer callback */
373 static snd_pcm_uframes_t snd_vortex_pcm_pointer(snd_pcm_substream_t * substream)
374 {
375         vortex_t *chip = snd_pcm_substream_chip(substream);
376         stream_t *stream = (stream_t *) substream->runtime->private_data;
377         int dma = stream->dma;
378         snd_pcm_uframes_t current_ptr = 0;
379
380         spin_lock(&chip->lock);
381         if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
382                 current_ptr = vortex_adbdma_getlinearpos(chip, dma);
383 #ifndef CHIP_AU8810
384         else
385                 current_ptr = vortex_wtdma_getlinearpos(chip, dma);
386 #endif
387         //printk(KERN_INFO "vortex: pointer = 0x%x\n", current_ptr);
388         spin_unlock(&chip->lock);
389         return (bytes_to_frames(substream->runtime, current_ptr));
390 }
391
392 /* Page callback. */
393 /*
394 static struct page *snd_pcm_sgbuf_ops_page(snd_pcm_substream_t *substream, unsigned long offset) {
395         
396         
397 }
398 */
399 /* operators */
400 static snd_pcm_ops_t snd_vortex_playback_ops = {
401         .open = snd_vortex_pcm_open,
402         .close = snd_vortex_pcm_close,
403         .ioctl = snd_pcm_lib_ioctl,
404         .hw_params = snd_vortex_pcm_hw_params,
405         .hw_free = snd_vortex_pcm_hw_free,
406         .prepare = snd_vortex_pcm_prepare,
407         .trigger = snd_vortex_pcm_trigger,
408         .pointer = snd_vortex_pcm_pointer,
409         .page = snd_pcm_sgbuf_ops_page,
410 };
411
412 /*
413 *  definitions of capture are omitted here...
414 */
415
416 static char *vortex_pcm_prettyname[VORTEX_PCM_LAST] = {
417         "AU88x0 ADB",
418         "AU88x0 SPDIF",
419         "AU88x0 A3D",
420         "AU88x0 WT",
421         "AU88x0 I2S",
422 };
423 static char *vortex_pcm_name[VORTEX_PCM_LAST] = {
424         "adb",
425         "spdif",
426         "a3d",
427         "wt",
428         "i2s",
429 };
430
431 /* SPDIF kcontrol */
432 static int
433 snd_vortex_spdif_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
434 {
435         static char *texts[] = { "32000", "44100", "48000" };
436
437         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
438         uinfo->count = 1;
439         uinfo->value.enumerated.items = 3;
440         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
441                 uinfo->value.enumerated.item =
442                     uinfo->value.enumerated.items - 1;
443         strcpy(uinfo->value.enumerated.name,
444                texts[uinfo->value.enumerated.item]);
445         return 0;
446 }
447 static int
448 snd_vortex_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
449 {
450         vortex_t *vortex = snd_kcontrol_chip(kcontrol);
451
452         if (vortex->spdif_sr == 32000)
453                 ucontrol->value.enumerated.item[0] = 0;
454         if (vortex->spdif_sr == 44100)
455                 ucontrol->value.enumerated.item[0] = 1;
456         if (vortex->spdif_sr == 48000)
457                 ucontrol->value.enumerated.item[0] = 2;
458         return 0;
459 }
460 static int
461 snd_vortex_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
462 {
463         vortex_t *vortex = snd_kcontrol_chip(kcontrol);
464         static unsigned int sr[3] = { 32000, 44100, 48000 };
465
466         //printk("vortex: spdif sr = %d\n", ucontrol->value.enumerated.item[0]);
467         vortex->spdif_sr = sr[ucontrol->value.enumerated.item[0] % 3];
468         vortex_spdif_init(vortex,
469                           sr[ucontrol->value.enumerated.item[0] % 3], 1);
470         return 1;
471 }
472 static snd_kcontrol_new_t vortex_spdif_kcontrol __devinitdata = {
473         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
474         .name = "SPDIF SR",
475         .index = 0,
476         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
477         .private_value = 0,
478         .info = snd_vortex_spdif_info,
479         .get = snd_vortex_spdif_get,
480         .put = snd_vortex_spdif_put
481 };
482
483 /* create a pcm device */
484 static int __devinit snd_vortex_new_pcm(vortex_t * chip, int idx, int nr)
485 {
486         snd_pcm_t *pcm;
487         int err, nr_capt;
488
489         if ((chip == 0) || (idx < 0) || (idx > VORTEX_PCM_LAST))
490                 return -ENODEV;
491
492         /* idx indicates which kind of PCM device. ADB, SPDIF, I2S and A3D share the 
493          * same dma engine. WT uses it own separate dma engine whcih cant capture. */
494         if (idx == VORTEX_PCM_ADB)
495                 nr_capt = nr;
496         else
497                 nr_capt = 0;
498         if ((err =
499              snd_pcm_new(chip->card, vortex_pcm_prettyname[idx], idx, nr,
500                          nr_capt, &pcm)) < 0)
501                 return err;
502         strcpy(pcm->name, vortex_pcm_name[idx]);
503         chip->pcm[idx] = pcm;
504         // This is an evil hack, but it saves a lot of duplicated code.
505         VORTEX_PCM_TYPE(pcm) = idx;
506         pcm->private_data = chip;
507         /* set operators */
508         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
509                         &snd_vortex_playback_ops);
510         if (idx == VORTEX_PCM_ADB)
511                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
512                                 &snd_vortex_playback_ops);
513         
514         /* pre-allocation of Scatter-Gather buffers */
515         
516         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
517                                               snd_dma_pci_data(chip->pci_dev),
518                                               0x10000, 0x10000);
519
520         if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_SPDIF) {
521                 snd_kcontrol_t *kcontrol;
522
523                 if ((kcontrol =
524                      snd_ctl_new1(&vortex_spdif_kcontrol, chip)) == NULL)
525                         return -ENOMEM;
526                 if ((err = snd_ctl_add(chip->card, kcontrol)) < 0)
527                         return err;
528         }
529         return 0;
530 }