0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/init.h>
0011 #include <linux/pci.h>
0012 #include <linux/dma-mapping.h>
0013 #include <linux/slab.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/delay.h>
0016 #include <linux/io.h>
0017 #include <linux/module.h>
0018 #include <sound/core.h>
0019 #include <sound/initval.h>
0020 #include <sound/pcm.h>
0021 #include <sound/pcm_params.h>
0022 #include <sound/control.h>
0023
0024 #include "saa7146.h"
0025 #include "aw2-saa7146.h"
0026
0027 MODULE_AUTHOR("Cedric Bregardis <cedric.bregardis@free.fr>, "
0028 "Jean-Christian Hassler <jhassler@free.fr>");
0029 MODULE_DESCRIPTION("Emagic Audiowerk 2 sound driver");
0030 MODULE_LICENSE("GPL");
0031
0032
0033
0034
0035 #define CTL_ROUTE_ANALOG 0
0036 #define CTL_ROUTE_DIGITAL 1
0037
0038
0039
0040
0041
0042 static const struct snd_pcm_hardware snd_aw2_playback_hw = {
0043 .info = (SNDRV_PCM_INFO_MMAP |
0044 SNDRV_PCM_INFO_INTERLEAVED |
0045 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
0046 .formats = SNDRV_PCM_FMTBIT_S16_LE,
0047 .rates = SNDRV_PCM_RATE_44100,
0048 .rate_min = 44100,
0049 .rate_max = 44100,
0050 .channels_min = 2,
0051 .channels_max = 4,
0052 .buffer_bytes_max = 32768,
0053 .period_bytes_min = 4096,
0054 .period_bytes_max = 32768,
0055 .periods_min = 1,
0056 .periods_max = 1024,
0057 };
0058
0059 static const struct snd_pcm_hardware snd_aw2_capture_hw = {
0060 .info = (SNDRV_PCM_INFO_MMAP |
0061 SNDRV_PCM_INFO_INTERLEAVED |
0062 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
0063 .formats = SNDRV_PCM_FMTBIT_S16_LE,
0064 .rates = SNDRV_PCM_RATE_44100,
0065 .rate_min = 44100,
0066 .rate_max = 44100,
0067 .channels_min = 2,
0068 .channels_max = 2,
0069 .buffer_bytes_max = 32768,
0070 .period_bytes_min = 4096,
0071 .period_bytes_max = 32768,
0072 .periods_min = 1,
0073 .periods_max = 1024,
0074 };
0075
0076 struct aw2_pcm_device {
0077 struct snd_pcm *pcm;
0078 unsigned int stream_number;
0079 struct aw2 *chip;
0080 };
0081
0082 struct aw2 {
0083 struct snd_aw2_saa7146 saa7146;
0084
0085 struct pci_dev *pci;
0086 int irq;
0087 spinlock_t reg_lock;
0088 struct mutex mtx;
0089
0090 unsigned long iobase_phys;
0091 void __iomem *iobase_virt;
0092
0093 struct snd_card *card;
0094
0095 struct aw2_pcm_device device_playback[NB_STREAM_PLAYBACK];
0096 struct aw2_pcm_device device_capture[NB_STREAM_CAPTURE];
0097 };
0098
0099
0100
0101
0102 static int snd_aw2_create(struct snd_card *card, struct pci_dev *pci);
0103 static int snd_aw2_probe(struct pci_dev *pci,
0104 const struct pci_device_id *pci_id);
0105 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream);
0106 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream);
0107 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream);
0108 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream);
0109 static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream);
0110 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream);
0111 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
0112 int cmd);
0113 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
0114 int cmd);
0115 static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
0116 *substream);
0117 static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
0118 *substream);
0119 static int snd_aw2_new_pcm(struct aw2 *chip);
0120
0121 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
0122 struct snd_ctl_elem_info *uinfo);
0123 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
0124 struct snd_ctl_elem_value
0125 *ucontrol);
0126 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
0127 struct snd_ctl_elem_value
0128 *ucontrol);
0129
0130
0131
0132
0133 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
0134 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
0135 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
0136
0137 module_param_array(index, int, NULL, 0444);
0138 MODULE_PARM_DESC(index, "Index value for Audiowerk2 soundcard.");
0139 module_param_array(id, charp, NULL, 0444);
0140 MODULE_PARM_DESC(id, "ID string for the Audiowerk2 soundcard.");
0141 module_param_array(enable, bool, NULL, 0444);
0142 MODULE_PARM_DESC(enable, "Enable Audiowerk2 soundcard.");
0143
0144 static const struct pci_device_id snd_aw2_ids[] = {
0145 {PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_PHILIPS_SAA7146, 0, 0,
0146 0, 0, 0},
0147 {0}
0148 };
0149
0150 MODULE_DEVICE_TABLE(pci, snd_aw2_ids);
0151
0152
0153 static struct pci_driver aw2_driver = {
0154 .name = KBUILD_MODNAME,
0155 .id_table = snd_aw2_ids,
0156 .probe = snd_aw2_probe,
0157 };
0158
0159 module_pci_driver(aw2_driver);
0160
0161
0162 static const struct snd_pcm_ops snd_aw2_playback_ops = {
0163 .open = snd_aw2_pcm_playback_open,
0164 .close = snd_aw2_pcm_playback_close,
0165 .prepare = snd_aw2_pcm_prepare_playback,
0166 .trigger = snd_aw2_pcm_trigger_playback,
0167 .pointer = snd_aw2_pcm_pointer_playback,
0168 };
0169
0170
0171 static const struct snd_pcm_ops snd_aw2_capture_ops = {
0172 .open = snd_aw2_pcm_capture_open,
0173 .close = snd_aw2_pcm_capture_close,
0174 .prepare = snd_aw2_pcm_prepare_capture,
0175 .trigger = snd_aw2_pcm_trigger_capture,
0176 .pointer = snd_aw2_pcm_pointer_capture,
0177 };
0178
0179 static const struct snd_kcontrol_new aw2_control = {
0180 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0181 .name = "PCM Capture Route",
0182 .index = 0,
0183 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
0184 .private_value = 0xffff,
0185 .info = snd_aw2_control_switch_capture_info,
0186 .get = snd_aw2_control_switch_capture_get,
0187 .put = snd_aw2_control_switch_capture_put
0188 };
0189
0190
0191
0192
0193
0194
0195 static void snd_aw2_free(struct snd_card *card)
0196 {
0197 struct aw2 *chip = card->private_data;
0198
0199
0200 snd_aw2_saa7146_free(&chip->saa7146);
0201 }
0202
0203
0204 static int snd_aw2_create(struct snd_card *card,
0205 struct pci_dev *pci)
0206 {
0207 struct aw2 *chip = card->private_data;
0208 int err;
0209
0210
0211 err = pcim_enable_device(pci);
0212 if (err < 0)
0213 return err;
0214 pci_set_master(pci);
0215
0216
0217 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32))) {
0218 dev_err(card->dev, "Impossible to set 32bit mask DMA\n");
0219 return -ENXIO;
0220 }
0221
0222
0223 chip->card = card;
0224 chip->pci = pci;
0225 chip->irq = -1;
0226
0227
0228 err = pcim_iomap_regions(pci, 1 << 0, "Audiowerk2");
0229 if (err < 0)
0230 return err;
0231 chip->iobase_phys = pci_resource_start(pci, 0);
0232 chip->iobase_virt = pcim_iomap_table(pci)[0];
0233
0234
0235 snd_aw2_saa7146_setup(&chip->saa7146, chip->iobase_virt);
0236
0237 if (devm_request_irq(&pci->dev, pci->irq, snd_aw2_saa7146_interrupt,
0238 IRQF_SHARED, KBUILD_MODNAME, chip)) {
0239 dev_err(card->dev, "Cannot grab irq %d\n", pci->irq);
0240 return -EBUSY;
0241 }
0242 chip->irq = pci->irq;
0243 card->sync_irq = chip->irq;
0244 card->private_free = snd_aw2_free;
0245
0246 dev_info(card->dev,
0247 "Audiowerk 2 sound card (saa7146 chipset) detected and managed\n");
0248 return 0;
0249 }
0250
0251
0252 static int snd_aw2_probe(struct pci_dev *pci,
0253 const struct pci_device_id *pci_id)
0254 {
0255 static int dev;
0256 struct snd_card *card;
0257 struct aw2 *chip;
0258 int err;
0259
0260
0261 if (dev >= SNDRV_CARDS)
0262 return -ENODEV;
0263 if (!enable[dev]) {
0264 dev++;
0265 return -ENOENT;
0266 }
0267
0268
0269 err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
0270 sizeof(*chip), &card);
0271 if (err < 0)
0272 return err;
0273 chip = card->private_data;
0274
0275
0276 err = snd_aw2_create(card, pci);
0277 if (err < 0)
0278 goto error;
0279
0280
0281 mutex_init(&chip->mtx);
0282
0283 spin_lock_init(&chip->reg_lock);
0284
0285 strcpy(card->driver, "aw2");
0286 strcpy(card->shortname, "Audiowerk2");
0287
0288 sprintf(card->longname, "%s with SAA7146 irq %i",
0289 card->shortname, chip->irq);
0290
0291
0292 snd_aw2_new_pcm(chip);
0293
0294
0295 err = snd_card_register(card);
0296 if (err < 0)
0297 goto error;
0298
0299
0300 pci_set_drvdata(pci, card);
0301
0302 dev++;
0303 return 0;
0304
0305 error:
0306 snd_card_free(card);
0307 return err;
0308 }
0309
0310
0311 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream)
0312 {
0313 struct snd_pcm_runtime *runtime = substream->runtime;
0314
0315 dev_dbg(substream->pcm->card->dev, "Playback_open\n");
0316 runtime->hw = snd_aw2_playback_hw;
0317 return 0;
0318 }
0319
0320
0321 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream)
0322 {
0323 return 0;
0324
0325 }
0326
0327 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream)
0328 {
0329 struct snd_pcm_runtime *runtime = substream->runtime;
0330
0331 dev_dbg(substream->pcm->card->dev, "Capture_open\n");
0332 runtime->hw = snd_aw2_capture_hw;
0333 return 0;
0334 }
0335
0336
0337 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream)
0338 {
0339
0340 return 0;
0341 }
0342
0343
0344 static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream)
0345 {
0346 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
0347 struct aw2 *chip = pcm_device->chip;
0348 struct snd_pcm_runtime *runtime = substream->runtime;
0349 unsigned long period_size, buffer_size;
0350
0351 mutex_lock(&chip->mtx);
0352
0353 period_size = snd_pcm_lib_period_bytes(substream);
0354 buffer_size = snd_pcm_lib_buffer_bytes(substream);
0355
0356 snd_aw2_saa7146_pcm_init_playback(&chip->saa7146,
0357 pcm_device->stream_number,
0358 runtime->dma_addr, period_size,
0359 buffer_size);
0360
0361
0362 snd_aw2_saa7146_define_it_playback_callback(pcm_device->stream_number,
0363 (snd_aw2_saa7146_it_cb)
0364 snd_pcm_period_elapsed,
0365 (void *)substream);
0366
0367 mutex_unlock(&chip->mtx);
0368
0369 return 0;
0370 }
0371
0372
0373 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream)
0374 {
0375 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
0376 struct aw2 *chip = pcm_device->chip;
0377 struct snd_pcm_runtime *runtime = substream->runtime;
0378 unsigned long period_size, buffer_size;
0379
0380 mutex_lock(&chip->mtx);
0381
0382 period_size = snd_pcm_lib_period_bytes(substream);
0383 buffer_size = snd_pcm_lib_buffer_bytes(substream);
0384
0385 snd_aw2_saa7146_pcm_init_capture(&chip->saa7146,
0386 pcm_device->stream_number,
0387 runtime->dma_addr, period_size,
0388 buffer_size);
0389
0390
0391 snd_aw2_saa7146_define_it_capture_callback(pcm_device->stream_number,
0392 (snd_aw2_saa7146_it_cb)
0393 snd_pcm_period_elapsed,
0394 (void *)substream);
0395
0396 mutex_unlock(&chip->mtx);
0397
0398 return 0;
0399 }
0400
0401
0402 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
0403 int cmd)
0404 {
0405 int status = 0;
0406 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
0407 struct aw2 *chip = pcm_device->chip;
0408 spin_lock(&chip->reg_lock);
0409 switch (cmd) {
0410 case SNDRV_PCM_TRIGGER_START:
0411 snd_aw2_saa7146_pcm_trigger_start_playback(&chip->saa7146,
0412 pcm_device->
0413 stream_number);
0414 break;
0415 case SNDRV_PCM_TRIGGER_STOP:
0416 snd_aw2_saa7146_pcm_trigger_stop_playback(&chip->saa7146,
0417 pcm_device->
0418 stream_number);
0419 break;
0420 default:
0421 status = -EINVAL;
0422 }
0423 spin_unlock(&chip->reg_lock);
0424 return status;
0425 }
0426
0427
0428 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
0429 int cmd)
0430 {
0431 int status = 0;
0432 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
0433 struct aw2 *chip = pcm_device->chip;
0434 spin_lock(&chip->reg_lock);
0435 switch (cmd) {
0436 case SNDRV_PCM_TRIGGER_START:
0437 snd_aw2_saa7146_pcm_trigger_start_capture(&chip->saa7146,
0438 pcm_device->
0439 stream_number);
0440 break;
0441 case SNDRV_PCM_TRIGGER_STOP:
0442 snd_aw2_saa7146_pcm_trigger_stop_capture(&chip->saa7146,
0443 pcm_device->
0444 stream_number);
0445 break;
0446 default:
0447 status = -EINVAL;
0448 }
0449 spin_unlock(&chip->reg_lock);
0450 return status;
0451 }
0452
0453
0454 static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
0455 *substream)
0456 {
0457 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
0458 struct aw2 *chip = pcm_device->chip;
0459 unsigned int current_ptr;
0460
0461
0462 struct snd_pcm_runtime *runtime = substream->runtime;
0463 current_ptr =
0464 snd_aw2_saa7146_get_hw_ptr_playback(&chip->saa7146,
0465 pcm_device->stream_number,
0466 runtime->dma_area,
0467 runtime->buffer_size);
0468
0469 return bytes_to_frames(substream->runtime, current_ptr);
0470 }
0471
0472
0473 static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
0474 *substream)
0475 {
0476 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
0477 struct aw2 *chip = pcm_device->chip;
0478 unsigned int current_ptr;
0479
0480
0481 struct snd_pcm_runtime *runtime = substream->runtime;
0482 current_ptr =
0483 snd_aw2_saa7146_get_hw_ptr_capture(&chip->saa7146,
0484 pcm_device->stream_number,
0485 runtime->dma_area,
0486 runtime->buffer_size);
0487
0488 return bytes_to_frames(substream->runtime, current_ptr);
0489 }
0490
0491
0492 static int snd_aw2_new_pcm(struct aw2 *chip)
0493 {
0494 struct snd_pcm *pcm_playback_ana;
0495 struct snd_pcm *pcm_playback_num;
0496 struct snd_pcm *pcm_capture;
0497 struct aw2_pcm_device *pcm_device;
0498 int err = 0;
0499
0500
0501
0502 err = snd_pcm_new(chip->card, "Audiowerk2 analog playback", 0, 1, 0,
0503 &pcm_playback_ana);
0504 if (err < 0) {
0505 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
0506 return err;
0507 }
0508
0509
0510 pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_ANA];
0511
0512
0513 strcpy(pcm_playback_ana->name, "Analog playback");
0514
0515 pcm_playback_ana->private_data = pcm_device;
0516
0517 snd_pcm_set_ops(pcm_playback_ana, SNDRV_PCM_STREAM_PLAYBACK,
0518 &snd_aw2_playback_ops);
0519
0520 pcm_device->pcm = pcm_playback_ana;
0521
0522
0523 pcm_device->chip = chip;
0524
0525 pcm_device->stream_number = NUM_STREAM_PLAYBACK_ANA;
0526
0527
0528
0529 snd_pcm_set_managed_buffer_all(pcm_playback_ana,
0530 SNDRV_DMA_TYPE_DEV,
0531 &chip->pci->dev,
0532 64 * 1024, 64 * 1024);
0533
0534 err = snd_pcm_new(chip->card, "Audiowerk2 digital playback", 1, 1, 0,
0535 &pcm_playback_num);
0536
0537 if (err < 0) {
0538 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
0539 return err;
0540 }
0541
0542 pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_DIG];
0543
0544
0545 strcpy(pcm_playback_num->name, "Digital playback");
0546
0547 pcm_playback_num->private_data = pcm_device;
0548
0549 snd_pcm_set_ops(pcm_playback_num, SNDRV_PCM_STREAM_PLAYBACK,
0550 &snd_aw2_playback_ops);
0551
0552 pcm_device->pcm = pcm_playback_num;
0553
0554
0555 pcm_device->chip = chip;
0556
0557 pcm_device->stream_number = NUM_STREAM_PLAYBACK_DIG;
0558
0559
0560
0561 snd_pcm_set_managed_buffer_all(pcm_playback_num,
0562 SNDRV_DMA_TYPE_DEV,
0563 &chip->pci->dev,
0564 64 * 1024, 64 * 1024);
0565
0566 err = snd_pcm_new(chip->card, "Audiowerk2 capture", 2, 0, 1,
0567 &pcm_capture);
0568
0569 if (err < 0) {
0570 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
0571 return err;
0572 }
0573
0574
0575 pcm_device = &chip->device_capture[NUM_STREAM_CAPTURE_ANA];
0576
0577
0578 strcpy(pcm_capture->name, "Capture");
0579
0580 pcm_capture->private_data = pcm_device;
0581
0582 snd_pcm_set_ops(pcm_capture, SNDRV_PCM_STREAM_CAPTURE,
0583 &snd_aw2_capture_ops);
0584
0585 pcm_device->pcm = pcm_capture;
0586
0587
0588 pcm_device->chip = chip;
0589
0590 pcm_device->stream_number = NUM_STREAM_CAPTURE_ANA;
0591
0592
0593
0594 snd_pcm_set_managed_buffer_all(pcm_capture,
0595 SNDRV_DMA_TYPE_DEV,
0596 &chip->pci->dev,
0597 64 * 1024, 64 * 1024);
0598
0599
0600 err = snd_ctl_add(chip->card, snd_ctl_new1(&aw2_control, chip));
0601 if (err < 0) {
0602 dev_err(chip->card->dev, "snd_ctl_add error (0x%X)\n", err);
0603 return err;
0604 }
0605
0606 return 0;
0607 }
0608
0609 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
0610 struct snd_ctl_elem_info *uinfo)
0611 {
0612 static const char * const texts[2] = {
0613 "Analog", "Digital"
0614 };
0615 return snd_ctl_enum_info(uinfo, 1, 2, texts);
0616 }
0617
0618 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
0619 struct snd_ctl_elem_value
0620 *ucontrol)
0621 {
0622 struct aw2 *chip = snd_kcontrol_chip(kcontrol);
0623 if (snd_aw2_saa7146_is_using_digital_input(&chip->saa7146))
0624 ucontrol->value.enumerated.item[0] = CTL_ROUTE_DIGITAL;
0625 else
0626 ucontrol->value.enumerated.item[0] = CTL_ROUTE_ANALOG;
0627 return 0;
0628 }
0629
0630 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
0631 struct snd_ctl_elem_value
0632 *ucontrol)
0633 {
0634 struct aw2 *chip = snd_kcontrol_chip(kcontrol);
0635 int changed = 0;
0636 int is_disgital =
0637 snd_aw2_saa7146_is_using_digital_input(&chip->saa7146);
0638
0639 if (((ucontrol->value.integer.value[0] == CTL_ROUTE_DIGITAL)
0640 && !is_disgital)
0641 || ((ucontrol->value.integer.value[0] == CTL_ROUTE_ANALOG)
0642 && is_disgital)) {
0643 snd_aw2_saa7146_use_digital_input(&chip->saa7146, !is_disgital);
0644 changed = 1;
0645 }
0646 return changed;
0647 }