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