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