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