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