ALSA: aw2: do not grab every saa7146 based device
[linux-2.6] / sound / pci / aw2 / aw2-alsa.c
1 /*****************************************************************************
2  *
3  * Copyright (C) 2008 Cedric Bregardis <cedric.bregardis@free.fr> and
4  * Jean-Christian Hassler <jhassler@free.fr>
5  *
6  * This file is part of the Audiowerk2 ALSA driver
7  *
8  * The Audiowerk2 ALSA driver is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2.
11  *
12  * The Audiowerk2 ALSA driver is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with the Audiowerk2 ALSA driver; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20  * USA.
21  *
22  *****************************************************************************/
23 #include <linux/init.h>
24 #include <linux/pci.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <asm/io.h>
30 #include <sound/core.h>
31 #include <sound/initval.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/control.h>
35
36 #include "saa7146.h"
37 #include "aw2-saa7146.h"
38
39 MODULE_AUTHOR("Cedric Bregardis <cedric.bregardis@free.fr>, "
40               "Jean-Christian Hassler <jhassler@free.fr>");
41 MODULE_DESCRIPTION("Emagic Audiowerk 2 sound driver");
42 MODULE_LICENSE("GPL");
43
44 /*********************************
45  * DEFINES
46  ********************************/
47 #define PCI_VENDOR_ID_SAA7146             0x1131
48 #define PCI_DEVICE_ID_SAA7146             0x7146
49
50 #define CTL_ROUTE_ANALOG 0
51 #define CTL_ROUTE_DIGITAL 1
52
53 /*********************************
54  * TYPEDEFS
55  ********************************/
56   /* hardware definition */
57 static struct snd_pcm_hardware snd_aw2_playback_hw = {
58         .info = (SNDRV_PCM_INFO_MMAP |
59                  SNDRV_PCM_INFO_INTERLEAVED |
60                  SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
61         .formats = SNDRV_PCM_FMTBIT_S16_LE,
62         .rates = SNDRV_PCM_RATE_44100,
63         .rate_min = 44100,
64         .rate_max = 44100,
65         .channels_min = 2,
66         .channels_max = 4,
67         .buffer_bytes_max = 32768,
68         .period_bytes_min = 4096,
69         .period_bytes_max = 32768,
70         .periods_min = 1,
71         .periods_max = 1024,
72 };
73
74 static struct snd_pcm_hardware snd_aw2_capture_hw = {
75         .info = (SNDRV_PCM_INFO_MMAP |
76                  SNDRV_PCM_INFO_INTERLEAVED |
77                  SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
78         .formats = SNDRV_PCM_FMTBIT_S16_LE,
79         .rates = SNDRV_PCM_RATE_44100,
80         .rate_min = 44100,
81         .rate_max = 44100,
82         .channels_min = 2,
83         .channels_max = 2,
84         .buffer_bytes_max = 32768,
85         .period_bytes_min = 4096,
86         .period_bytes_max = 32768,
87         .periods_min = 1,
88         .periods_max = 1024,
89 };
90
91 struct aw2_pcm_device {
92         struct snd_pcm *pcm;
93         unsigned int stream_number;
94         struct aw2 *chip;
95 };
96
97 struct aw2 {
98         struct snd_aw2_saa7146 saa7146;
99
100         struct pci_dev *pci;
101         int irq;
102         spinlock_t reg_lock;
103         struct mutex mtx;
104
105         unsigned long iobase_phys;
106         void __iomem *iobase_virt;
107
108         struct snd_card *card;
109
110         struct aw2_pcm_device device_playback[NB_STREAM_PLAYBACK];
111         struct aw2_pcm_device device_capture[NB_STREAM_CAPTURE];
112 };
113
114 /*********************************
115  * FUNCTION DECLARATIONS
116  ********************************/
117 static int __init alsa_card_aw2_init(void);
118 static void __exit alsa_card_aw2_exit(void);
119 static int snd_aw2_dev_free(struct snd_device *device);
120 static int __devinit snd_aw2_create(struct snd_card *card,
121                                     struct pci_dev *pci, struct aw2 **rchip);
122 static int __devinit snd_aw2_probe(struct pci_dev *pci,
123                                    const struct pci_device_id *pci_id);
124 static void __devexit snd_aw2_remove(struct pci_dev *pci);
125 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream);
126 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream);
127 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream);
128 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream);
129 static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
130                                  struct snd_pcm_hw_params *hw_params);
131 static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream);
132 static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream);
133 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream);
134 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
135                                         int cmd);
136 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
137                                        int cmd);
138 static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
139                                                       *substream);
140 static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
141                                                      *substream);
142 static int __devinit snd_aw2_new_pcm(struct aw2 *chip);
143
144 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
145                                                struct snd_ctl_elem_info *uinfo);
146 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
147                                               struct snd_ctl_elem_value
148                                               *ucontrol);
149 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
150                                               struct snd_ctl_elem_value
151                                               *ucontrol);
152
153 /*********************************
154  * VARIABLES
155  ********************************/
156 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
157 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
158 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
159
160 module_param_array(index, int, NULL, 0444);
161 MODULE_PARM_DESC(index, "Index value for Audiowerk2 soundcard.");
162 module_param_array(id, charp, NULL, 0444);
163 MODULE_PARM_DESC(id, "ID string for the Audiowerk2 soundcard.");
164 module_param_array(enable, bool, NULL, 0444);
165 MODULE_PARM_DESC(enable, "Enable Audiowerk2 soundcard.");
166
167 static struct pci_device_id snd_aw2_ids[] = {
168         {PCI_VENDOR_ID_SAA7146, PCI_DEVICE_ID_SAA7146, 0, 0,
169          0, 0, 0},
170         {0}
171 };
172
173 MODULE_DEVICE_TABLE(pci, snd_aw2_ids);
174
175 /* pci_driver definition */
176 static struct pci_driver driver = {
177         .name = "Emagic Audiowerk 2",
178         .id_table = snd_aw2_ids,
179         .probe = snd_aw2_probe,
180         .remove = __devexit_p(snd_aw2_remove),
181 };
182
183 /* operators for playback PCM alsa interface */
184 static struct snd_pcm_ops snd_aw2_playback_ops = {
185         .open = snd_aw2_pcm_playback_open,
186         .close = snd_aw2_pcm_playback_close,
187         .ioctl = snd_pcm_lib_ioctl,
188         .hw_params = snd_aw2_pcm_hw_params,
189         .hw_free = snd_aw2_pcm_hw_free,
190         .prepare = snd_aw2_pcm_prepare_playback,
191         .trigger = snd_aw2_pcm_trigger_playback,
192         .pointer = snd_aw2_pcm_pointer_playback,
193 };
194
195 /* operators for capture PCM alsa interface */
196 static struct snd_pcm_ops snd_aw2_capture_ops = {
197         .open = snd_aw2_pcm_capture_open,
198         .close = snd_aw2_pcm_capture_close,
199         .ioctl = snd_pcm_lib_ioctl,
200         .hw_params = snd_aw2_pcm_hw_params,
201         .hw_free = snd_aw2_pcm_hw_free,
202         .prepare = snd_aw2_pcm_prepare_capture,
203         .trigger = snd_aw2_pcm_trigger_capture,
204         .pointer = snd_aw2_pcm_pointer_capture,
205 };
206
207 static struct snd_kcontrol_new aw2_control __devinitdata = {
208         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
209         .name = "PCM Capture Route",
210         .index = 0,
211         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
212         .private_value = 0xffff,
213         .info = snd_aw2_control_switch_capture_info,
214         .get = snd_aw2_control_switch_capture_get,
215         .put = snd_aw2_control_switch_capture_put
216 };
217
218 /*********************************
219  * FUNCTION IMPLEMENTATIONS
220  ********************************/
221
222 /* initialization of the module */
223 static int __init alsa_card_aw2_init(void)
224 {
225         snd_printdd(KERN_DEBUG "aw2: Load aw2 module\n");
226         return pci_register_driver(&driver);
227 }
228
229 /* clean up the module */
230 static void __exit alsa_card_aw2_exit(void)
231 {
232         snd_printdd(KERN_DEBUG "aw2: Unload aw2 module\n");
233         pci_unregister_driver(&driver);
234 }
235
236 module_init(alsa_card_aw2_init);
237 module_exit(alsa_card_aw2_exit);
238
239 /* component-destructor */
240 static int snd_aw2_dev_free(struct snd_device *device)
241 {
242         struct aw2 *chip = device->device_data;
243
244         /* Free hardware */
245         snd_aw2_saa7146_free(&chip->saa7146);
246
247         /* release the irq */
248         if (chip->irq >= 0)
249                 free_irq(chip->irq, (void *)chip);
250         /* release the i/o ports & memory */
251         if (chip->iobase_virt)
252                 iounmap(chip->iobase_virt);
253
254         pci_release_regions(chip->pci);
255         /* disable the PCI entry */
256         pci_disable_device(chip->pci);
257         /* release the data */
258         kfree(chip);
259
260         return 0;
261 }
262
263 /* chip-specific constructor */
264 static int __devinit snd_aw2_create(struct snd_card *card,
265                                     struct pci_dev *pci, struct aw2 **rchip)
266 {
267         struct aw2 *chip;
268         int err;
269         static struct snd_device_ops ops = {
270                 .dev_free = snd_aw2_dev_free,
271         };
272
273         *rchip = NULL;
274
275         /* initialize the PCI entry */
276         err = pci_enable_device(pci);
277         if (err < 0)
278                 return err;
279         pci_set_master(pci);
280
281         /* check PCI availability (32bit DMA) */
282         if ((pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0) ||
283             (pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0)) {
284                 printk(KERN_ERR "aw2: Impossible to set 32bit mask DMA\n");
285                 pci_disable_device(pci);
286                 return -ENXIO;
287         }
288         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
289         if (chip == NULL) {
290                 pci_disable_device(pci);
291                 return -ENOMEM;
292         }
293
294         /* initialize the stuff */
295         chip->card = card;
296         chip->pci = pci;
297         chip->irq = -1;
298
299         /* (1) PCI resource allocation */
300         err = pci_request_regions(pci, "Audiowerk2");
301         if (err < 0) {
302                 pci_disable_device(pci);
303                 kfree(chip);
304                 return err;
305         }
306         chip->iobase_phys = pci_resource_start(pci, 0);
307         chip->iobase_virt =
308                 ioremap_nocache(chip->iobase_phys,
309                                 pci_resource_len(pci, 0));
310
311         if (chip->iobase_virt == NULL) {
312                 printk(KERN_ERR "aw2: unable to remap memory region");
313                 pci_release_regions(pci);
314                 pci_disable_device(pci);
315                 kfree(chip);
316                 return -ENOMEM;
317         }
318
319         /* (2) initialization of the chip hardware */
320         snd_aw2_saa7146_setup(&chip->saa7146, chip->iobase_virt);
321
322         if (request_irq(pci->irq, snd_aw2_saa7146_interrupt,
323                         IRQF_SHARED, "Audiowerk2", chip)) {
324                 printk(KERN_ERR "aw2: Cannot grab irq %d\n", pci->irq);
325
326                 iounmap(chip->iobase_virt);
327                 pci_release_regions(chip->pci);
328                 pci_disable_device(chip->pci);
329                 kfree(chip);
330                 return -EBUSY;
331         }
332         chip->irq = pci->irq;
333
334         err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
335         if (err < 0) {
336                 free_irq(chip->irq, (void *)chip);
337                 iounmap(chip->iobase_virt);
338                 pci_release_regions(chip->pci);
339                 pci_disable_device(chip->pci);
340                 kfree(chip);
341                 return err;
342         }
343
344         snd_card_set_dev(card, &pci->dev);
345         *rchip = chip;
346
347         printk(KERN_INFO
348                "Audiowerk 2 sound card (saa7146 chipset) detected and "
349                "managed\n");
350         return 0;
351 }
352
353 /* constructor */
354 static int __devinit snd_aw2_probe(struct pci_dev *pci,
355                                    const struct pci_device_id *pci_id)
356 {
357         static int dev;
358         struct snd_card *card;
359         struct aw2 *chip;
360         int err;
361
362         /* (1) Continue if device is not enabled, else inc dev */
363         if (dev >= SNDRV_CARDS)
364                 return -ENODEV;
365         if (!enable[dev]) {
366                 dev++;
367                 return -ENOENT;
368         }
369
370         /* (2) Create card instance */
371         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
372         if (card == NULL)
373                 return -ENOMEM;
374
375         /* (3) Create main component */
376         err = snd_aw2_create(card, pci, &chip);
377         if (err < 0) {
378                 snd_card_free(card);
379                 return err;
380         }
381
382         /* initialize mutex */
383         mutex_init(&chip->mtx);
384         /* init spinlock */
385         spin_lock_init(&chip->reg_lock);
386         /* (4) Define driver ID and name string */
387         strcpy(card->driver, "aw2");
388         strcpy(card->shortname, "Audiowerk2");
389
390         sprintf(card->longname, "%s with SAA7146 irq %i",
391                 card->shortname, chip->irq);
392
393         /* (5) Create other components */
394         snd_aw2_new_pcm(chip);
395
396         /* (6) Register card instance */
397         err = snd_card_register(card);
398         if (err < 0) {
399                 snd_card_free(card);
400                 return err;
401         }
402
403         /* (7) Set PCI driver data */
404         pci_set_drvdata(pci, card);
405
406         dev++;
407         return 0;
408 }
409
410 /* destructor */
411 static void __devexit snd_aw2_remove(struct pci_dev *pci)
412 {
413         snd_card_free(pci_get_drvdata(pci));
414         pci_set_drvdata(pci, NULL);
415 }
416
417 /* open callback */
418 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream)
419 {
420         struct snd_pcm_runtime *runtime = substream->runtime;
421
422         snd_printdd(KERN_DEBUG "aw2: Playback_open \n");
423         runtime->hw = snd_aw2_playback_hw;
424         return 0;
425 }
426
427 /* close callback */
428 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream)
429 {
430         return 0;
431
432 }
433
434 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream)
435 {
436         struct snd_pcm_runtime *runtime = substream->runtime;
437
438         snd_printdd(KERN_DEBUG "aw2: Capture_open \n");
439         runtime->hw = snd_aw2_capture_hw;
440         return 0;
441 }
442
443 /* close callback */
444 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream)
445 {
446         /* TODO: something to do ? */
447         return 0;
448 }
449
450  /* hw_params callback */
451 static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
452                                  struct snd_pcm_hw_params *hw_params)
453 {
454         return snd_pcm_lib_malloc_pages(substream,
455                                         params_buffer_bytes(hw_params));
456 }
457
458 /* hw_free callback */
459 static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream)
460 {
461         return snd_pcm_lib_free_pages(substream);
462 }
463
464 /* prepare callback for playback */
465 static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream)
466 {
467         struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
468         struct aw2 *chip = pcm_device->chip;
469         struct snd_pcm_runtime *runtime = substream->runtime;
470         unsigned long period_size, buffer_size;
471
472         mutex_lock(&chip->mtx);
473
474         period_size = snd_pcm_lib_period_bytes(substream);
475         buffer_size = snd_pcm_lib_buffer_bytes(substream);
476
477         snd_aw2_saa7146_pcm_init_playback(&chip->saa7146,
478                                           pcm_device->stream_number,
479                                           runtime->dma_addr, period_size,
480                                           buffer_size);
481
482         /* Define Interrupt callback */
483         snd_aw2_saa7146_define_it_playback_callback(pcm_device->stream_number,
484                                                     (snd_aw2_saa7146_it_cb)
485                                                     snd_pcm_period_elapsed,
486                                                     (void *)substream);
487
488         mutex_unlock(&chip->mtx);
489
490         return 0;
491 }
492
493 /* prepare callback for capture */
494 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream)
495 {
496         struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
497         struct aw2 *chip = pcm_device->chip;
498         struct snd_pcm_runtime *runtime = substream->runtime;
499         unsigned long period_size, buffer_size;
500
501         mutex_lock(&chip->mtx);
502
503         period_size = snd_pcm_lib_period_bytes(substream);
504         buffer_size = snd_pcm_lib_buffer_bytes(substream);
505
506         snd_aw2_saa7146_pcm_init_capture(&chip->saa7146,
507                                          pcm_device->stream_number,
508                                          runtime->dma_addr, period_size,
509                                          buffer_size);
510
511         /* Define Interrupt callback */
512         snd_aw2_saa7146_define_it_capture_callback(pcm_device->stream_number,
513                                                    (snd_aw2_saa7146_it_cb)
514                                                    snd_pcm_period_elapsed,
515                                                    (void *)substream);
516
517         mutex_unlock(&chip->mtx);
518
519         return 0;
520 }
521
522 /* playback trigger callback */
523 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
524                                         int cmd)
525 {
526         int status = 0;
527         struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
528         struct aw2 *chip = pcm_device->chip;
529         spin_lock(&chip->reg_lock);
530         switch (cmd) {
531         case SNDRV_PCM_TRIGGER_START:
532                 snd_aw2_saa7146_pcm_trigger_start_playback(&chip->saa7146,
533                                                            pcm_device->
534                                                            stream_number);
535                 break;
536         case SNDRV_PCM_TRIGGER_STOP:
537                 snd_aw2_saa7146_pcm_trigger_stop_playback(&chip->saa7146,
538                                                           pcm_device->
539                                                           stream_number);
540                 break;
541         default:
542                 status = -EINVAL;
543         }
544         spin_unlock(&chip->reg_lock);
545         return status;
546 }
547
548 /* capture trigger callback */
549 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
550                                        int cmd)
551 {
552         int status = 0;
553         struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
554         struct aw2 *chip = pcm_device->chip;
555         spin_lock(&chip->reg_lock);
556         switch (cmd) {
557         case SNDRV_PCM_TRIGGER_START:
558                 snd_aw2_saa7146_pcm_trigger_start_capture(&chip->saa7146,
559                                                           pcm_device->
560                                                           stream_number);
561                 break;
562         case SNDRV_PCM_TRIGGER_STOP:
563                 snd_aw2_saa7146_pcm_trigger_stop_capture(&chip->saa7146,
564                                                          pcm_device->
565                                                          stream_number);
566                 break;
567         default:
568                 status = -EINVAL;
569         }
570         spin_unlock(&chip->reg_lock);
571         return status;
572 }
573
574 /* playback pointer callback */
575 static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
576                                                       *substream)
577 {
578         struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
579         struct aw2 *chip = pcm_device->chip;
580         unsigned int current_ptr;
581
582         /* get the current hardware pointer */
583         struct snd_pcm_runtime *runtime = substream->runtime;
584         current_ptr =
585                 snd_aw2_saa7146_get_hw_ptr_playback(&chip->saa7146,
586                                                     pcm_device->stream_number,
587                                                     runtime->dma_area,
588                                                     runtime->buffer_size);
589
590         return bytes_to_frames(substream->runtime, current_ptr);
591 }
592
593 /* capture pointer callback */
594 static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
595                                                      *substream)
596 {
597         struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
598         struct aw2 *chip = pcm_device->chip;
599         unsigned int current_ptr;
600
601         /* get the current hardware pointer */
602         struct snd_pcm_runtime *runtime = substream->runtime;
603         current_ptr =
604                 snd_aw2_saa7146_get_hw_ptr_capture(&chip->saa7146,
605                                                    pcm_device->stream_number,
606                                                    runtime->dma_area,
607                                                    runtime->buffer_size);
608
609         return bytes_to_frames(substream->runtime, current_ptr);
610 }
611
612 /* create a pcm device */
613 static int __devinit snd_aw2_new_pcm(struct aw2 *chip)
614 {
615         struct snd_pcm *pcm_playback_ana;
616         struct snd_pcm *pcm_playback_num;
617         struct snd_pcm *pcm_capture;
618         struct aw2_pcm_device *pcm_device;
619         int err = 0;
620
621         /* Create new Alsa PCM device */
622
623         err = snd_pcm_new(chip->card, "Audiowerk2 analog playback", 0, 1, 0,
624                           &pcm_playback_ana);
625         if (err < 0) {
626                 printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
627                 return err;
628         }
629
630         /* Creation ok */
631         pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_ANA];
632
633         /* Set PCM device name */
634         strcpy(pcm_playback_ana->name, "Analog playback");
635         /* Associate private data to PCM device */
636         pcm_playback_ana->private_data = pcm_device;
637         /* set operators of PCM device */
638         snd_pcm_set_ops(pcm_playback_ana, SNDRV_PCM_STREAM_PLAYBACK,
639                         &snd_aw2_playback_ops);
640         /* store PCM device */
641         pcm_device->pcm = pcm_playback_ana;
642         /* give base chip pointer to our internal pcm device
643            structure */
644         pcm_device->chip = chip;
645         /* Give stream number to PCM device */
646         pcm_device->stream_number = NUM_STREAM_PLAYBACK_ANA;
647
648         /* pre-allocation of buffers */
649         /* Preallocate continuous pages. */
650         err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_ana,
651                                                     SNDRV_DMA_TYPE_DEV,
652                                                     snd_dma_pci_data
653                                                     (chip->pci),
654                                                     64 * 1024, 64 * 1024);
655         if (err)
656                 printk(KERN_ERR "aw2: snd_pcm_lib_preallocate_pages_for_all "
657                        "error (0x%X)\n", err);
658
659         err = snd_pcm_new(chip->card, "Audiowerk2 digital playback", 1, 1, 0,
660                           &pcm_playback_num);
661
662         if (err < 0) {
663                 printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
664                 return err;
665         }
666         /* Creation ok */
667         pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_DIG];
668
669         /* Set PCM device name */
670         strcpy(pcm_playback_num->name, "Digital playback");
671         /* Associate private data to PCM device */
672         pcm_playback_num->private_data = pcm_device;
673         /* set operators of PCM device */
674         snd_pcm_set_ops(pcm_playback_num, SNDRV_PCM_STREAM_PLAYBACK,
675                         &snd_aw2_playback_ops);
676         /* store PCM device */
677         pcm_device->pcm = pcm_playback_num;
678         /* give base chip pointer to our internal pcm device
679            structure */
680         pcm_device->chip = chip;
681         /* Give stream number to PCM device */
682         pcm_device->stream_number = NUM_STREAM_PLAYBACK_DIG;
683
684         /* pre-allocation of buffers */
685         /* Preallocate continuous pages. */
686         err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_num,
687                                                     SNDRV_DMA_TYPE_DEV,
688                                                     snd_dma_pci_data
689                                                     (chip->pci),
690                                                     64 * 1024, 64 * 1024);
691         if (err)
692                 printk(KERN_ERR
693                        "aw2: snd_pcm_lib_preallocate_pages_for_all error "
694                        "(0x%X)\n", err);
695
696
697
698         err = snd_pcm_new(chip->card, "Audiowerk2 capture", 2, 0, 1,
699                           &pcm_capture);
700
701         if (err < 0) {
702                 printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
703                 return err;
704         }
705
706         /* Creation ok */
707         pcm_device = &chip->device_capture[NUM_STREAM_CAPTURE_ANA];
708
709         /* Set PCM device name */
710         strcpy(pcm_capture->name, "Capture");
711         /* Associate private data to PCM device */
712         pcm_capture->private_data = pcm_device;
713         /* set operators of PCM device */
714         snd_pcm_set_ops(pcm_capture, SNDRV_PCM_STREAM_CAPTURE,
715                         &snd_aw2_capture_ops);
716         /* store PCM device */
717         pcm_device->pcm = pcm_capture;
718         /* give base chip pointer to our internal pcm device
719            structure */
720         pcm_device->chip = chip;
721         /* Give stream number to PCM device */
722         pcm_device->stream_number = NUM_STREAM_CAPTURE_ANA;
723
724         /* pre-allocation of buffers */
725         /* Preallocate continuous pages. */
726         err = snd_pcm_lib_preallocate_pages_for_all(pcm_capture,
727                                                     SNDRV_DMA_TYPE_DEV,
728                                                     snd_dma_pci_data
729                                                     (chip->pci),
730                                                     64 * 1024, 64 * 1024);
731         if (err)
732                 printk(KERN_ERR
733                        "aw2: snd_pcm_lib_preallocate_pages_for_all error "
734                        "(0x%X)\n", err);
735
736
737         /* Create control */
738         err = snd_ctl_add(chip->card, snd_ctl_new1(&aw2_control, chip));
739         if (err < 0) {
740                 printk(KERN_ERR "aw2: snd_ctl_add error (0x%X)\n", err);
741                 return err;
742         }
743
744         return 0;
745 }
746
747 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
748                                                struct snd_ctl_elem_info *uinfo)
749 {
750         static char *texts[2] = {
751                 "Analog", "Digital"
752         };
753         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
754         uinfo->count = 1;
755         uinfo->value.enumerated.items = 2;
756         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) {
757                 uinfo->value.enumerated.item =
758                     uinfo->value.enumerated.items - 1;
759         }
760         strcpy(uinfo->value.enumerated.name,
761                texts[uinfo->value.enumerated.item]);
762         return 0;
763 }
764
765 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
766                                               struct snd_ctl_elem_value
767                                               *ucontrol)
768 {
769         struct aw2 *chip = snd_kcontrol_chip(kcontrol);
770         if (snd_aw2_saa7146_is_using_digital_input(&chip->saa7146))
771                 ucontrol->value.enumerated.item[0] = CTL_ROUTE_DIGITAL;
772         else
773                 ucontrol->value.enumerated.item[0] = CTL_ROUTE_ANALOG;
774         return 0;
775 }
776
777 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
778                                               struct snd_ctl_elem_value
779                                               *ucontrol)
780 {
781         struct aw2 *chip = snd_kcontrol_chip(kcontrol);
782         int changed = 0;
783         int is_disgital =
784             snd_aw2_saa7146_is_using_digital_input(&chip->saa7146);
785
786         if (((ucontrol->value.integer.value[0] == CTL_ROUTE_DIGITAL)
787              && !is_disgital)
788             || ((ucontrol->value.integer.value[0] == CTL_ROUTE_ANALOG)
789                 && is_disgital)) {
790                 snd_aw2_saa7146_use_digital_input(&chip->saa7146, !is_disgital);
791                 changed = 1;
792         }
793         return changed;
794 }