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