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