blob: 3878cf5de9a48449d9a2a2e42baa1a07ea4ea217 [file] [log] [blame]
Cedric Bregardis98f2a972008-02-20 12:05:13 +01001/*****************************************************************************
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>
Andrew Morton91e24fa2008-03-20 12:04:46 +010025#include <linux/dma-mapping.h>
Cedric Bregardis98f2a972008-02-20 12:05:13 +010026#include <linux/slab.h>
27#include <linux/interrupt.h>
28#include <linux/delay.h>
H Hartley Sweeten34329fa2010-05-21 17:35:03 -050029#include <linux/io.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040030#include <linux/module.h>
Cedric Bregardis98f2a972008-02-20 12:05:13 +010031#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
Cedric Bregardis98f2a972008-02-20 12:05:13 +010040MODULE_AUTHOR("Cedric Bregardis <cedric.bregardis@free.fr>, "
41 "Jean-Christian Hassler <jhassler@free.fr>");
42MODULE_DESCRIPTION("Emagic Audiowerk 2 sound driver");
43MODULE_LICENSE("GPL");
44
45/*********************************
46 * DEFINES
47 ********************************/
Cedric Bregardis98f2a972008-02-20 12:05:13 +010048#define CTL_ROUTE_ANALOG 0
49#define CTL_ROUTE_DIGITAL 1
50
51/*********************************
52 * TYPEDEFS
53 ********************************/
54 /* hardware definition */
55static 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
72static 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
89struct aw2_pcm_device {
90 struct snd_pcm *pcm;
91 unsigned int stream_number;
92 struct aw2 *chip;
93};
94
95struct 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 ********************************/
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100115static int snd_aw2_dev_free(struct snd_device *device);
Bill Pembertone23e7a12012-12-06 12:35:10 -0500116static int snd_aw2_create(struct snd_card *card,
117 struct pci_dev *pci, struct aw2 **rchip);
118static int snd_aw2_probe(struct pci_dev *pci,
119 const struct pci_device_id *pci_id);
120static void snd_aw2_remove(struct pci_dev *pci);
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100121static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream);
122static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream);
123static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream);
124static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream);
125static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
126 struct snd_pcm_hw_params *hw_params);
127static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream);
128static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream);
129static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream);
130static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
131 int cmd);
132static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
133 int cmd);
134static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
135 *substream);
136static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
137 *substream);
Bill Pembertone23e7a12012-12-06 12:35:10 -0500138static int snd_aw2_new_pcm(struct aw2 *chip);
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100139
140static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
141 struct snd_ctl_elem_info *uinfo);
142static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
143 struct snd_ctl_elem_value
144 *ucontrol);
145static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
146 struct snd_ctl_elem_value
147 *ucontrol);
148
149/*********************************
150 * VARIABLES
151 ********************************/
152static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
153static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
Rusty Russella67ff6a2011-12-15 13:49:36 +1030154static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100155
Takashi Iwai34b67572008-02-20 12:12:58 +0100156module_param_array(index, int, NULL, 0444);
157MODULE_PARM_DESC(index, "Index value for Audiowerk2 soundcard.");
158module_param_array(id, charp, NULL, 0444);
159MODULE_PARM_DESC(id, "ID string for the Audiowerk2 soundcard.");
160module_param_array(enable, bool, NULL, 0444);
161MODULE_PARM_DESC(enable, "Enable Audiowerk2 soundcard.");
162
Benoit Taine9baa3c32014-08-08 15:56:03 +0200163static const struct pci_device_id snd_aw2_ids[] = {
H Hartley Sweeten34329fa2010-05-21 17:35:03 -0500164 {PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_PHILIPS_SAA7146, 0, 0,
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100165 0, 0, 0},
166 {0}
167};
168
169MODULE_DEVICE_TABLE(pci, snd_aw2_ids);
170
171/* pci_driver definition */
Takashi Iwaie9f66d92012-04-24 12:25:00 +0200172static struct pci_driver aw2_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +0200173 .name = KBUILD_MODNAME,
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100174 .id_table = snd_aw2_ids,
175 .probe = snd_aw2_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -0500176 .remove = snd_aw2_remove,
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100177};
178
Takashi Iwaie9f66d92012-04-24 12:25:00 +0200179module_pci_driver(aw2_driver);
180
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100181/* operators for playback PCM alsa interface */
182static 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 */
194static 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
Bill Pembertone23e7a12012-12-06 12:35:10 -0500205static struct snd_kcontrol_new aw2_control = {
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100206 .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
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100220/* component-destructor */
221static 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 if (chip->iobase_virt)
233 iounmap(chip->iobase_virt);
234
235 pci_release_regions(chip->pci);
236 /* disable the PCI entry */
237 pci_disable_device(chip->pci);
238 /* release the data */
239 kfree(chip);
240
241 return 0;
242}
243
244/* chip-specific constructor */
Bill Pembertone23e7a12012-12-06 12:35:10 -0500245static int snd_aw2_create(struct snd_card *card,
246 struct pci_dev *pci, struct aw2 **rchip)
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100247{
248 struct aw2 *chip;
249 int err;
250 static struct snd_device_ops ops = {
251 .dev_free = snd_aw2_dev_free,
252 };
253
254 *rchip = NULL;
255
256 /* initialize the PCI entry */
257 err = pci_enable_device(pci);
258 if (err < 0)
259 return err;
260 pci_set_master(pci);
261
262 /* check PCI availability (32bit DMA) */
Yang Hongyang284901a2009-04-06 19:01:15 -0700263 if ((pci_set_dma_mask(pci, DMA_BIT_MASK(32)) < 0) ||
264 (pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32)) < 0)) {
Takashi Iwai179bb7f2014-02-25 15:54:30 +0100265 dev_err(card->dev, "Impossible to set 32bit mask DMA\n");
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100266 pci_disable_device(pci);
267 return -ENXIO;
268 }
269 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
270 if (chip == NULL) {
271 pci_disable_device(pci);
272 return -ENOMEM;
273 }
274
275 /* initialize the stuff */
276 chip->card = card;
277 chip->pci = pci;
278 chip->irq = -1;
279
280 /* (1) PCI resource allocation */
281 err = pci_request_regions(pci, "Audiowerk2");
282 if (err < 0) {
283 pci_disable_device(pci);
284 kfree(chip);
285 return err;
286 }
287 chip->iobase_phys = pci_resource_start(pci, 0);
288 chip->iobase_virt =
289 ioremap_nocache(chip->iobase_phys,
290 pci_resource_len(pci, 0));
291
292 if (chip->iobase_virt == NULL) {
Takashi Iwai179bb7f2014-02-25 15:54:30 +0100293 dev_err(card->dev, "unable to remap memory region");
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100294 pci_release_regions(pci);
295 pci_disable_device(pci);
296 kfree(chip);
297 return -ENOMEM;
298 }
299
Takashi Iwai44e05172008-06-23 11:54:05 +0200300 /* (2) initialization of the chip hardware */
301 snd_aw2_saa7146_setup(&chip->saa7146, chip->iobase_virt);
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100302
303 if (request_irq(pci->irq, snd_aw2_saa7146_interrupt,
Takashi Iwai934c2b62011-06-10 16:36:37 +0200304 IRQF_SHARED, KBUILD_MODNAME, chip)) {
Takashi Iwai179bb7f2014-02-25 15:54:30 +0100305 dev_err(card->dev, "Cannot grab irq %d\n", pci->irq);
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100306
307 iounmap(chip->iobase_virt);
308 pci_release_regions(chip->pci);
309 pci_disable_device(chip->pci);
310 kfree(chip);
311 return -EBUSY;
312 }
313 chip->irq = pci->irq;
314
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100315 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
316 if (err < 0) {
317 free_irq(chip->irq, (void *)chip);
318 iounmap(chip->iobase_virt);
319 pci_release_regions(chip->pci);
320 pci_disable_device(chip->pci);
321 kfree(chip);
322 return err;
323 }
324
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100325 *rchip = chip;
326
Takashi Iwai179bb7f2014-02-25 15:54:30 +0100327 dev_info(card->dev,
328 "Audiowerk 2 sound card (saa7146 chipset) detected and managed\n");
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100329 return 0;
330}
331
332/* constructor */
Bill Pembertone23e7a12012-12-06 12:35:10 -0500333static int snd_aw2_probe(struct pci_dev *pci,
334 const struct pci_device_id *pci_id)
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100335{
336 static int dev;
337 struct snd_card *card;
338 struct aw2 *chip;
339 int err;
340
341 /* (1) Continue if device is not enabled, else inc dev */
342 if (dev >= SNDRV_CARDS)
343 return -ENODEV;
344 if (!enable[dev]) {
345 dev++;
346 return -ENOENT;
347 }
348
349 /* (2) Create card instance */
Takashi Iwai60c57722014-01-29 14:20:19 +0100350 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
351 0, &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +0100352 if (err < 0)
353 return err;
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100354
355 /* (3) Create main component */
356 err = snd_aw2_create(card, pci, &chip);
357 if (err < 0) {
358 snd_card_free(card);
359 return err;
360 }
361
362 /* initialize mutex */
363 mutex_init(&chip->mtx);
364 /* init spinlock */
365 spin_lock_init(&chip->reg_lock);
366 /* (4) Define driver ID and name string */
367 strcpy(card->driver, "aw2");
368 strcpy(card->shortname, "Audiowerk2");
369
370 sprintf(card->longname, "%s with SAA7146 irq %i",
371 card->shortname, chip->irq);
372
373 /* (5) Create other components */
374 snd_aw2_new_pcm(chip);
375
376 /* (6) Register card instance */
377 err = snd_card_register(card);
378 if (err < 0) {
379 snd_card_free(card);
380 return err;
381 }
382
383 /* (7) Set PCI driver data */
384 pci_set_drvdata(pci, card);
385
386 dev++;
387 return 0;
388}
389
390/* destructor */
Bill Pembertone23e7a12012-12-06 12:35:10 -0500391static void snd_aw2_remove(struct pci_dev *pci)
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100392{
393 snd_card_free(pci_get_drvdata(pci));
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100394}
395
396/* open callback */
397static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream)
398{
399 struct snd_pcm_runtime *runtime = substream->runtime;
400
Takashi Iwai179bb7f2014-02-25 15:54:30 +0100401 dev_dbg(substream->pcm->card->dev, "Playback_open\n");
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100402 runtime->hw = snd_aw2_playback_hw;
403 return 0;
404}
405
406/* close callback */
407static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream)
408{
409 return 0;
410
411}
412
413static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream)
414{
415 struct snd_pcm_runtime *runtime = substream->runtime;
416
Takashi Iwai179bb7f2014-02-25 15:54:30 +0100417 dev_dbg(substream->pcm->card->dev, "Capture_open\n");
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100418 runtime->hw = snd_aw2_capture_hw;
419 return 0;
420}
421
422/* close callback */
423static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream)
424{
425 /* TODO: something to do ? */
426 return 0;
427}
428
429 /* hw_params callback */
430static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
431 struct snd_pcm_hw_params *hw_params)
432{
433 return snd_pcm_lib_malloc_pages(substream,
434 params_buffer_bytes(hw_params));
435}
436
437/* hw_free callback */
438static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream)
439{
440 return snd_pcm_lib_free_pages(substream);
441}
442
443/* prepare callback for playback */
444static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream)
445{
446 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
447 struct aw2 *chip = pcm_device->chip;
448 struct snd_pcm_runtime *runtime = substream->runtime;
449 unsigned long period_size, buffer_size;
450
451 mutex_lock(&chip->mtx);
452
453 period_size = snd_pcm_lib_period_bytes(substream);
454 buffer_size = snd_pcm_lib_buffer_bytes(substream);
455
456 snd_aw2_saa7146_pcm_init_playback(&chip->saa7146,
457 pcm_device->stream_number,
458 runtime->dma_addr, period_size,
459 buffer_size);
460
461 /* Define Interrupt callback */
462 snd_aw2_saa7146_define_it_playback_callback(pcm_device->stream_number,
463 (snd_aw2_saa7146_it_cb)
464 snd_pcm_period_elapsed,
465 (void *)substream);
466
467 mutex_unlock(&chip->mtx);
468
469 return 0;
470}
471
472/* prepare callback for capture */
473static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream)
474{
475 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
476 struct aw2 *chip = pcm_device->chip;
477 struct snd_pcm_runtime *runtime = substream->runtime;
478 unsigned long period_size, buffer_size;
479
480 mutex_lock(&chip->mtx);
481
482 period_size = snd_pcm_lib_period_bytes(substream);
483 buffer_size = snd_pcm_lib_buffer_bytes(substream);
484
485 snd_aw2_saa7146_pcm_init_capture(&chip->saa7146,
486 pcm_device->stream_number,
487 runtime->dma_addr, period_size,
488 buffer_size);
489
490 /* Define Interrupt callback */
491 snd_aw2_saa7146_define_it_capture_callback(pcm_device->stream_number,
492 (snd_aw2_saa7146_it_cb)
493 snd_pcm_period_elapsed,
494 (void *)substream);
495
496 mutex_unlock(&chip->mtx);
497
498 return 0;
499}
500
501/* playback trigger callback */
502static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
503 int cmd)
504{
505 int status = 0;
506 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
507 struct aw2 *chip = pcm_device->chip;
508 spin_lock(&chip->reg_lock);
509 switch (cmd) {
510 case SNDRV_PCM_TRIGGER_START:
511 snd_aw2_saa7146_pcm_trigger_start_playback(&chip->saa7146,
512 pcm_device->
513 stream_number);
514 break;
515 case SNDRV_PCM_TRIGGER_STOP:
516 snd_aw2_saa7146_pcm_trigger_stop_playback(&chip->saa7146,
517 pcm_device->
518 stream_number);
519 break;
520 default:
521 status = -EINVAL;
522 }
523 spin_unlock(&chip->reg_lock);
524 return status;
525}
526
527/* capture trigger callback */
528static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
529 int cmd)
530{
531 int status = 0;
532 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
533 struct aw2 *chip = pcm_device->chip;
534 spin_lock(&chip->reg_lock);
535 switch (cmd) {
536 case SNDRV_PCM_TRIGGER_START:
537 snd_aw2_saa7146_pcm_trigger_start_capture(&chip->saa7146,
538 pcm_device->
539 stream_number);
540 break;
541 case SNDRV_PCM_TRIGGER_STOP:
542 snd_aw2_saa7146_pcm_trigger_stop_capture(&chip->saa7146,
543 pcm_device->
544 stream_number);
545 break;
546 default:
547 status = -EINVAL;
548 }
549 spin_unlock(&chip->reg_lock);
550 return status;
551}
552
553/* playback pointer callback */
554static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
555 *substream)
556{
557 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
558 struct aw2 *chip = pcm_device->chip;
559 unsigned int current_ptr;
560
561 /* get the current hardware pointer */
562 struct snd_pcm_runtime *runtime = substream->runtime;
563 current_ptr =
564 snd_aw2_saa7146_get_hw_ptr_playback(&chip->saa7146,
565 pcm_device->stream_number,
566 runtime->dma_area,
567 runtime->buffer_size);
568
569 return bytes_to_frames(substream->runtime, current_ptr);
570}
571
572/* capture pointer callback */
573static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
574 *substream)
575{
576 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
577 struct aw2 *chip = pcm_device->chip;
578 unsigned int current_ptr;
579
580 /* get the current hardware pointer */
581 struct snd_pcm_runtime *runtime = substream->runtime;
582 current_ptr =
583 snd_aw2_saa7146_get_hw_ptr_capture(&chip->saa7146,
584 pcm_device->stream_number,
585 runtime->dma_area,
586 runtime->buffer_size);
587
588 return bytes_to_frames(substream->runtime, current_ptr);
589}
590
591/* create a pcm device */
Bill Pembertone23e7a12012-12-06 12:35:10 -0500592static int snd_aw2_new_pcm(struct aw2 *chip)
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100593{
594 struct snd_pcm *pcm_playback_ana;
595 struct snd_pcm *pcm_playback_num;
596 struct snd_pcm *pcm_capture;
597 struct aw2_pcm_device *pcm_device;
598 int err = 0;
599
600 /* Create new Alsa PCM device */
601
602 err = snd_pcm_new(chip->card, "Audiowerk2 analog playback", 0, 1, 0,
603 &pcm_playback_ana);
604 if (err < 0) {
Takashi Iwai179bb7f2014-02-25 15:54:30 +0100605 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100606 return err;
607 }
608
609 /* Creation ok */
610 pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_ANA];
611
612 /* Set PCM device name */
613 strcpy(pcm_playback_ana->name, "Analog playback");
614 /* Associate private data to PCM device */
615 pcm_playback_ana->private_data = pcm_device;
616 /* set operators of PCM device */
617 snd_pcm_set_ops(pcm_playback_ana, SNDRV_PCM_STREAM_PLAYBACK,
618 &snd_aw2_playback_ops);
619 /* store PCM device */
620 pcm_device->pcm = pcm_playback_ana;
621 /* give base chip pointer to our internal pcm device
622 structure */
623 pcm_device->chip = chip;
624 /* Give stream number to PCM device */
625 pcm_device->stream_number = NUM_STREAM_PLAYBACK_ANA;
626
627 /* pre-allocation of buffers */
628 /* Preallocate continuous pages. */
629 err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_ana,
630 SNDRV_DMA_TYPE_DEV,
631 snd_dma_pci_data
632 (chip->pci),
633 64 * 1024, 64 * 1024);
634 if (err)
Takashi Iwai179bb7f2014-02-25 15:54:30 +0100635 dev_err(chip->card->dev,
636 "snd_pcm_lib_preallocate_pages_for_all error (0x%X)\n",
637 err);
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100638
639 err = snd_pcm_new(chip->card, "Audiowerk2 digital playback", 1, 1, 0,
640 &pcm_playback_num);
641
642 if (err < 0) {
Takashi Iwai179bb7f2014-02-25 15:54:30 +0100643 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100644 return err;
645 }
646 /* Creation ok */
647 pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_DIG];
648
649 /* Set PCM device name */
650 strcpy(pcm_playback_num->name, "Digital playback");
651 /* Associate private data to PCM device */
652 pcm_playback_num->private_data = pcm_device;
653 /* set operators of PCM device */
654 snd_pcm_set_ops(pcm_playback_num, SNDRV_PCM_STREAM_PLAYBACK,
655 &snd_aw2_playback_ops);
656 /* store PCM device */
657 pcm_device->pcm = pcm_playback_num;
658 /* give base chip pointer to our internal pcm device
659 structure */
660 pcm_device->chip = chip;
661 /* Give stream number to PCM device */
662 pcm_device->stream_number = NUM_STREAM_PLAYBACK_DIG;
663
664 /* pre-allocation of buffers */
665 /* Preallocate continuous pages. */
666 err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_num,
667 SNDRV_DMA_TYPE_DEV,
668 snd_dma_pci_data
669 (chip->pci),
670 64 * 1024, 64 * 1024);
671 if (err)
Takashi Iwai179bb7f2014-02-25 15:54:30 +0100672 dev_err(chip->card->dev,
673 "snd_pcm_lib_preallocate_pages_for_all error (0x%X)\n",
674 err);
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100675
676 err = snd_pcm_new(chip->card, "Audiowerk2 capture", 2, 0, 1,
677 &pcm_capture);
678
679 if (err < 0) {
Takashi Iwai179bb7f2014-02-25 15:54:30 +0100680 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100681 return err;
682 }
683
684 /* Creation ok */
685 pcm_device = &chip->device_capture[NUM_STREAM_CAPTURE_ANA];
686
687 /* Set PCM device name */
688 strcpy(pcm_capture->name, "Capture");
689 /* Associate private data to PCM device */
690 pcm_capture->private_data = pcm_device;
691 /* set operators of PCM device */
692 snd_pcm_set_ops(pcm_capture, SNDRV_PCM_STREAM_CAPTURE,
693 &snd_aw2_capture_ops);
694 /* store PCM device */
695 pcm_device->pcm = pcm_capture;
696 /* give base chip pointer to our internal pcm device
697 structure */
698 pcm_device->chip = chip;
699 /* Give stream number to PCM device */
700 pcm_device->stream_number = NUM_STREAM_CAPTURE_ANA;
701
702 /* pre-allocation of buffers */
703 /* Preallocate continuous pages. */
704 err = snd_pcm_lib_preallocate_pages_for_all(pcm_capture,
705 SNDRV_DMA_TYPE_DEV,
706 snd_dma_pci_data
707 (chip->pci),
708 64 * 1024, 64 * 1024);
709 if (err)
Takashi Iwai179bb7f2014-02-25 15:54:30 +0100710 dev_err(chip->card->dev,
711 "snd_pcm_lib_preallocate_pages_for_all error (0x%X)\n",
712 err);
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100713
714
715 /* Create control */
716 err = snd_ctl_add(chip->card, snd_ctl_new1(&aw2_control, chip));
717 if (err < 0) {
Takashi Iwai179bb7f2014-02-25 15:54:30 +0100718 dev_err(chip->card->dev, "snd_ctl_add error (0x%X)\n", err);
Cedric Bregardis98f2a972008-02-20 12:05:13 +0100719 return err;
720 }
721
722 return 0;
723}
724
725static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
726 struct snd_ctl_elem_info *uinfo)
727{
728 static char *texts[2] = {
729 "Analog", "Digital"
730 };
731 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
732 uinfo->count = 1;
733 uinfo->value.enumerated.items = 2;
734 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) {
735 uinfo->value.enumerated.item =
736 uinfo->value.enumerated.items - 1;
737 }
738 strcpy(uinfo->value.enumerated.name,
739 texts[uinfo->value.enumerated.item]);
740 return 0;
741}
742
743static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
744 struct snd_ctl_elem_value
745 *ucontrol)
746{
747 struct aw2 *chip = snd_kcontrol_chip(kcontrol);
748 if (snd_aw2_saa7146_is_using_digital_input(&chip->saa7146))
749 ucontrol->value.enumerated.item[0] = CTL_ROUTE_DIGITAL;
750 else
751 ucontrol->value.enumerated.item[0] = CTL_ROUTE_ANALOG;
752 return 0;
753}
754
755static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
756 struct snd_ctl_elem_value
757 *ucontrol)
758{
759 struct aw2 *chip = snd_kcontrol_chip(kcontrol);
760 int changed = 0;
761 int is_disgital =
762 snd_aw2_saa7146_is_using_digital_input(&chip->saa7146);
763
764 if (((ucontrol->value.integer.value[0] == CTL_ROUTE_DIGITAL)
765 && !is_disgital)
766 || ((ucontrol->value.integer.value[0] == CTL_ROUTE_ANALOG)
767 && is_disgital)) {
768 snd_aw2_saa7146_use_digital_input(&chip->saa7146, !is_disgital);
769 changed = 1;
770 }
771 return changed;
772}