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