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