Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* -*- linux-c -*- *
0003  *
0004  * ALSA driver for the digigram lx6464es interface
0005  *
0006  * Copyright (c) 2008, 2009 Tim Blechmann <tim@klingt.org>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/init.h>
0011 #include <linux/pci.h>
0012 #include <linux/delay.h>
0013 #include <linux/slab.h>
0014 
0015 #include <sound/initval.h>
0016 #include <sound/control.h>
0017 #include <sound/info.h>
0018 
0019 #include "lx6464es.h"
0020 
0021 MODULE_AUTHOR("Tim Blechmann");
0022 MODULE_LICENSE("GPL");
0023 MODULE_DESCRIPTION("digigram lx6464es");
0024 
0025 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
0026 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
0027 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
0028 
0029 module_param_array(index, int, NULL, 0444);
0030 MODULE_PARM_DESC(index, "Index value for Digigram LX6464ES interface.");
0031 module_param_array(id, charp, NULL, 0444);
0032 MODULE_PARM_DESC(id, "ID string for  Digigram LX6464ES interface.");
0033 module_param_array(enable, bool, NULL, 0444);
0034 MODULE_PARM_DESC(enable, "Enable/disable specific Digigram LX6464ES soundcards.");
0035 
0036 static const char card_name[] = "LX6464ES";
0037 
0038 
0039 #define PCI_DEVICE_ID_PLX_LX6464ES      PCI_DEVICE_ID_PLX_9056
0040 
0041 static const struct pci_device_id snd_lx6464es_ids[] = {
0042     { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
0043              PCI_VENDOR_ID_DIGIGRAM,
0044              PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_SERIAL_SUBSYSTEM),
0045     },          /* LX6464ES */
0046     { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
0047              PCI_VENDOR_ID_DIGIGRAM,
0048              PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_CAE_SERIAL_SUBSYSTEM),
0049     },          /* LX6464ES-CAE */
0050     { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
0051              PCI_VENDOR_ID_DIGIGRAM,
0052              PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ESE_SERIAL_SUBSYSTEM),
0053     },          /* LX6464ESe */
0054     { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
0055              PCI_VENDOR_ID_DIGIGRAM,
0056              PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ESE_CAE_SERIAL_SUBSYSTEM),
0057     },          /* LX6464ESe-CAE */
0058     { 0, },
0059 };
0060 
0061 MODULE_DEVICE_TABLE(pci, snd_lx6464es_ids);
0062 
0063 
0064 
0065 /* PGO pour USERo dans le registre pci_0x06/loc_0xEC */
0066 #define CHIPSC_RESET_XILINX (1L<<16)
0067 
0068 
0069 /* alsa callbacks */
0070 static const struct snd_pcm_hardware lx_caps = {
0071     .info             = (SNDRV_PCM_INFO_MMAP |
0072                  SNDRV_PCM_INFO_INTERLEAVED |
0073                  SNDRV_PCM_INFO_MMAP_VALID |
0074                  SNDRV_PCM_INFO_SYNC_START),
0075     .formats      = (SNDRV_PCM_FMTBIT_S16_LE |
0076                  SNDRV_PCM_FMTBIT_S16_BE |
0077                  SNDRV_PCM_FMTBIT_S24_3LE |
0078                  SNDRV_PCM_FMTBIT_S24_3BE),
0079     .rates            = (SNDRV_PCM_RATE_CONTINUOUS |
0080                  SNDRV_PCM_RATE_8000_192000),
0081     .rate_min         = 8000,
0082     .rate_max         = 192000,
0083     .channels_min     = 2,
0084     .channels_max     = 64,
0085     .buffer_bytes_max = 64*2*3*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER,
0086     .period_bytes_min = (2*2*MICROBLAZE_IBL_MIN*2),
0087     .period_bytes_max = (4*64*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER),
0088     .periods_min      = 2,
0089     .periods_max      = MAX_STREAM_BUFFER,
0090 };
0091 
0092 static int lx_set_granularity(struct lx6464es *chip, u32 gran);
0093 
0094 
0095 static int lx_hardware_open(struct lx6464es *chip,
0096                 struct snd_pcm_substream *substream)
0097 {
0098     int err = 0;
0099     struct snd_pcm_runtime *runtime = substream->runtime;
0100     int channels = runtime->channels;
0101     int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
0102 
0103     snd_pcm_uframes_t period_size = runtime->period_size;
0104 
0105     dev_dbg(chip->card->dev, "allocating pipe for %d channels\n", channels);
0106     err = lx_pipe_allocate(chip, 0, is_capture, channels);
0107     if (err < 0) {
0108         dev_err(chip->card->dev, LXP "allocating pipe failed\n");
0109         return err;
0110     }
0111 
0112     err = lx_set_granularity(chip, period_size);
0113     if (err < 0) {
0114         dev_err(chip->card->dev, "setting granularity to %ld failed\n",
0115                period_size);
0116         return err;
0117     }
0118 
0119     return 0;
0120 }
0121 
0122 static int lx_hardware_start(struct lx6464es *chip,
0123                  struct snd_pcm_substream *substream)
0124 {
0125     int err = 0;
0126     struct snd_pcm_runtime *runtime = substream->runtime;
0127     int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
0128 
0129     dev_dbg(chip->card->dev, "setting stream format\n");
0130     err = lx_stream_set_format(chip, runtime, 0, is_capture);
0131     if (err < 0) {
0132         dev_err(chip->card->dev, "setting stream format failed\n");
0133         return err;
0134     }
0135 
0136     dev_dbg(chip->card->dev, "starting pipe\n");
0137     err = lx_pipe_start(chip, 0, is_capture);
0138     if (err < 0) {
0139         dev_err(chip->card->dev, "starting pipe failed\n");
0140         return err;
0141     }
0142 
0143     dev_dbg(chip->card->dev, "waiting for pipe to start\n");
0144     err = lx_pipe_wait_for_start(chip, 0, is_capture);
0145     if (err < 0) {
0146         dev_err(chip->card->dev, "waiting for pipe failed\n");
0147         return err;
0148     }
0149 
0150     return err;
0151 }
0152 
0153 
0154 static int lx_hardware_stop(struct lx6464es *chip,
0155                 struct snd_pcm_substream *substream)
0156 {
0157     int err = 0;
0158     int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
0159 
0160     dev_dbg(chip->card->dev, "pausing pipe\n");
0161     err = lx_pipe_pause(chip, 0, is_capture);
0162     if (err < 0) {
0163         dev_err(chip->card->dev, "pausing pipe failed\n");
0164         return err;
0165     }
0166 
0167     dev_dbg(chip->card->dev, "waiting for pipe to become idle\n");
0168     err = lx_pipe_wait_for_idle(chip, 0, is_capture);
0169     if (err < 0) {
0170         dev_err(chip->card->dev, "waiting for pipe failed\n");
0171         return err;
0172     }
0173 
0174     dev_dbg(chip->card->dev, "stopping pipe\n");
0175     err = lx_pipe_stop(chip, 0, is_capture);
0176     if (err < 0) {
0177         dev_err(chip->card->dev, "stopping pipe failed\n");
0178         return err;
0179     }
0180 
0181     return err;
0182 }
0183 
0184 
0185 static int lx_hardware_close(struct lx6464es *chip,
0186                  struct snd_pcm_substream *substream)
0187 {
0188     int err = 0;
0189     int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
0190 
0191     dev_dbg(chip->card->dev, "releasing pipe\n");
0192     err = lx_pipe_release(chip, 0, is_capture);
0193     if (err < 0) {
0194         dev_err(chip->card->dev, "releasing pipe failed\n");
0195         return err;
0196     }
0197 
0198     return err;
0199 }
0200 
0201 
0202 static int lx_pcm_open(struct snd_pcm_substream *substream)
0203 {
0204     struct lx6464es *chip = snd_pcm_substream_chip(substream);
0205     struct snd_pcm_runtime *runtime = substream->runtime;
0206     int err = 0;
0207     int board_rate;
0208 
0209     dev_dbg(chip->card->dev, "->lx_pcm_open\n");
0210     mutex_lock(&chip->setup_mutex);
0211 
0212     /* copy the struct snd_pcm_hardware struct */
0213     runtime->hw = lx_caps;
0214 
0215 #if 0
0216     /* buffer-size should better be multiple of period-size */
0217     err = snd_pcm_hw_constraint_integer(runtime,
0218                         SNDRV_PCM_HW_PARAM_PERIODS);
0219     if (err < 0) {
0220         dev_warn(chip->card->dev, "could not constrain periods\n");
0221         goto exit;
0222     }
0223 #endif
0224 
0225     /* the clock rate cannot be changed */
0226     board_rate = chip->board_sample_rate;
0227     err = snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_RATE,
0228                        board_rate);
0229 
0230     if (err < 0) {
0231         dev_warn(chip->card->dev, "could not constrain periods\n");
0232         goto exit;
0233     }
0234 
0235     /* constrain period size */
0236     err = snd_pcm_hw_constraint_minmax(runtime,
0237                        SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
0238                        MICROBLAZE_IBL_MIN,
0239                        MICROBLAZE_IBL_MAX);
0240     if (err < 0) {
0241         dev_warn(chip->card->dev,
0242                "could not constrain period size\n");
0243         goto exit;
0244     }
0245 
0246     snd_pcm_hw_constraint_step(runtime, 0,
0247                    SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
0248 
0249     snd_pcm_set_sync(substream);
0250     err = 0;
0251 
0252 exit:
0253     runtime->private_data = chip;
0254 
0255     mutex_unlock(&chip->setup_mutex);
0256     dev_dbg(chip->card->dev, "<-lx_pcm_open, %d\n", err);
0257     return err;
0258 }
0259 
0260 static int lx_pcm_close(struct snd_pcm_substream *substream)
0261 {
0262     dev_dbg(substream->pcm->card->dev, "->lx_pcm_close\n");
0263     return 0;
0264 }
0265 
0266 static snd_pcm_uframes_t lx_pcm_stream_pointer(struct snd_pcm_substream
0267                            *substream)
0268 {
0269     struct lx6464es *chip = snd_pcm_substream_chip(substream);
0270     snd_pcm_uframes_t pos;
0271     int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
0272 
0273     struct lx_stream *lx_stream = is_capture ? &chip->capture_stream :
0274         &chip->playback_stream;
0275 
0276     dev_dbg(chip->card->dev, "->lx_pcm_stream_pointer\n");
0277 
0278     mutex_lock(&chip->lock);
0279     pos = lx_stream->frame_pos * substream->runtime->period_size;
0280     mutex_unlock(&chip->lock);
0281 
0282     dev_dbg(chip->card->dev, "stream_pointer at %ld\n", pos);
0283     return pos;
0284 }
0285 
0286 static int lx_pcm_prepare(struct snd_pcm_substream *substream)
0287 {
0288     struct lx6464es *chip = snd_pcm_substream_chip(substream);
0289     int err = 0;
0290     const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
0291 
0292     dev_dbg(chip->card->dev, "->lx_pcm_prepare\n");
0293 
0294     mutex_lock(&chip->setup_mutex);
0295 
0296     if (chip->hardware_running[is_capture]) {
0297         err = lx_hardware_stop(chip, substream);
0298         if (err < 0) {
0299             dev_err(chip->card->dev, "failed to stop hardware. "
0300                    "Error code %d\n", err);
0301             goto exit;
0302         }
0303 
0304         err = lx_hardware_close(chip, substream);
0305         if (err < 0) {
0306             dev_err(chip->card->dev, "failed to close hardware. "
0307                    "Error code %d\n", err);
0308             goto exit;
0309         }
0310     }
0311 
0312     dev_dbg(chip->card->dev, "opening hardware\n");
0313     err = lx_hardware_open(chip, substream);
0314     if (err < 0) {
0315         dev_err(chip->card->dev, "failed to open hardware. "
0316                "Error code %d\n", err);
0317         goto exit;
0318     }
0319 
0320     err = lx_hardware_start(chip, substream);
0321     if (err < 0) {
0322         dev_err(chip->card->dev, "failed to start hardware. "
0323                "Error code %d\n", err);
0324         goto exit;
0325     }
0326 
0327     chip->hardware_running[is_capture] = 1;
0328 
0329     if (chip->board_sample_rate != substream->runtime->rate) {
0330         if (!err)
0331             chip->board_sample_rate = substream->runtime->rate;
0332     }
0333 
0334 exit:
0335     mutex_unlock(&chip->setup_mutex);
0336     return err;
0337 }
0338 
0339 static int lx_pcm_hw_params(struct snd_pcm_substream *substream,
0340                 struct snd_pcm_hw_params *hw_params, int is_capture)
0341 {
0342     struct lx6464es *chip = snd_pcm_substream_chip(substream);
0343 
0344     dev_dbg(chip->card->dev, "->lx_pcm_hw_params\n");
0345 
0346     mutex_lock(&chip->setup_mutex);
0347 
0348     if (is_capture)
0349         chip->capture_stream.stream = substream;
0350     else
0351         chip->playback_stream.stream = substream;
0352 
0353     mutex_unlock(&chip->setup_mutex);
0354     return 0;
0355 }
0356 
0357 static int lx_pcm_hw_params_playback(struct snd_pcm_substream *substream,
0358                  struct snd_pcm_hw_params *hw_params)
0359 {
0360     return lx_pcm_hw_params(substream, hw_params, 0);
0361 }
0362 
0363 static int lx_pcm_hw_params_capture(struct snd_pcm_substream *substream,
0364                  struct snd_pcm_hw_params *hw_params)
0365 {
0366     return lx_pcm_hw_params(substream, hw_params, 1);
0367 }
0368 
0369 static int lx_pcm_hw_free(struct snd_pcm_substream *substream)
0370 {
0371     struct lx6464es *chip = snd_pcm_substream_chip(substream);
0372     int err = 0;
0373     int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
0374 
0375     dev_dbg(chip->card->dev, "->lx_pcm_hw_free\n");
0376     mutex_lock(&chip->setup_mutex);
0377 
0378     if (chip->hardware_running[is_capture]) {
0379         err = lx_hardware_stop(chip, substream);
0380         if (err < 0) {
0381             dev_err(chip->card->dev, "failed to stop hardware. "
0382                    "Error code %d\n", err);
0383             goto exit;
0384         }
0385 
0386         err = lx_hardware_close(chip, substream);
0387         if (err < 0) {
0388             dev_err(chip->card->dev, "failed to close hardware. "
0389                    "Error code %d\n", err);
0390             goto exit;
0391         }
0392 
0393         chip->hardware_running[is_capture] = 0;
0394     }
0395 
0396     if (is_capture)
0397         chip->capture_stream.stream = NULL;
0398     else
0399         chip->playback_stream.stream = NULL;
0400 
0401 exit:
0402     mutex_unlock(&chip->setup_mutex);
0403     return err;
0404 }
0405 
0406 static void lx_trigger_start(struct lx6464es *chip, struct lx_stream *lx_stream)
0407 {
0408     struct snd_pcm_substream *substream = lx_stream->stream;
0409     const unsigned int is_capture = lx_stream->is_capture;
0410 
0411     int err;
0412 
0413     const u32 channels = substream->runtime->channels;
0414     const u32 bytes_per_frame = channels * 3;
0415     const u32 period_size = substream->runtime->period_size;
0416     const u32 periods = substream->runtime->periods;
0417     const u32 period_bytes = period_size * bytes_per_frame;
0418 
0419     dma_addr_t buf = substream->dma_buffer.addr;
0420     int i;
0421 
0422     u32 needed, freed;
0423     u32 size_array[5];
0424 
0425     for (i = 0; i != periods; ++i) {
0426         u32 buffer_index = 0;
0427 
0428         err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed,
0429                     size_array);
0430         dev_dbg(chip->card->dev, "starting: needed %d, freed %d\n",
0431                 needed, freed);
0432 
0433         err = lx_buffer_give(chip, 0, is_capture, period_bytes,
0434                      lower_32_bits(buf), upper_32_bits(buf),
0435                      &buffer_index);
0436 
0437         dev_dbg(chip->card->dev, "starting: buffer index %x on 0x%lx (%d bytes)\n",
0438                 buffer_index, (unsigned long)buf, period_bytes);
0439         buf += period_bytes;
0440     }
0441 
0442     err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed, size_array);
0443     dev_dbg(chip->card->dev, "starting: needed %d, freed %d\n", needed, freed);
0444 
0445     dev_dbg(chip->card->dev, "starting: starting stream\n");
0446     err = lx_stream_start(chip, 0, is_capture);
0447     if (err < 0)
0448         dev_err(chip->card->dev, "couldn't start stream\n");
0449     else
0450         lx_stream->status = LX_STREAM_STATUS_RUNNING;
0451 
0452     lx_stream->frame_pos = 0;
0453 }
0454 
0455 static void lx_trigger_stop(struct lx6464es *chip, struct lx_stream *lx_stream)
0456 {
0457     const unsigned int is_capture = lx_stream->is_capture;
0458     int err;
0459 
0460     dev_dbg(chip->card->dev, "stopping: stopping stream\n");
0461     err = lx_stream_stop(chip, 0, is_capture);
0462     if (err < 0)
0463         dev_err(chip->card->dev, "couldn't stop stream\n");
0464     else
0465         lx_stream->status = LX_STREAM_STATUS_FREE;
0466 
0467 }
0468 
0469 static void lx_trigger_dispatch_stream(struct lx6464es *chip,
0470                        struct lx_stream *lx_stream)
0471 {
0472     switch (lx_stream->status) {
0473     case LX_STREAM_STATUS_SCHEDULE_RUN:
0474         lx_trigger_start(chip, lx_stream);
0475         break;
0476 
0477     case LX_STREAM_STATUS_SCHEDULE_STOP:
0478         lx_trigger_stop(chip, lx_stream);
0479         break;
0480 
0481     default:
0482         break;
0483     }
0484 }
0485 
0486 static int lx_pcm_trigger_dispatch(struct lx6464es *chip,
0487                    struct lx_stream *lx_stream, int cmd)
0488 {
0489     int err = 0;
0490 
0491     mutex_lock(&chip->lock);
0492     switch (cmd) {
0493     case SNDRV_PCM_TRIGGER_START:
0494         lx_stream->status = LX_STREAM_STATUS_SCHEDULE_RUN;
0495         break;
0496 
0497     case SNDRV_PCM_TRIGGER_STOP:
0498         lx_stream->status = LX_STREAM_STATUS_SCHEDULE_STOP;
0499         break;
0500 
0501     default:
0502         err = -EINVAL;
0503         goto exit;
0504     }
0505 
0506     lx_trigger_dispatch_stream(chip, &chip->capture_stream);
0507     lx_trigger_dispatch_stream(chip, &chip->playback_stream);
0508 
0509 exit:
0510     mutex_unlock(&chip->lock);
0511     return err;
0512 }
0513 
0514 
0515 static int lx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
0516 {
0517     struct lx6464es *chip = snd_pcm_substream_chip(substream);
0518     const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
0519     struct lx_stream *stream = is_capture ? &chip->capture_stream :
0520         &chip->playback_stream;
0521 
0522     dev_dbg(chip->card->dev, "->lx_pcm_trigger\n");
0523 
0524     return lx_pcm_trigger_dispatch(chip, stream, cmd);
0525 }
0526 
0527 static void snd_lx6464es_free(struct snd_card *card)
0528 {
0529     struct lx6464es *chip = card->private_data;
0530 
0531     lx_irq_disable(chip);
0532 }
0533 
0534 /* reset the dsp during initialization */
0535 static int lx_init_xilinx_reset(struct lx6464es *chip)
0536 {
0537     int i;
0538     u32 plx_reg = lx_plx_reg_read(chip, ePLX_CHIPSC);
0539 
0540     dev_dbg(chip->card->dev, "->lx_init_xilinx_reset\n");
0541 
0542     /* activate reset of xilinx */
0543     plx_reg &= ~CHIPSC_RESET_XILINX;
0544 
0545     lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
0546     msleep(1);
0547 
0548     lx_plx_reg_write(chip, ePLX_MBOX3, 0);
0549     msleep(1);
0550 
0551     plx_reg |= CHIPSC_RESET_XILINX;
0552     lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
0553 
0554     /* deactivate reset of xilinx */
0555     for (i = 0; i != 100; ++i) {
0556         u32 reg_mbox3;
0557         msleep(10);
0558         reg_mbox3 = lx_plx_reg_read(chip, ePLX_MBOX3);
0559         if (reg_mbox3) {
0560             dev_dbg(chip->card->dev, "xilinx reset done\n");
0561             dev_dbg(chip->card->dev, "xilinx took %d loops\n", i);
0562             break;
0563         }
0564     }
0565 
0566     /* todo: add some error handling? */
0567 
0568     /* clear mr */
0569     lx_dsp_reg_write(chip, eReg_CSM, 0);
0570 
0571     /* le xilinx ES peut ne pas etre encore pret, on attend. */
0572     msleep(600);
0573 
0574     return 0;
0575 }
0576 
0577 static int lx_init_xilinx_test(struct lx6464es *chip)
0578 {
0579     u32 reg;
0580 
0581     dev_dbg(chip->card->dev, "->lx_init_xilinx_test\n");
0582 
0583     /* TEST if we have access to Xilinx/MicroBlaze */
0584     lx_dsp_reg_write(chip, eReg_CSM, 0);
0585 
0586     reg = lx_dsp_reg_read(chip, eReg_CSM);
0587 
0588     if (reg) {
0589         dev_err(chip->card->dev, "Problem: Reg_CSM %x.\n", reg);
0590 
0591         /* PCI9056_SPACE0_REMAP */
0592         lx_plx_reg_write(chip, ePLX_PCICR, 1);
0593 
0594         reg = lx_dsp_reg_read(chip, eReg_CSM);
0595         if (reg) {
0596             dev_err(chip->card->dev, "Error: Reg_CSM %x.\n", reg);
0597             return -EAGAIN; /* seems to be appropriate */
0598         }
0599     }
0600 
0601     dev_dbg(chip->card->dev, "Xilinx/MicroBlaze access test successful\n");
0602 
0603     return 0;
0604 }
0605 
0606 /* initialize ethersound */
0607 static int lx_init_ethersound_config(struct lx6464es *chip)
0608 {
0609     int i;
0610     u32 orig_conf_es = lx_dsp_reg_read(chip, eReg_CONFES);
0611 
0612     /* configure 64 io channels */
0613     u32 conf_es = (orig_conf_es & CONFES_READ_PART_MASK) |
0614         (64 << IOCR_INPUTS_OFFSET) |
0615         (64 << IOCR_OUTPUTS_OFFSET) |
0616         (FREQ_RATIO_SINGLE_MODE << FREQ_RATIO_OFFSET);
0617 
0618     dev_dbg(chip->card->dev, "->lx_init_ethersound\n");
0619 
0620     chip->freq_ratio = FREQ_RATIO_SINGLE_MODE;
0621 
0622     /*
0623      * write it to the card !
0624      * this actually kicks the ES xilinx, the first time since poweron.
0625      * the MAC address in the Reg_ADMACESMSB Reg_ADMACESLSB registers
0626      * is not ready before this is done, and the bit 2 in Reg_CSES is set.
0627      * */
0628     lx_dsp_reg_write(chip, eReg_CONFES, conf_es);
0629 
0630     for (i = 0; i != 1000; ++i) {
0631         if (lx_dsp_reg_read(chip, eReg_CSES) & 4) {
0632             dev_dbg(chip->card->dev, "ethersound initialized after %dms\n",
0633                    i);
0634             goto ethersound_initialized;
0635         }
0636         msleep(1);
0637     }
0638     dev_warn(chip->card->dev,
0639            "ethersound could not be initialized after %dms\n", i);
0640     return -ETIMEDOUT;
0641 
0642  ethersound_initialized:
0643     dev_dbg(chip->card->dev, "ethersound initialized\n");
0644     return 0;
0645 }
0646 
0647 static int lx_init_get_version_features(struct lx6464es *chip)
0648 {
0649     u32 dsp_version;
0650 
0651     int err;
0652 
0653     dev_dbg(chip->card->dev, "->lx_init_get_version_features\n");
0654 
0655     err = lx_dsp_get_version(chip, &dsp_version);
0656 
0657     if (err == 0) {
0658         u32 freq;
0659 
0660         dev_info(chip->card->dev, "DSP version: V%02d.%02d #%d\n",
0661                (dsp_version>>16) & 0xff, (dsp_version>>8) & 0xff,
0662                dsp_version & 0xff);
0663 
0664         /* later: what firmware version do we expect? */
0665 
0666         /* retrieve Play/Rec features */
0667         /* done here because we may have to handle alternate
0668          * DSP files. */
0669         /* later */
0670 
0671         /* init the EtherSound sample rate */
0672         err = lx_dsp_get_clock_frequency(chip, &freq);
0673         if (err == 0)
0674             chip->board_sample_rate = freq;
0675         dev_dbg(chip->card->dev, "actual clock frequency %d\n", freq);
0676     } else {
0677         dev_err(chip->card->dev, "DSP corrupted \n");
0678         err = -EAGAIN;
0679     }
0680 
0681     return err;
0682 }
0683 
0684 static int lx_set_granularity(struct lx6464es *chip, u32 gran)
0685 {
0686     int err = 0;
0687     u32 snapped_gran = MICROBLAZE_IBL_MIN;
0688 
0689     dev_dbg(chip->card->dev, "->lx_set_granularity\n");
0690 
0691     /* blocksize is a power of 2 */
0692     while ((snapped_gran < gran) &&
0693            (snapped_gran < MICROBLAZE_IBL_MAX)) {
0694         snapped_gran *= 2;
0695     }
0696 
0697     if (snapped_gran == chip->pcm_granularity)
0698         return 0;
0699 
0700     err = lx_dsp_set_granularity(chip, snapped_gran);
0701     if (err < 0) {
0702         dev_warn(chip->card->dev, "could not set granularity\n");
0703         err = -EAGAIN;
0704     }
0705 
0706     if (snapped_gran != gran)
0707         dev_err(chip->card->dev, "snapped blocksize to %d\n", snapped_gran);
0708 
0709     dev_dbg(chip->card->dev, "set blocksize on board %d\n", snapped_gran);
0710     chip->pcm_granularity = snapped_gran;
0711 
0712     return err;
0713 }
0714 
0715 /* initialize and test the xilinx dsp chip */
0716 static int lx_init_dsp(struct lx6464es *chip)
0717 {
0718     int err;
0719     int i;
0720 
0721     dev_dbg(chip->card->dev, "->lx_init_dsp\n");
0722 
0723     dev_dbg(chip->card->dev, "initialize board\n");
0724     err = lx_init_xilinx_reset(chip);
0725     if (err)
0726         return err;
0727 
0728     dev_dbg(chip->card->dev, "testing board\n");
0729     err = lx_init_xilinx_test(chip);
0730     if (err)
0731         return err;
0732 
0733     dev_dbg(chip->card->dev, "initialize ethersound configuration\n");
0734     err = lx_init_ethersound_config(chip);
0735     if (err)
0736         return err;
0737 
0738     lx_irq_enable(chip);
0739 
0740     /** \todo the mac address should be ready by not, but it isn't,
0741      *  so we wait for it */
0742     for (i = 0; i != 1000; ++i) {
0743         err = lx_dsp_get_mac(chip);
0744         if (err)
0745             return err;
0746         if (chip->mac_address[0] || chip->mac_address[1] || chip->mac_address[2] ||
0747             chip->mac_address[3] || chip->mac_address[4] || chip->mac_address[5])
0748             goto mac_ready;
0749         msleep(1);
0750     }
0751     return -ETIMEDOUT;
0752 
0753 mac_ready:
0754     dev_dbg(chip->card->dev, "mac address ready read after: %dms\n", i);
0755     dev_info(chip->card->dev,
0756          "mac address: %02X.%02X.%02X.%02X.%02X.%02X\n",
0757            chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
0758            chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
0759 
0760     err = lx_init_get_version_features(chip);
0761     if (err)
0762         return err;
0763 
0764     lx_set_granularity(chip, MICROBLAZE_IBL_DEFAULT);
0765 
0766     chip->playback_mute = 0;
0767 
0768     return err;
0769 }
0770 
0771 static const struct snd_pcm_ops lx_ops_playback = {
0772     .open      = lx_pcm_open,
0773     .close     = lx_pcm_close,
0774     .prepare   = lx_pcm_prepare,
0775     .hw_params = lx_pcm_hw_params_playback,
0776     .hw_free   = lx_pcm_hw_free,
0777     .trigger   = lx_pcm_trigger,
0778     .pointer   = lx_pcm_stream_pointer,
0779 };
0780 
0781 static const struct snd_pcm_ops lx_ops_capture = {
0782     .open      = lx_pcm_open,
0783     .close     = lx_pcm_close,
0784     .prepare   = lx_pcm_prepare,
0785     .hw_params = lx_pcm_hw_params_capture,
0786     .hw_free   = lx_pcm_hw_free,
0787     .trigger   = lx_pcm_trigger,
0788     .pointer   = lx_pcm_stream_pointer,
0789 };
0790 
0791 static int lx_pcm_create(struct lx6464es *chip)
0792 {
0793     int err;
0794     struct snd_pcm *pcm;
0795 
0796     u32 size = 64 *          /* channels */
0797         3 *          /* 24 bit samples */
0798         MAX_STREAM_BUFFER *  /* periods */
0799         MICROBLAZE_IBL_MAX * /* frames per period */
0800         2;           /* duplex */
0801 
0802     size = PAGE_ALIGN(size);
0803 
0804     /* hardcoded device name & channel count */
0805     err = snd_pcm_new(chip->card, (char *)card_name, 0,
0806               1, 1, &pcm);
0807     if (err < 0)
0808         return err;
0809 
0810     pcm->private_data = chip;
0811 
0812     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &lx_ops_playback);
0813     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &lx_ops_capture);
0814 
0815     pcm->info_flags = 0;
0816     pcm->nonatomic = true;
0817     strcpy(pcm->name, card_name);
0818 
0819     snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
0820                        &chip->pci->dev, size, size);
0821 
0822     chip->pcm = pcm;
0823     chip->capture_stream.is_capture = 1;
0824 
0825     return 0;
0826 }
0827 
0828 static int lx_control_playback_info(struct snd_kcontrol *kcontrol,
0829                     struct snd_ctl_elem_info *uinfo)
0830 {
0831     uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
0832     uinfo->count = 1;
0833     uinfo->value.integer.min = 0;
0834     uinfo->value.integer.max = 1;
0835     return 0;
0836 }
0837 
0838 static int lx_control_playback_get(struct snd_kcontrol *kcontrol,
0839                    struct snd_ctl_elem_value *ucontrol)
0840 {
0841     struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
0842     ucontrol->value.integer.value[0] = chip->playback_mute;
0843     return 0;
0844 }
0845 
0846 static int lx_control_playback_put(struct snd_kcontrol *kcontrol,
0847                    struct snd_ctl_elem_value *ucontrol)
0848 {
0849     struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
0850     int changed = 0;
0851     int current_value = chip->playback_mute;
0852 
0853     if (current_value != ucontrol->value.integer.value[0]) {
0854         lx_level_unmute(chip, 0, !current_value);
0855         chip->playback_mute = !current_value;
0856         changed = 1;
0857     }
0858     return changed;
0859 }
0860 
0861 static const struct snd_kcontrol_new lx_control_playback_switch = {
0862     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0863     .name = "PCM Playback Switch",
0864     .index = 0,
0865     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
0866     .private_value = 0,
0867     .info = lx_control_playback_info,
0868     .get = lx_control_playback_get,
0869     .put = lx_control_playback_put
0870 };
0871 
0872 
0873 
0874 static void lx_proc_levels_read(struct snd_info_entry *entry,
0875                 struct snd_info_buffer *buffer)
0876 {
0877     u32 levels[64];
0878     int err;
0879     int i, j;
0880     struct lx6464es *chip = entry->private_data;
0881 
0882     snd_iprintf(buffer, "capture levels:\n");
0883     err = lx_level_peaks(chip, 1, 64, levels);
0884     if (err < 0)
0885         return;
0886 
0887     for (i = 0; i != 8; ++i) {
0888         for (j = 0; j != 8; ++j)
0889             snd_iprintf(buffer, "%08x ", levels[i*8+j]);
0890         snd_iprintf(buffer, "\n");
0891     }
0892 
0893     snd_iprintf(buffer, "\nplayback levels:\n");
0894 
0895     err = lx_level_peaks(chip, 0, 64, levels);
0896     if (err < 0)
0897         return;
0898 
0899     for (i = 0; i != 8; ++i) {
0900         for (j = 0; j != 8; ++j)
0901             snd_iprintf(buffer, "%08x ", levels[i*8+j]);
0902         snd_iprintf(buffer, "\n");
0903     }
0904 
0905     snd_iprintf(buffer, "\n");
0906 }
0907 
0908 static int lx_proc_create(struct snd_card *card, struct lx6464es *chip)
0909 {
0910     return snd_card_ro_proc_new(card, "levels", chip, lx_proc_levels_read);
0911 }
0912 
0913 
0914 static int snd_lx6464es_create(struct snd_card *card,
0915                    struct pci_dev *pci)
0916 {
0917     struct lx6464es *chip = card->private_data;
0918     int err;
0919 
0920     dev_dbg(card->dev, "->snd_lx6464es_create\n");
0921 
0922     /* enable PCI device */
0923     err = pcim_enable_device(pci);
0924     if (err < 0)
0925         return err;
0926 
0927     pci_set_master(pci);
0928 
0929     /* check if we can restrict PCI DMA transfers to 32 bits */
0930     err = dma_set_mask(&pci->dev, DMA_BIT_MASK(32));
0931     if (err < 0) {
0932         dev_err(card->dev,
0933             "architecture does not support 32bit PCI busmaster DMA\n");
0934         return -ENXIO;
0935     }
0936 
0937     chip->card = card;
0938     chip->pci = pci;
0939     chip->irq = -1;
0940 
0941     /* initialize synchronization structs */
0942     mutex_init(&chip->lock);
0943     mutex_init(&chip->msg_lock);
0944     mutex_init(&chip->setup_mutex);
0945 
0946     /* request resources */
0947     err = pci_request_regions(pci, card_name);
0948     if (err < 0)
0949         return err;
0950 
0951     /* plx port */
0952     chip->port_plx = pci_resource_start(pci, 1);
0953     chip->port_plx_remapped = devm_ioport_map(&pci->dev, chip->port_plx,
0954                           pci_resource_len(pci, 1));
0955     if (!chip->port_plx_remapped)
0956         return -ENOMEM;
0957 
0958     /* dsp port */
0959     chip->port_dsp_bar = pcim_iomap(pci, 2, 0);
0960     if (!chip->port_dsp_bar)
0961         return -ENOMEM;
0962 
0963     err = devm_request_threaded_irq(&pci->dev, pci->irq, lx_interrupt,
0964                     lx_threaded_irq, IRQF_SHARED,
0965                     KBUILD_MODNAME, chip);
0966     if (err) {
0967         dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
0968         return err;
0969     }
0970     chip->irq = pci->irq;
0971     card->sync_irq = chip->irq;
0972     card->private_free = snd_lx6464es_free;
0973 
0974     err = lx_init_dsp(chip);
0975     if (err < 0) {
0976         dev_err(card->dev, "error during DSP initialization\n");
0977         return err;
0978     }
0979 
0980     err = lx_pcm_create(chip);
0981     if (err < 0)
0982         return err;
0983 
0984     err = lx_proc_create(card, chip);
0985     if (err < 0)
0986         return err;
0987 
0988     err = snd_ctl_add(card, snd_ctl_new1(&lx_control_playback_switch,
0989                          chip));
0990     if (err < 0)
0991         return err;
0992 
0993     return 0;
0994 }
0995 
0996 static int snd_lx6464es_probe(struct pci_dev *pci,
0997                   const struct pci_device_id *pci_id)
0998 {
0999     static int dev;
1000     struct snd_card *card;
1001     struct lx6464es *chip;
1002     int err;
1003 
1004     dev_dbg(&pci->dev, "->snd_lx6464es_probe\n");
1005 
1006     if (dev >= SNDRV_CARDS)
1007         return -ENODEV;
1008     if (!enable[dev]) {
1009         dev++;
1010         return -ENOENT;
1011     }
1012 
1013     err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1014                 sizeof(*chip), &card);
1015     if (err < 0)
1016         return err;
1017     chip = card->private_data;
1018 
1019     err = snd_lx6464es_create(card, pci);
1020     if (err < 0) {
1021         dev_err(card->dev, "error during snd_lx6464es_create\n");
1022         goto error;
1023     }
1024 
1025     strcpy(card->driver, "LX6464ES");
1026     sprintf(card->id, "LX6464ES_%02X%02X%02X",
1027         chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
1028 
1029     sprintf(card->shortname, "LX6464ES %02X.%02X.%02X.%02X.%02X.%02X",
1030         chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
1031         chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
1032 
1033     sprintf(card->longname, "%s at 0x%lx, 0x%p, irq %i",
1034         card->shortname, chip->port_plx,
1035         chip->port_dsp_bar, chip->irq);
1036 
1037     err = snd_card_register(card);
1038     if (err < 0)
1039         goto error;
1040 
1041     dev_dbg(chip->card->dev, "initialization successful\n");
1042     pci_set_drvdata(pci, card);
1043     dev++;
1044     return 0;
1045 
1046  error:
1047     snd_card_free(card);
1048     return err;
1049 }
1050 
1051 static struct pci_driver lx6464es_driver = {
1052     .name =     KBUILD_MODNAME,
1053     .id_table = snd_lx6464es_ids,
1054     .probe =    snd_lx6464es_probe,
1055 };
1056 
1057 module_pci_driver(lx6464es_driver);