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