linux/sound/pci/lx6464es/lx6464es.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/* -*- linux-c -*- *
   3 *
   4 * ALSA driver for the digigram lx6464es interface
   5 *
   6 * Copyright (c) 2008, 2009 Tim Blechmann <tim@klingt.org>
   7 */
   8
   9#include <linux/module.h>
  10#include <linux/init.h>
  11#include <linux/pci.h>
  12#include <linux/delay.h>
  13#include <linux/slab.h>
  14
  15#include <sound/initval.h>
  16#include <sound/control.h>
  17#include <sound/info.h>
  18
  19#include "lx6464es.h"
  20
  21MODULE_AUTHOR("Tim Blechmann");
  22MODULE_LICENSE("GPL");
  23MODULE_DESCRIPTION("digigram lx6464es");
  24MODULE_SUPPORTED_DEVICE("{digigram lx6464es{}}");
  25
  26
  27static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  28static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  29static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  30
  31module_param_array(index, int, NULL, 0444);
  32MODULE_PARM_DESC(index, "Index value for Digigram LX6464ES interface.");
  33module_param_array(id, charp, NULL, 0444);
  34MODULE_PARM_DESC(id, "ID string for  Digigram LX6464ES interface.");
  35module_param_array(enable, bool, NULL, 0444);
  36MODULE_PARM_DESC(enable, "Enable/disable specific Digigram LX6464ES soundcards.");
  37
  38static const char card_name[] = "LX6464ES";
  39
  40
  41#define PCI_DEVICE_ID_PLX_LX6464ES              PCI_DEVICE_ID_PLX_9056
  42
  43static const struct pci_device_id snd_lx6464es_ids[] = {
  44        { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
  45                         PCI_VENDOR_ID_DIGIGRAM,
  46                         PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_SERIAL_SUBSYSTEM),
  47        },                      /* LX6464ES */
  48        { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
  49                         PCI_VENDOR_ID_DIGIGRAM,
  50                         PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_CAE_SERIAL_SUBSYSTEM),
  51        },                      /* LX6464ES-CAE */
  52        { 0, },
  53};
  54
  55MODULE_DEVICE_TABLE(pci, snd_lx6464es_ids);
  56
  57
  58
  59/* PGO pour USERo dans le registre pci_0x06/loc_0xEC */
  60#define CHIPSC_RESET_XILINX (1L<<16)
  61
  62
  63/* alsa callbacks */
  64static const struct snd_pcm_hardware lx_caps = {
  65        .info             = (SNDRV_PCM_INFO_MMAP |
  66                             SNDRV_PCM_INFO_INTERLEAVED |
  67                             SNDRV_PCM_INFO_MMAP_VALID |
  68                             SNDRV_PCM_INFO_SYNC_START),
  69        .formats          = (SNDRV_PCM_FMTBIT_S16_LE |
  70                             SNDRV_PCM_FMTBIT_S16_BE |
  71                             SNDRV_PCM_FMTBIT_S24_3LE |
  72                             SNDRV_PCM_FMTBIT_S24_3BE),
  73        .rates            = (SNDRV_PCM_RATE_CONTINUOUS |
  74                             SNDRV_PCM_RATE_8000_192000),
  75        .rate_min         = 8000,
  76        .rate_max         = 192000,
  77        .channels_min     = 2,
  78        .channels_max     = 64,
  79        .buffer_bytes_max = 64*2*3*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER,
  80        .period_bytes_min = (2*2*MICROBLAZE_IBL_MIN*2),
  81        .period_bytes_max = (4*64*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER),
  82        .periods_min      = 2,
  83        .periods_max      = MAX_STREAM_BUFFER,
  84};
  85
  86static int lx_set_granularity(struct lx6464es *chip, u32 gran);
  87
  88
  89static int lx_hardware_open(struct lx6464es *chip,
  90                            struct snd_pcm_substream *substream)
  91{
  92        int err = 0;
  93        struct snd_pcm_runtime *runtime = substream->runtime;
  94        int channels = runtime->channels;
  95        int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  96
  97        snd_pcm_uframes_t period_size = runtime->period_size;
  98
  99        dev_dbg(chip->card->dev, "allocating pipe for %d channels\n", channels);
 100        err = lx_pipe_allocate(chip, 0, is_capture, channels);
 101        if (err < 0) {
 102                dev_err(chip->card->dev, LXP "allocating pipe failed\n");
 103                return err;
 104        }
 105
 106        err = lx_set_granularity(chip, period_size);
 107        if (err < 0) {
 108                dev_err(chip->card->dev, "setting granularity to %ld failed\n",
 109                           period_size);
 110                return err;
 111        }
 112
 113        return 0;
 114}
 115
 116static int lx_hardware_start(struct lx6464es *chip,
 117                             struct snd_pcm_substream *substream)
 118{
 119        int err = 0;
 120        struct snd_pcm_runtime *runtime = substream->runtime;
 121        int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
 122
 123        dev_dbg(chip->card->dev, "setting stream format\n");
 124        err = lx_stream_set_format(chip, runtime, 0, is_capture);
 125        if (err < 0) {
 126                dev_err(chip->card->dev, "setting stream format failed\n");
 127                return err;
 128        }
 129
 130        dev_dbg(chip->card->dev, "starting pipe\n");
 131        err = lx_pipe_start(chip, 0, is_capture);
 132        if (err < 0) {
 133                dev_err(chip->card->dev, "starting pipe failed\n");
 134                return err;
 135        }
 136
 137        dev_dbg(chip->card->dev, "waiting for pipe to start\n");
 138        err = lx_pipe_wait_for_start(chip, 0, is_capture);
 139        if (err < 0) {
 140                dev_err(chip->card->dev, "waiting for pipe failed\n");
 141                return err;
 142        }
 143
 144        return err;
 145}
 146
 147
 148static int lx_hardware_stop(struct lx6464es *chip,
 149                            struct snd_pcm_substream *substream)
 150{
 151        int err = 0;
 152        int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
 153
 154        dev_dbg(chip->card->dev, "pausing pipe\n");
 155        err = lx_pipe_pause(chip, 0, is_capture);
 156        if (err < 0) {
 157                dev_err(chip->card->dev, "pausing pipe failed\n");
 158                return err;
 159        }
 160
 161        dev_dbg(chip->card->dev, "waiting for pipe to become idle\n");
 162        err = lx_pipe_wait_for_idle(chip, 0, is_capture);
 163        if (err < 0) {
 164                dev_err(chip->card->dev, "waiting for pipe failed\n");
 165                return err;
 166        }
 167
 168        dev_dbg(chip->card->dev, "stopping pipe\n");
 169        err = lx_pipe_stop(chip, 0, is_capture);
 170        if (err < 0) {
 171                dev_err(chip->card->dev, "stopping pipe failed\n");
 172                return err;
 173        }
 174
 175        return err;
 176}
 177
 178
 179static int lx_hardware_close(struct lx6464es *chip,
 180                             struct snd_pcm_substream *substream)
 181{
 182        int err = 0;
 183        int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
 184
 185        dev_dbg(chip->card->dev, "releasing pipe\n");
 186        err = lx_pipe_release(chip, 0, is_capture);
 187        if (err < 0) {
 188                dev_err(chip->card->dev, "releasing pipe failed\n");
 189                return err;
 190        }
 191
 192        return err;
 193}
 194
 195
 196static int lx_pcm_open(struct snd_pcm_substream *substream)
 197{
 198        struct lx6464es *chip = snd_pcm_substream_chip(substream);
 199        struct snd_pcm_runtime *runtime = substream->runtime;
 200        int err = 0;
 201        int board_rate;
 202
 203        dev_dbg(chip->card->dev, "->lx_pcm_open\n");
 204        mutex_lock(&chip->setup_mutex);
 205
 206        /* copy the struct snd_pcm_hardware struct */
 207        runtime->hw = lx_caps;
 208
 209#if 0
 210        /* buffer-size should better be multiple of period-size */
 211        err = snd_pcm_hw_constraint_integer(runtime,
 212                                            SNDRV_PCM_HW_PARAM_PERIODS);
 213        if (err < 0) {
 214                dev_warn(chip->card->dev, "could not constrain periods\n");
 215                goto exit;
 216        }
 217#endif
 218
 219        /* the clock rate cannot be changed */
 220        board_rate = chip->board_sample_rate;
 221        err = snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_RATE,
 222                                           board_rate);
 223
 224        if (err < 0) {
 225                dev_warn(chip->card->dev, "could not constrain periods\n");
 226                goto exit;
 227        }
 228
 229        /* constrain period size */
 230        err = snd_pcm_hw_constraint_minmax(runtime,
 231                                           SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
 232                                           MICROBLAZE_IBL_MIN,
 233                                           MICROBLAZE_IBL_MAX);
 234        if (err < 0) {
 235                dev_warn(chip->card->dev,
 236                           "could not constrain period size\n");
 237                goto exit;
 238        }
 239
 240        snd_pcm_hw_constraint_step(runtime, 0,
 241                                   SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
 242
 243        snd_pcm_set_sync(substream);
 244        err = 0;
 245
 246exit:
 247        runtime->private_data = chip;
 248
 249        mutex_unlock(&chip->setup_mutex);
 250        dev_dbg(chip->card->dev, "<-lx_pcm_open, %d\n", err);
 251        return err;
 252}
 253
 254static int lx_pcm_close(struct snd_pcm_substream *substream)
 255{
 256        dev_dbg(substream->pcm->card->dev, "->lx_pcm_close\n");
 257        return 0;
 258}
 259
 260static snd_pcm_uframes_t lx_pcm_stream_pointer(struct snd_pcm_substream
 261                                               *substream)
 262{
 263        struct lx6464es *chip = snd_pcm_substream_chip(substream);
 264        snd_pcm_uframes_t pos;
 265        int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
 266
 267        struct lx_stream *lx_stream = is_capture ? &chip->capture_stream :
 268                &chip->playback_stream;
 269
 270        dev_dbg(chip->card->dev, "->lx_pcm_stream_pointer\n");
 271
 272        mutex_lock(&chip->lock);
 273        pos = lx_stream->frame_pos * substream->runtime->period_size;
 274        mutex_unlock(&chip->lock);
 275
 276        dev_dbg(chip->card->dev, "stream_pointer at %ld\n", pos);
 277        return pos;
 278}
 279
 280static int lx_pcm_prepare(struct snd_pcm_substream *substream)
 281{
 282        struct lx6464es *chip = snd_pcm_substream_chip(substream);
 283        int err = 0;
 284        const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
 285
 286        dev_dbg(chip->card->dev, "->lx_pcm_prepare\n");
 287
 288        mutex_lock(&chip->setup_mutex);
 289
 290        if (chip->hardware_running[is_capture]) {
 291                err = lx_hardware_stop(chip, substream);
 292                if (err < 0) {
 293                        dev_err(chip->card->dev, "failed to stop hardware. "
 294                                   "Error code %d\n", err);
 295                        goto exit;
 296                }
 297
 298                err = lx_hardware_close(chip, substream);
 299                if (err < 0) {
 300                        dev_err(chip->card->dev, "failed to close hardware. "
 301                                   "Error code %d\n", err);
 302                        goto exit;
 303                }
 304        }
 305
 306        dev_dbg(chip->card->dev, "opening hardware\n");
 307        err = lx_hardware_open(chip, substream);
 308        if (err < 0) {
 309                dev_err(chip->card->dev, "failed to open hardware. "
 310                           "Error code %d\n", err);
 311                goto exit;
 312        }
 313
 314        err = lx_hardware_start(chip, substream);
 315        if (err < 0) {
 316                dev_err(chip->card->dev, "failed to start hardware. "
 317                           "Error code %d\n", err);
 318                goto exit;
 319        }
 320
 321        chip->hardware_running[is_capture] = 1;
 322
 323        if (chip->board_sample_rate != substream->runtime->rate) {
 324                if (!err)
 325                        chip->board_sample_rate = substream->runtime->rate;
 326        }
 327
 328exit:
 329        mutex_unlock(&chip->setup_mutex);
 330        return err;
 331}
 332
 333static int lx_pcm_hw_params(struct snd_pcm_substream *substream,
 334                            struct snd_pcm_hw_params *hw_params, int is_capture)
 335{
 336        struct lx6464es *chip = snd_pcm_substream_chip(substream);
 337        int err = 0;
 338
 339        dev_dbg(chip->card->dev, "->lx_pcm_hw_params\n");
 340
 341        mutex_lock(&chip->setup_mutex);
 342
 343        /* set dma buffer */
 344        err = snd_pcm_lib_malloc_pages(substream,
 345                                       params_buffer_bytes(hw_params));
 346
 347        if (is_capture)
 348                chip->capture_stream.stream = substream;
 349        else
 350                chip->playback_stream.stream = substream;
 351
 352        mutex_unlock(&chip->setup_mutex);
 353        return err;
 354}
 355
 356static int lx_pcm_hw_params_playback(struct snd_pcm_substream *substream,
 357                                 struct snd_pcm_hw_params *hw_params)
 358{
 359        return lx_pcm_hw_params(substream, hw_params, 0);
 360}
 361
 362static int lx_pcm_hw_params_capture(struct snd_pcm_substream *substream,
 363                                 struct snd_pcm_hw_params *hw_params)
 364{
 365        return lx_pcm_hw_params(substream, hw_params, 1);
 366}
 367
 368static int lx_pcm_hw_free(struct snd_pcm_substream *substream)
 369{
 370        struct lx6464es *chip = snd_pcm_substream_chip(substream);
 371        int err = 0;
 372        int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
 373
 374        dev_dbg(chip->card->dev, "->lx_pcm_hw_free\n");
 375        mutex_lock(&chip->setup_mutex);
 376
 377        if (chip->hardware_running[is_capture]) {
 378                err = lx_hardware_stop(chip, substream);
 379                if (err < 0) {
 380                        dev_err(chip->card->dev, "failed to stop hardware. "
 381                                   "Error code %d\n", err);
 382                        goto exit;
 383                }
 384
 385                err = lx_hardware_close(chip, substream);
 386                if (err < 0) {
 387                        dev_err(chip->card->dev, "failed to close hardware. "
 388                                   "Error code %d\n", err);
 389                        goto exit;
 390                }
 391
 392                chip->hardware_running[is_capture] = 0;
 393        }
 394
 395        err = snd_pcm_lib_free_pages(substream);
 396
 397        if (is_capture)
 398                chip->capture_stream.stream = NULL;
 399        else
 400                chip->playback_stream.stream = NULL;
 401
 402exit:
 403        mutex_unlock(&chip->setup_mutex);
 404        return err;
 405}
 406
 407static void lx_trigger_start(struct lx6464es *chip, struct lx_stream *lx_stream)
 408{
 409        struct snd_pcm_substream *substream = lx_stream->stream;
 410        const unsigned int is_capture = lx_stream->is_capture;
 411
 412        int err;
 413
 414        const u32 channels = substream->runtime->channels;
 415        const u32 bytes_per_frame = channels * 3;
 416        const u32 period_size = substream->runtime->period_size;
 417        const u32 periods = substream->runtime->periods;
 418        const u32 period_bytes = period_size * bytes_per_frame;
 419
 420        dma_addr_t buf = substream->dma_buffer.addr;
 421        int i;
 422
 423        u32 needed, freed;
 424        u32 size_array[5];
 425
 426        for (i = 0; i != periods; ++i) {
 427                u32 buffer_index = 0;
 428
 429                err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed,
 430                                    size_array);
 431                dev_dbg(chip->card->dev, "starting: needed %d, freed %d\n",
 432                            needed, freed);
 433
 434                err = lx_buffer_give(chip, 0, is_capture, period_bytes,
 435                                     lower_32_bits(buf), upper_32_bits(buf),
 436                                     &buffer_index);
 437
 438                dev_dbg(chip->card->dev, "starting: buffer index %x on 0x%lx (%d bytes)\n",
 439                            buffer_index, (unsigned long)buf, period_bytes);
 440                buf += period_bytes;
 441        }
 442
 443        err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed, size_array);
 444        dev_dbg(chip->card->dev, "starting: needed %d, freed %d\n", needed, freed);
 445
 446        dev_dbg(chip->card->dev, "starting: starting stream\n");
 447        err = lx_stream_start(chip, 0, is_capture);
 448        if (err < 0)
 449                dev_err(chip->card->dev, "couldn't start stream\n");
 450        else
 451                lx_stream->status = LX_STREAM_STATUS_RUNNING;
 452
 453        lx_stream->frame_pos = 0;
 454}
 455
 456static void lx_trigger_stop(struct lx6464es *chip, struct lx_stream *lx_stream)
 457{
 458        const unsigned int is_capture = lx_stream->is_capture;
 459        int err;
 460
 461        dev_dbg(chip->card->dev, "stopping: stopping stream\n");
 462        err = lx_stream_stop(chip, 0, is_capture);
 463        if (err < 0)
 464                dev_err(chip->card->dev, "couldn't stop stream\n");
 465        else
 466                lx_stream->status = LX_STREAM_STATUS_FREE;
 467
 468}
 469
 470static void lx_trigger_dispatch_stream(struct lx6464es *chip,
 471                                       struct lx_stream *lx_stream)
 472{
 473        switch (lx_stream->status) {
 474        case LX_STREAM_STATUS_SCHEDULE_RUN:
 475                lx_trigger_start(chip, lx_stream);
 476                break;
 477
 478        case LX_STREAM_STATUS_SCHEDULE_STOP:
 479                lx_trigger_stop(chip, lx_stream);
 480                break;
 481
 482        default:
 483                break;
 484        }
 485}
 486
 487static int lx_pcm_trigger_dispatch(struct lx6464es *chip,
 488                                   struct lx_stream *lx_stream, int cmd)
 489{
 490        int err = 0;
 491
 492        mutex_lock(&chip->lock);
 493        switch (cmd) {
 494        case SNDRV_PCM_TRIGGER_START:
 495                lx_stream->status = LX_STREAM_STATUS_SCHEDULE_RUN;
 496                break;
 497
 498        case SNDRV_PCM_TRIGGER_STOP:
 499                lx_stream->status = LX_STREAM_STATUS_SCHEDULE_STOP;
 500                break;
 501
 502        default:
 503                err = -EINVAL;
 504                goto exit;
 505        }
 506
 507        lx_trigger_dispatch_stream(chip, &chip->capture_stream);
 508        lx_trigger_dispatch_stream(chip, &chip->playback_stream);
 509
 510exit:
 511        mutex_unlock(&chip->lock);
 512        return err;
 513}
 514
 515
 516static int lx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 517{
 518        struct lx6464es *chip = snd_pcm_substream_chip(substream);
 519        const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
 520        struct lx_stream *stream = is_capture ? &chip->capture_stream :
 521                &chip->playback_stream;
 522
 523        dev_dbg(chip->card->dev, "->lx_pcm_trigger\n");
 524
 525        return lx_pcm_trigger_dispatch(chip, stream, cmd);
 526}
 527
 528static int snd_lx6464es_free(struct lx6464es *chip)
 529{
 530        dev_dbg(chip->card->dev, "->snd_lx6464es_free\n");
 531
 532        lx_irq_disable(chip);
 533
 534        if (chip->irq >= 0)
 535                free_irq(chip->irq, chip);
 536
 537        iounmap(chip->port_dsp_bar);
 538        ioport_unmap(chip->port_plx_remapped);
 539
 540        pci_release_regions(chip->pci);
 541        pci_disable_device(chip->pci);
 542
 543        kfree(chip);
 544
 545        return 0;
 546}
 547
 548static int snd_lx6464es_dev_free(struct snd_device *device)
 549{
 550        return snd_lx6464es_free(device->device_data);
 551}
 552
 553/* reset the dsp during initialization */
 554static int lx_init_xilinx_reset(struct lx6464es *chip)
 555{
 556        int i;
 557        u32 plx_reg = lx_plx_reg_read(chip, ePLX_CHIPSC);
 558
 559        dev_dbg(chip->card->dev, "->lx_init_xilinx_reset\n");
 560
 561        /* activate reset of xilinx */
 562        plx_reg &= ~CHIPSC_RESET_XILINX;
 563
 564        lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
 565        msleep(1);
 566
 567        lx_plx_reg_write(chip, ePLX_MBOX3, 0);
 568        msleep(1);
 569
 570        plx_reg |= CHIPSC_RESET_XILINX;
 571        lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
 572
 573        /* deactivate reset of xilinx */
 574        for (i = 0; i != 100; ++i) {
 575                u32 reg_mbox3;
 576                msleep(10);
 577                reg_mbox3 = lx_plx_reg_read(chip, ePLX_MBOX3);
 578                if (reg_mbox3) {
 579                        dev_dbg(chip->card->dev, "xilinx reset done\n");
 580                        dev_dbg(chip->card->dev, "xilinx took %d loops\n", i);
 581                        break;
 582                }
 583        }
 584
 585        /* todo: add some error handling? */
 586
 587        /* clear mr */
 588        lx_dsp_reg_write(chip, eReg_CSM, 0);
 589
 590        /* le xilinx ES peut ne pas etre encore pret, on attend. */
 591        msleep(600);
 592
 593        return 0;
 594}
 595
 596static int lx_init_xilinx_test(struct lx6464es *chip)
 597{
 598        u32 reg;
 599
 600        dev_dbg(chip->card->dev, "->lx_init_xilinx_test\n");
 601
 602        /* TEST if we have access to Xilinx/MicroBlaze */
 603        lx_dsp_reg_write(chip, eReg_CSM, 0);
 604
 605        reg = lx_dsp_reg_read(chip, eReg_CSM);
 606
 607        if (reg) {
 608                dev_err(chip->card->dev, "Problem: Reg_CSM %x.\n", reg);
 609
 610                /* PCI9056_SPACE0_REMAP */
 611                lx_plx_reg_write(chip, ePLX_PCICR, 1);
 612
 613                reg = lx_dsp_reg_read(chip, eReg_CSM);
 614                if (reg) {
 615                        dev_err(chip->card->dev, "Error: Reg_CSM %x.\n", reg);
 616                        return -EAGAIN; /* seems to be appropriate */
 617                }
 618        }
 619
 620        dev_dbg(chip->card->dev, "Xilinx/MicroBlaze access test successful\n");
 621
 622        return 0;
 623}
 624
 625/* initialize ethersound */
 626static int lx_init_ethersound_config(struct lx6464es *chip)
 627{
 628        int i;
 629        u32 orig_conf_es = lx_dsp_reg_read(chip, eReg_CONFES);
 630
 631        /* configure 64 io channels */
 632        u32 conf_es = (orig_conf_es & CONFES_READ_PART_MASK) |
 633                (64 << IOCR_INPUTS_OFFSET) |
 634                (64 << IOCR_OUTPUTS_OFFSET) |
 635                (FREQ_RATIO_SINGLE_MODE << FREQ_RATIO_OFFSET);
 636
 637        dev_dbg(chip->card->dev, "->lx_init_ethersound\n");
 638
 639        chip->freq_ratio = FREQ_RATIO_SINGLE_MODE;
 640
 641        /*
 642         * write it to the card !
 643         * this actually kicks the ES xilinx, the first time since poweron.
 644         * the MAC address in the Reg_ADMACESMSB Reg_ADMACESLSB registers
 645         * is not ready before this is done, and the bit 2 in Reg_CSES is set.
 646         * */
 647        lx_dsp_reg_write(chip, eReg_CONFES, conf_es);
 648
 649        for (i = 0; i != 1000; ++i) {
 650                if (lx_dsp_reg_read(chip, eReg_CSES) & 4) {
 651                        dev_dbg(chip->card->dev, "ethersound initialized after %dms\n",
 652                                   i);
 653                        goto ethersound_initialized;
 654                }
 655                msleep(1);
 656        }
 657        dev_warn(chip->card->dev,
 658                   "ethersound could not be initialized after %dms\n", i);
 659        return -ETIMEDOUT;
 660
 661 ethersound_initialized:
 662        dev_dbg(chip->card->dev, "ethersound initialized\n");
 663        return 0;
 664}
 665
 666static int lx_init_get_version_features(struct lx6464es *chip)
 667{
 668        u32 dsp_version;
 669
 670        int err;
 671
 672        dev_dbg(chip->card->dev, "->lx_init_get_version_features\n");
 673
 674        err = lx_dsp_get_version(chip, &dsp_version);
 675
 676        if (err == 0) {
 677                u32 freq;
 678
 679                dev_info(chip->card->dev, "DSP version: V%02d.%02d #%d\n",
 680                           (dsp_version>>16) & 0xff, (dsp_version>>8) & 0xff,
 681                           dsp_version & 0xff);
 682
 683                /* later: what firmware version do we expect? */
 684
 685                /* retrieve Play/Rec features */
 686                /* done here because we may have to handle alternate
 687                 * DSP files. */
 688                /* later */
 689
 690                /* init the EtherSound sample rate */
 691                err = lx_dsp_get_clock_frequency(chip, &freq);
 692                if (err == 0)
 693                        chip->board_sample_rate = freq;
 694                dev_dbg(chip->card->dev, "actual clock frequency %d\n", freq);
 695        } else {
 696                dev_err(chip->card->dev, "DSP corrupted \n");
 697                err = -EAGAIN;
 698        }
 699
 700        return err;
 701}
 702
 703static int lx_set_granularity(struct lx6464es *chip, u32 gran)
 704{
 705        int err = 0;
 706        u32 snapped_gran = MICROBLAZE_IBL_MIN;
 707
 708        dev_dbg(chip->card->dev, "->lx_set_granularity\n");
 709
 710        /* blocksize is a power of 2 */
 711        while ((snapped_gran < gran) &&
 712               (snapped_gran < MICROBLAZE_IBL_MAX)) {
 713                snapped_gran *= 2;
 714        }
 715
 716        if (snapped_gran == chip->pcm_granularity)
 717                return 0;
 718
 719        err = lx_dsp_set_granularity(chip, snapped_gran);
 720        if (err < 0) {
 721                dev_warn(chip->card->dev, "could not set granularity\n");
 722                err = -EAGAIN;
 723        }
 724
 725        if (snapped_gran != gran)
 726                dev_err(chip->card->dev, "snapped blocksize to %d\n", snapped_gran);
 727
 728        dev_dbg(chip->card->dev, "set blocksize on board %d\n", snapped_gran);
 729        chip->pcm_granularity = snapped_gran;
 730
 731        return err;
 732}
 733
 734/* initialize and test the xilinx dsp chip */
 735static int lx_init_dsp(struct lx6464es *chip)
 736{
 737        int err;
 738        int i;
 739
 740        dev_dbg(chip->card->dev, "->lx_init_dsp\n");
 741
 742        dev_dbg(chip->card->dev, "initialize board\n");
 743        err = lx_init_xilinx_reset(chip);
 744        if (err)
 745                return err;
 746
 747        dev_dbg(chip->card->dev, "testing board\n");
 748        err = lx_init_xilinx_test(chip);
 749        if (err)
 750                return err;
 751
 752        dev_dbg(chip->card->dev, "initialize ethersound configuration\n");
 753        err = lx_init_ethersound_config(chip);
 754        if (err)
 755                return err;
 756
 757        lx_irq_enable(chip);
 758
 759        /** \todo the mac address should be ready by not, but it isn't,
 760         *  so we wait for it */
 761        for (i = 0; i != 1000; ++i) {
 762                err = lx_dsp_get_mac(chip);
 763                if (err)
 764                        return err;
 765                if (chip->mac_address[0] || chip->mac_address[1] || chip->mac_address[2] ||
 766                    chip->mac_address[3] || chip->mac_address[4] || chip->mac_address[5])
 767                        goto mac_ready;
 768                msleep(1);
 769        }
 770        return -ETIMEDOUT;
 771
 772mac_ready:
 773        dev_dbg(chip->card->dev, "mac address ready read after: %dms\n", i);
 774        dev_info(chip->card->dev,
 775                 "mac address: %02X.%02X.%02X.%02X.%02X.%02X\n",
 776                   chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
 777                   chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
 778
 779        err = lx_init_get_version_features(chip);
 780        if (err)
 781                return err;
 782
 783        lx_set_granularity(chip, MICROBLAZE_IBL_DEFAULT);
 784
 785        chip->playback_mute = 0;
 786
 787        return err;
 788}
 789
 790static const struct snd_pcm_ops lx_ops_playback = {
 791        .open      = lx_pcm_open,
 792        .close     = lx_pcm_close,
 793        .ioctl     = snd_pcm_lib_ioctl,
 794        .prepare   = lx_pcm_prepare,
 795        .hw_params = lx_pcm_hw_params_playback,
 796        .hw_free   = lx_pcm_hw_free,
 797        .trigger   = lx_pcm_trigger,
 798        .pointer   = lx_pcm_stream_pointer,
 799};
 800
 801static const struct snd_pcm_ops lx_ops_capture = {
 802        .open      = lx_pcm_open,
 803        .close     = lx_pcm_close,
 804        .ioctl     = snd_pcm_lib_ioctl,
 805        .prepare   = lx_pcm_prepare,
 806        .hw_params = lx_pcm_hw_params_capture,
 807        .hw_free   = lx_pcm_hw_free,
 808        .trigger   = lx_pcm_trigger,
 809        .pointer   = lx_pcm_stream_pointer,
 810};
 811
 812static int lx_pcm_create(struct lx6464es *chip)
 813{
 814        int err;
 815        struct snd_pcm *pcm;
 816
 817        u32 size = 64 *              /* channels */
 818                3 *                  /* 24 bit samples */
 819                MAX_STREAM_BUFFER *  /* periods */
 820                MICROBLAZE_IBL_MAX * /* frames per period */
 821                2;                   /* duplex */
 822
 823        size = PAGE_ALIGN(size);
 824
 825        /* hardcoded device name & channel count */
 826        err = snd_pcm_new(chip->card, (char *)card_name, 0,
 827                          1, 1, &pcm);
 828        if (err < 0)
 829                return err;
 830
 831        pcm->private_data = chip;
 832
 833        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &lx_ops_playback);
 834        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &lx_ops_capture);
 835
 836        pcm->info_flags = 0;
 837        pcm->nonatomic = true;
 838        strcpy(pcm->name, card_name);
 839
 840        snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
 841                                              snd_dma_pci_data(chip->pci),
 842                                              size, size);
 843
 844        chip->pcm = pcm;
 845        chip->capture_stream.is_capture = 1;
 846
 847        return 0;
 848}
 849
 850static int lx_control_playback_info(struct snd_kcontrol *kcontrol,
 851                                    struct snd_ctl_elem_info *uinfo)
 852{
 853        uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
 854        uinfo->count = 1;
 855        uinfo->value.integer.min = 0;
 856        uinfo->value.integer.max = 1;
 857        return 0;
 858}
 859
 860static int lx_control_playback_get(struct snd_kcontrol *kcontrol,
 861                                   struct snd_ctl_elem_value *ucontrol)
 862{
 863        struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
 864        ucontrol->value.integer.value[0] = chip->playback_mute;
 865        return 0;
 866}
 867
 868static int lx_control_playback_put(struct snd_kcontrol *kcontrol,
 869                                   struct snd_ctl_elem_value *ucontrol)
 870{
 871        struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
 872        int changed = 0;
 873        int current_value = chip->playback_mute;
 874
 875        if (current_value != ucontrol->value.integer.value[0]) {
 876                lx_level_unmute(chip, 0, !current_value);
 877                chip->playback_mute = !current_value;
 878                changed = 1;
 879        }
 880        return changed;
 881}
 882
 883static const struct snd_kcontrol_new lx_control_playback_switch = {
 884        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 885        .name = "PCM Playback Switch",
 886        .index = 0,
 887        .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 888        .private_value = 0,
 889        .info = lx_control_playback_info,
 890        .get = lx_control_playback_get,
 891        .put = lx_control_playback_put
 892};
 893
 894
 895
 896static void lx_proc_levels_read(struct snd_info_entry *entry,
 897                                struct snd_info_buffer *buffer)
 898{
 899        u32 levels[64];
 900        int err;
 901        int i, j;
 902        struct lx6464es *chip = entry->private_data;
 903
 904        snd_iprintf(buffer, "capture levels:\n");
 905        err = lx_level_peaks(chip, 1, 64, levels);
 906        if (err < 0)
 907                return;
 908
 909        for (i = 0; i != 8; ++i) {
 910                for (j = 0; j != 8; ++j)
 911                        snd_iprintf(buffer, "%08x ", levels[i*8+j]);
 912                snd_iprintf(buffer, "\n");
 913        }
 914
 915        snd_iprintf(buffer, "\nplayback levels:\n");
 916
 917        err = lx_level_peaks(chip, 0, 64, levels);
 918        if (err < 0)
 919                return;
 920
 921        for (i = 0; i != 8; ++i) {
 922                for (j = 0; j != 8; ++j)
 923                        snd_iprintf(buffer, "%08x ", levels[i*8+j]);
 924                snd_iprintf(buffer, "\n");
 925        }
 926
 927        snd_iprintf(buffer, "\n");
 928}
 929
 930static int lx_proc_create(struct snd_card *card, struct lx6464es *chip)
 931{
 932        return snd_card_ro_proc_new(card, "levels", chip, lx_proc_levels_read);
 933}
 934
 935
 936static int snd_lx6464es_create(struct snd_card *card,
 937                               struct pci_dev *pci,
 938                               struct lx6464es **rchip)
 939{
 940        struct lx6464es *chip;
 941        int err;
 942
 943        static struct snd_device_ops ops = {
 944                .dev_free = snd_lx6464es_dev_free,
 945        };
 946
 947        dev_dbg(card->dev, "->snd_lx6464es_create\n");
 948
 949        *rchip = NULL;
 950
 951        /* enable PCI device */
 952        err = pci_enable_device(pci);
 953        if (err < 0)
 954                return err;
 955
 956        pci_set_master(pci);
 957
 958        /* check if we can restrict PCI DMA transfers to 32 bits */
 959        err = dma_set_mask(&pci->dev, DMA_BIT_MASK(32));
 960        if (err < 0) {
 961                dev_err(card->dev,
 962                        "architecture does not support 32bit PCI busmaster DMA\n");
 963                pci_disable_device(pci);
 964                return -ENXIO;
 965        }
 966
 967        chip = kzalloc(sizeof(*chip), GFP_KERNEL);
 968        if (chip == NULL) {
 969                err = -ENOMEM;
 970                goto alloc_failed;
 971        }
 972
 973        chip->card = card;
 974        chip->pci = pci;
 975        chip->irq = -1;
 976
 977        /* initialize synchronization structs */
 978        mutex_init(&chip->lock);
 979        mutex_init(&chip->msg_lock);
 980        mutex_init(&chip->setup_mutex);
 981
 982        /* request resources */
 983        err = pci_request_regions(pci, card_name);
 984        if (err < 0)
 985                goto request_regions_failed;
 986
 987        /* plx port */
 988        chip->port_plx = pci_resource_start(pci, 1);
 989        chip->port_plx_remapped = ioport_map(chip->port_plx,
 990                                             pci_resource_len(pci, 1));
 991
 992        /* dsp port */
 993        chip->port_dsp_bar = pci_ioremap_bar(pci, 2);
 994        if (!chip->port_dsp_bar) {
 995                dev_err(card->dev, "cannot remap PCI memory region\n");
 996                err = -ENOMEM;
 997                goto remap_pci_failed;
 998        }
 999
1000        err = request_threaded_irq(pci->irq, lx_interrupt, lx_threaded_irq,
1001                                   IRQF_SHARED, KBUILD_MODNAME, chip);
1002        if (err) {
1003                dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
1004                goto request_irq_failed;
1005        }
1006        chip->irq = pci->irq;
1007
1008        err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1009        if (err < 0)
1010                goto device_new_failed;
1011
1012        err = lx_init_dsp(chip);
1013        if (err < 0) {
1014                dev_err(card->dev, "error during DSP initialization\n");
1015                return err;
1016        }
1017
1018        err = lx_pcm_create(chip);
1019        if (err < 0)
1020                return err;
1021
1022        err = lx_proc_create(card, chip);
1023        if (err < 0)
1024                return err;
1025
1026        err = snd_ctl_add(card, snd_ctl_new1(&lx_control_playback_switch,
1027                                             chip));
1028        if (err < 0)
1029                return err;
1030
1031        *rchip = chip;
1032        return 0;
1033
1034device_new_failed:
1035        free_irq(pci->irq, chip);
1036
1037request_irq_failed:
1038        iounmap(chip->port_dsp_bar);
1039
1040remap_pci_failed:
1041        pci_release_regions(pci);
1042
1043request_regions_failed:
1044        kfree(chip);
1045
1046alloc_failed:
1047        pci_disable_device(pci);
1048
1049        return err;
1050}
1051
1052static int snd_lx6464es_probe(struct pci_dev *pci,
1053                              const struct pci_device_id *pci_id)
1054{
1055        static int dev;
1056        struct snd_card *card;
1057        struct lx6464es *chip;
1058        int err;
1059
1060        dev_dbg(&pci->dev, "->snd_lx6464es_probe\n");
1061
1062        if (dev >= SNDRV_CARDS)
1063                return -ENODEV;
1064        if (!enable[dev]) {
1065                dev++;
1066                return -ENOENT;
1067        }
1068
1069        err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1070                           0, &card);
1071        if (err < 0)
1072                return err;
1073
1074        err = snd_lx6464es_create(card, pci, &chip);
1075        if (err < 0) {
1076                dev_err(card->dev, "error during snd_lx6464es_create\n");
1077                goto out_free;
1078        }
1079
1080        strcpy(card->driver, "LX6464ES");
1081        sprintf(card->id, "LX6464ES_%02X%02X%02X",
1082                chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
1083
1084        sprintf(card->shortname, "LX6464ES %02X.%02X.%02X.%02X.%02X.%02X",
1085                chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
1086                chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
1087
1088        sprintf(card->longname, "%s at 0x%lx, 0x%p, irq %i",
1089                card->shortname, chip->port_plx,
1090                chip->port_dsp_bar, chip->irq);
1091
1092        err = snd_card_register(card);
1093        if (err < 0)
1094                goto out_free;
1095
1096        dev_dbg(chip->card->dev, "initialization successful\n");
1097        pci_set_drvdata(pci, card);
1098        dev++;
1099        return 0;
1100
1101out_free:
1102        snd_card_free(card);
1103        return err;
1104
1105}
1106
1107static void snd_lx6464es_remove(struct pci_dev *pci)
1108{
1109        snd_card_free(pci_get_drvdata(pci));
1110}
1111
1112
1113static struct pci_driver lx6464es_driver = {
1114        .name =     KBUILD_MODNAME,
1115        .id_table = snd_lx6464es_ids,
1116        .probe =    snd_lx6464es_probe,
1117        .remove = snd_lx6464es_remove,
1118};
1119
1120module_pci_driver(lx6464es_driver);
1121