linux/sound/ppc/snd_ps3.c
<<
>>
Prefs
   1/*
   2 * Audio support for PS3
   3 * Copyright (C) 2007 Sony Computer Entertainment Inc.
   4 * All rights reserved.
   5 * Copyright 2006, 2007 Sony Corporation
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License
   9 * as published by the Free Software Foundation; version 2 of the Licence.
  10 *
  11 * This program is distributed in the hope that it will be useful,
  12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 * GNU General Public License for more details.
  15 *
  16 * You should have received a copy of the GNU General Public License
  17 * along with this program; if not, write to the Free Software
  18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  19 */
  20
  21#include <linux/dma-mapping.h>
  22#include <linux/dmapool.h>
  23#include <linux/init.h>
  24#include <linux/interrupt.h>
  25#include <linux/io.h>
  26#include <linux/slab.h>
  27
  28#include <sound/asound.h>
  29#include <sound/control.h>
  30#include <sound/core.h>
  31#include <sound/initval.h>
  32#include <sound/memalloc.h>
  33#include <sound/pcm.h>
  34#include <sound/pcm_params.h>
  35
  36#include <asm/dma.h>
  37#include <asm/firmware.h>
  38#include <asm/lv1call.h>
  39#include <asm/ps3.h>
  40#include <asm/ps3av.h>
  41
  42#include "snd_ps3.h"
  43#include "snd_ps3_reg.h"
  44
  45
  46/*
  47 * global
  48 */
  49static struct snd_ps3_card_info the_card;
  50
  51static int snd_ps3_start_delay = CONFIG_SND_PS3_DEFAULT_START_DELAY;
  52
  53module_param_named(start_delay, snd_ps3_start_delay, uint, 0644);
  54MODULE_PARM_DESC(start_delay, "time to insert silent data in milisec");
  55
  56static int index = SNDRV_DEFAULT_IDX1;
  57static char *id = SNDRV_DEFAULT_STR1;
  58
  59module_param(index, int, 0444);
  60MODULE_PARM_DESC(index, "Index value for PS3 soundchip.");
  61module_param(id, charp, 0444);
  62MODULE_PARM_DESC(id, "ID string for PS3 soundchip.");
  63
  64
  65/*
  66 * PS3 audio register access
  67 */
  68static inline u32 read_reg(unsigned int reg)
  69{
  70        return in_be32(the_card.mapped_mmio_vaddr + reg);
  71}
  72static inline void write_reg(unsigned int reg, u32 val)
  73{
  74        out_be32(the_card.mapped_mmio_vaddr + reg, val);
  75}
  76static inline void update_reg(unsigned int reg, u32 or_val)
  77{
  78        u32 newval = read_reg(reg) | or_val;
  79        write_reg(reg, newval);
  80}
  81static inline void update_mask_reg(unsigned int reg, u32 mask, u32 or_val)
  82{
  83        u32 newval = (read_reg(reg) & mask) | or_val;
  84        write_reg(reg, newval);
  85}
  86
  87/*
  88 * ALSA defs
  89 */
  90static const struct snd_pcm_hardware snd_ps3_pcm_hw = {
  91        .info = (SNDRV_PCM_INFO_MMAP |
  92                 SNDRV_PCM_INFO_NONINTERLEAVED |
  93                 SNDRV_PCM_INFO_MMAP_VALID),
  94        .formats = (SNDRV_PCM_FMTBIT_S16_BE |
  95                    SNDRV_PCM_FMTBIT_S24_BE),
  96        .rates = (SNDRV_PCM_RATE_44100 |
  97                  SNDRV_PCM_RATE_48000 |
  98                  SNDRV_PCM_RATE_88200 |
  99                  SNDRV_PCM_RATE_96000),
 100        .rate_min = 44100,
 101        .rate_max = 96000,
 102
 103        .channels_min = 2, /* stereo only */
 104        .channels_max = 2,
 105
 106        .buffer_bytes_max = PS3_AUDIO_FIFO_SIZE * 64,
 107
 108        /* interrupt by four stages */
 109        .period_bytes_min = PS3_AUDIO_FIFO_STAGE_SIZE * 4,
 110        .period_bytes_max = PS3_AUDIO_FIFO_STAGE_SIZE * 4,
 111
 112        .periods_min = 16,
 113        .periods_max = 32, /* buffer_size_max/ period_bytes_max */
 114
 115        .fifo_size = PS3_AUDIO_FIFO_SIZE
 116};
 117
 118static int snd_ps3_verify_dma_stop(struct snd_ps3_card_info *card,
 119                                   int count, int force_stop)
 120{
 121        int dma_ch, done, retries, stop_forced = 0;
 122        uint32_t status;
 123
 124        for (dma_ch = 0; dma_ch < 8; dma_ch++) {
 125                retries = count;
 126                do {
 127                        status = read_reg(PS3_AUDIO_KICK(dma_ch)) &
 128                                PS3_AUDIO_KICK_STATUS_MASK;
 129                        switch (status) {
 130                        case PS3_AUDIO_KICK_STATUS_DONE:
 131                        case PS3_AUDIO_KICK_STATUS_NOTIFY:
 132                        case PS3_AUDIO_KICK_STATUS_CLEAR:
 133                        case PS3_AUDIO_KICK_STATUS_ERROR:
 134                                done = 1;
 135                                break;
 136                        default:
 137                                done = 0;
 138                                udelay(10);
 139                        }
 140                } while (!done && --retries);
 141                if (!retries && force_stop) {
 142                        pr_info("%s: DMA ch %d is not stopped.",
 143                                __func__, dma_ch);
 144                        /* last resort. force to stop dma.
 145                         *  NOTE: this cause DMA done interrupts
 146                         */
 147                        update_reg(PS3_AUDIO_CONFIG, PS3_AUDIO_CONFIG_CLEAR);
 148                        stop_forced = 1;
 149                }
 150        }
 151        return stop_forced;
 152}
 153
 154/*
 155 * wait for all dma is done.
 156 * NOTE: caller should reset card->running before call.
 157 *       If not, the interrupt handler will re-start DMA,
 158 *       then DMA is never stopped.
 159 */
 160static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info *card)
 161{
 162        int stop_forced;
 163        /*
 164         * wait for the last dma is done
 165         */
 166
 167        /*
 168         * expected maximum DMA done time is 5.7ms + something (DMA itself).
 169         * 5.7ms is from 16bit/sample 2ch 44.1Khz; the time next
 170         * DMA kick event would occur.
 171         */
 172        stop_forced = snd_ps3_verify_dma_stop(card, 700, 1);
 173
 174        /*
 175         * clear outstanding interrupts.
 176         */
 177        update_reg(PS3_AUDIO_INTR_0, 0);
 178        update_reg(PS3_AUDIO_AX_IS, 0);
 179
 180        /*
 181         *revert CLEAR bit since it will not reset automatically after DMA stop
 182         */
 183        if (stop_forced)
 184                update_mask_reg(PS3_AUDIO_CONFIG, ~PS3_AUDIO_CONFIG_CLEAR, 0);
 185        /* ensure the hardware sees changes */
 186        wmb();
 187}
 188
 189static void snd_ps3_kick_dma(struct snd_ps3_card_info *card)
 190{
 191
 192        update_reg(PS3_AUDIO_KICK(0), PS3_AUDIO_KICK_REQUEST);
 193        /* ensure the hardware sees the change */
 194        wmb();
 195}
 196
 197/*
 198 * convert virtual addr to ioif bus addr.
 199 */
 200static dma_addr_t v_to_bus(struct snd_ps3_card_info *card, void *paddr, int ch)
 201{
 202        return card->dma_start_bus_addr[ch] +
 203                (paddr - card->dma_start_vaddr[ch]);
 204};
 205
 206
 207/*
 208 * increment ring buffer pointer.
 209 * NOTE: caller must hold write spinlock
 210 */
 211static void snd_ps3_bump_buffer(struct snd_ps3_card_info *card,
 212                                enum snd_ps3_ch ch, size_t byte_count,
 213                                int stage)
 214{
 215        if (!stage)
 216                card->dma_last_transfer_vaddr[ch] =
 217                        card->dma_next_transfer_vaddr[ch];
 218        card->dma_next_transfer_vaddr[ch] += byte_count;
 219        if ((card->dma_start_vaddr[ch] + (card->dma_buffer_size / 2)) <=
 220            card->dma_next_transfer_vaddr[ch]) {
 221                card->dma_next_transfer_vaddr[ch] = card->dma_start_vaddr[ch];
 222        }
 223}
 224/*
 225 * setup dmac to send data to audio and attenuate samples on the ring buffer
 226 */
 227static int snd_ps3_program_dma(struct snd_ps3_card_info *card,
 228                               enum snd_ps3_dma_filltype filltype)
 229{
 230        /* this dmac does not support over 4G */
 231        uint32_t dma_addr;
 232        int fill_stages, dma_ch, stage;
 233        enum snd_ps3_ch ch;
 234        uint32_t ch0_kick_event = 0; /* initialize to mute gcc */
 235        void *start_vaddr;
 236        unsigned long irqsave;
 237        int silent = 0;
 238
 239        switch (filltype) {
 240        case SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL:
 241                silent = 1;
 242                /* intentionally fall thru */
 243        case SND_PS3_DMA_FILLTYPE_FIRSTFILL:
 244                ch0_kick_event = PS3_AUDIO_KICK_EVENT_ALWAYS;
 245                break;
 246
 247        case SND_PS3_DMA_FILLTYPE_SILENT_RUNNING:
 248                silent = 1;
 249                /* intentionally fall thru */
 250        case SND_PS3_DMA_FILLTYPE_RUNNING:
 251                ch0_kick_event = PS3_AUDIO_KICK_EVENT_SERIALOUT0_EMPTY;
 252                break;
 253        }
 254
 255        snd_ps3_verify_dma_stop(card, 700, 0);
 256        fill_stages = 4;
 257        spin_lock_irqsave(&card->dma_lock, irqsave);
 258        for (ch = 0; ch < 2; ch++) {
 259                start_vaddr = card->dma_next_transfer_vaddr[0];
 260                for (stage = 0; stage < fill_stages; stage++) {
 261                        dma_ch = stage * 2 + ch;
 262                        if (silent)
 263                                dma_addr = card->null_buffer_start_dma_addr;
 264                        else
 265                                dma_addr =
 266                                v_to_bus(card,
 267                                         card->dma_next_transfer_vaddr[ch],
 268                                         ch);
 269
 270                        write_reg(PS3_AUDIO_SOURCE(dma_ch),
 271                                  (PS3_AUDIO_SOURCE_TARGET_SYSTEM_MEMORY |
 272                                   dma_addr));
 273
 274                        /* dst: fixed to 3wire#0 */
 275                        if (ch == 0)
 276                                write_reg(PS3_AUDIO_DEST(dma_ch),
 277                                          (PS3_AUDIO_DEST_TARGET_AUDIOFIFO |
 278                                           PS3_AUDIO_AO_3W_LDATA(0)));
 279                        else
 280                                write_reg(PS3_AUDIO_DEST(dma_ch),
 281                                          (PS3_AUDIO_DEST_TARGET_AUDIOFIFO |
 282                                           PS3_AUDIO_AO_3W_RDATA(0)));
 283
 284                        /* count always 1 DMA block (1/2 stage = 128 bytes) */
 285                        write_reg(PS3_AUDIO_DMASIZE(dma_ch), 0);
 286                        /* bump pointer if needed */
 287                        if (!silent)
 288                                snd_ps3_bump_buffer(card, ch,
 289                                                    PS3_AUDIO_DMAC_BLOCK_SIZE,
 290                                                    stage);
 291
 292                        /* kick event  */
 293                        if (dma_ch == 0)
 294                                write_reg(PS3_AUDIO_KICK(dma_ch),
 295                                          ch0_kick_event);
 296                        else
 297                                write_reg(PS3_AUDIO_KICK(dma_ch),
 298                                          PS3_AUDIO_KICK_EVENT_AUDIO_DMA(dma_ch
 299                                                                         - 1) |
 300                                          PS3_AUDIO_KICK_REQUEST);
 301                }
 302        }
 303        /* ensure the hardware sees the change */
 304        wmb();
 305        spin_unlock_irqrestore(&card->dma_lock, irqsave);
 306
 307        return 0;
 308}
 309
 310/*
 311 * Interrupt handler
 312 */
 313static irqreturn_t snd_ps3_interrupt(int irq, void *dev_id)
 314{
 315
 316        uint32_t port_intr;
 317        int underflow_occured = 0;
 318        struct snd_ps3_card_info *card = dev_id;
 319
 320        if (!card->running) {
 321                update_reg(PS3_AUDIO_AX_IS, 0);
 322                update_reg(PS3_AUDIO_INTR_0, 0);
 323                return IRQ_HANDLED;
 324        }
 325
 326        port_intr = read_reg(PS3_AUDIO_AX_IS);
 327        /*
 328         *serial buffer empty detected (every 4 times),
 329         *program next dma and kick it
 330         */
 331        if (port_intr & PS3_AUDIO_AX_IE_ASOBEIE(0)) {
 332                write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBEIE(0));
 333                if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) {
 334                        write_reg(PS3_AUDIO_AX_IS, port_intr);
 335                        underflow_occured = 1;
 336                }
 337                if (card->silent) {
 338                        /* we are still in silent time */
 339                        snd_ps3_program_dma(card,
 340                                (underflow_occured) ?
 341                                SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL :
 342                                SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
 343                        snd_ps3_kick_dma(card);
 344                        card->silent--;
 345                } else {
 346                        snd_ps3_program_dma(card,
 347                                (underflow_occured) ?
 348                                SND_PS3_DMA_FILLTYPE_FIRSTFILL :
 349                                SND_PS3_DMA_FILLTYPE_RUNNING);
 350                        snd_ps3_kick_dma(card);
 351                        snd_pcm_period_elapsed(card->substream);
 352                }
 353        } else if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) {
 354                write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBUIE(0));
 355                /*
 356                 * serial out underflow, but buffer empty not detected.
 357                 * in this case, fill fifo with 0 to recover.  After
 358                 * filling dummy data, serial automatically start to
 359                 * consume them and then will generate normal buffer
 360                 * empty interrupts.
 361                 * If both buffer underflow and buffer empty are occured,
 362                 * it is better to do nomal data transfer than empty one
 363                 */
 364                snd_ps3_program_dma(card,
 365                                    SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
 366                snd_ps3_kick_dma(card);
 367                snd_ps3_program_dma(card,
 368                                    SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
 369                snd_ps3_kick_dma(card);
 370        }
 371        /* clear interrupt cause */
 372        return IRQ_HANDLED;
 373};
 374
 375/*
 376 * audio mute on/off
 377 * mute_on : 0 output enabled
 378 *           1 mute
 379 */
 380static int snd_ps3_mute(int mute_on)
 381{
 382        return ps3av_audio_mute(mute_on);
 383}
 384
 385/*
 386 * av setting
 387 * NOTE: calling this function may generate audio interrupt.
 388 */
 389static int snd_ps3_change_avsetting(struct snd_ps3_card_info *card)
 390{
 391        int ret, retries, i;
 392        pr_debug("%s: start\n", __func__);
 393
 394        ret = ps3av_set_audio_mode(card->avs.avs_audio_ch,
 395                                  card->avs.avs_audio_rate,
 396                                  card->avs.avs_audio_width,
 397                                  card->avs.avs_audio_format,
 398                                  card->avs.avs_audio_source);
 399        /*
 400         * Reset the following unwanted settings:
 401         */
 402
 403        /* disable all 3wire buffers */
 404        update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
 405                        ~(PS3_AUDIO_AO_3WMCTRL_ASOEN(0) |
 406                          PS3_AUDIO_AO_3WMCTRL_ASOEN(1) |
 407                          PS3_AUDIO_AO_3WMCTRL_ASOEN(2) |
 408                          PS3_AUDIO_AO_3WMCTRL_ASOEN(3)),
 409                        0);
 410        wmb();  /* ensure the hardware sees the change */
 411        /* wait for actually stopped */
 412        retries = 1000;
 413        while ((read_reg(PS3_AUDIO_AO_3WMCTRL) &
 414                (PS3_AUDIO_AO_3WMCTRL_ASORUN(0) |
 415                 PS3_AUDIO_AO_3WMCTRL_ASORUN(1) |
 416                 PS3_AUDIO_AO_3WMCTRL_ASORUN(2) |
 417                 PS3_AUDIO_AO_3WMCTRL_ASORUN(3))) &&
 418               --retries) {
 419                udelay(1);
 420        }
 421
 422        /* reset buffer pointer */
 423        for (i = 0; i < 4; i++) {
 424                update_reg(PS3_AUDIO_AO_3WCTRL(i),
 425                           PS3_AUDIO_AO_3WCTRL_ASOBRST_RESET);
 426                udelay(10);
 427        }
 428        wmb(); /* ensure the hardware actually start resetting */
 429
 430        /* enable 3wire#0 buffer */
 431        update_reg(PS3_AUDIO_AO_3WMCTRL, PS3_AUDIO_AO_3WMCTRL_ASOEN(0));
 432
 433
 434        /* In 24bit mode,ALSA inserts a zero byte at first byte of per sample */
 435        update_mask_reg(PS3_AUDIO_AO_3WCTRL(0),
 436                        ~PS3_AUDIO_AO_3WCTRL_ASODF,
 437                        PS3_AUDIO_AO_3WCTRL_ASODF_LSB);
 438        update_mask_reg(PS3_AUDIO_AO_SPDCTRL(0),
 439                        ~PS3_AUDIO_AO_SPDCTRL_SPODF,
 440                        PS3_AUDIO_AO_SPDCTRL_SPODF_LSB);
 441        /* ensure all the setting above is written back to register */
 442        wmb();
 443        /* avsetting driver altered AX_IE, caller must reset it if you want */
 444        pr_debug("%s: end\n", __func__);
 445        return ret;
 446}
 447
 448/*
 449 *  set sampling rate according to the substream
 450 */
 451static int snd_ps3_set_avsetting(struct snd_pcm_substream *substream)
 452{
 453        struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
 454        struct snd_ps3_avsetting_info avs;
 455        int ret;
 456
 457        avs = card->avs;
 458
 459        pr_debug("%s: called freq=%d width=%d\n", __func__,
 460                 substream->runtime->rate,
 461                 snd_pcm_format_width(substream->runtime->format));
 462
 463        pr_debug("%s: before freq=%d width=%d\n", __func__,
 464                 card->avs.avs_audio_rate, card->avs.avs_audio_width);
 465
 466        /* sample rate */
 467        switch (substream->runtime->rate) {
 468        case 44100:
 469                avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_44K;
 470                break;
 471        case 48000:
 472                avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
 473                break;
 474        case 88200:
 475                avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_88K;
 476                break;
 477        case 96000:
 478                avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_96K;
 479                break;
 480        default:
 481                pr_info("%s: invalid rate %d\n", __func__,
 482                        substream->runtime->rate);
 483                return 1;
 484        }
 485
 486        /* width */
 487        switch (snd_pcm_format_width(substream->runtime->format)) {
 488        case 16:
 489                avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
 490                break;
 491        case 24:
 492                avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_24;
 493                break;
 494        default:
 495                pr_info("%s: invalid width %d\n", __func__,
 496                        snd_pcm_format_width(substream->runtime->format));
 497                return 1;
 498        }
 499
 500        memcpy(avs.avs_cs_info, ps3av_mode_cs_info, 8);
 501
 502        if (memcmp(&card->avs, &avs, sizeof(avs))) {
 503                pr_debug("%s: after freq=%d width=%d\n", __func__,
 504                         card->avs.avs_audio_rate, card->avs.avs_audio_width);
 505
 506                card->avs = avs;
 507                snd_ps3_change_avsetting(card);
 508                ret = 0;
 509        } else
 510                ret = 1;
 511
 512        /* check CS non-audio bit and mute accordingly */
 513        if (avs.avs_cs_info[0] & 0x02)
 514                ps3av_audio_mute_analog(1); /* mute if non-audio */
 515        else
 516                ps3av_audio_mute_analog(0);
 517
 518        return ret;
 519}
 520
 521/*
 522 * PCM operators
 523 */
 524static int snd_ps3_pcm_open(struct snd_pcm_substream *substream)
 525{
 526        struct snd_pcm_runtime *runtime = substream->runtime;
 527        struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
 528        int pcm_index;
 529
 530        pcm_index = substream->pcm->device;
 531        /* to retrieve substream/runtime in interrupt handler */
 532        card->substream = substream;
 533
 534        runtime->hw = snd_ps3_pcm_hw;
 535
 536        card->start_delay = snd_ps3_start_delay;
 537
 538        /* mute off */
 539        snd_ps3_mute(0); /* this function sleep */
 540
 541        snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
 542                                   PS3_AUDIO_FIFO_STAGE_SIZE * 4 * 2);
 543        return 0;
 544};
 545
 546static int snd_ps3_pcm_close(struct snd_pcm_substream *substream)
 547{
 548        /* mute on */
 549        snd_ps3_mute(1);
 550        return 0;
 551};
 552
 553static int snd_ps3_pcm_hw_params(struct snd_pcm_substream *substream,
 554                                 struct snd_pcm_hw_params *hw_params)
 555{
 556        size_t size;
 557
 558        /* alloc transport buffer */
 559        size = params_buffer_bytes(hw_params);
 560        snd_pcm_lib_malloc_pages(substream, size);
 561        return 0;
 562};
 563
 564static int snd_ps3_pcm_hw_free(struct snd_pcm_substream *substream)
 565{
 566        int ret;
 567        ret = snd_pcm_lib_free_pages(substream);
 568        return ret;
 569};
 570
 571static int snd_ps3_delay_to_bytes(struct snd_pcm_substream *substream,
 572                                  unsigned int delay_ms)
 573{
 574        int ret;
 575        int rate ;
 576
 577        rate = substream->runtime->rate;
 578        ret = snd_pcm_format_size(substream->runtime->format,
 579                                  rate * delay_ms / 1000)
 580                * substream->runtime->channels;
 581
 582        pr_debug(KERN_ERR "%s: time=%d rate=%d bytes=%ld, frames=%d, ret=%d\n",
 583                 __func__,
 584                 delay_ms,
 585                 rate,
 586                 snd_pcm_format_size(substream->runtime->format, rate),
 587                 rate * delay_ms / 1000,
 588                 ret);
 589
 590        return ret;
 591};
 592
 593static int snd_ps3_pcm_prepare(struct snd_pcm_substream *substream)
 594{
 595        struct snd_pcm_runtime *runtime = substream->runtime;
 596        struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
 597        unsigned long irqsave;
 598
 599        if (!snd_ps3_set_avsetting(substream)) {
 600                /* some parameter changed */
 601                write_reg(PS3_AUDIO_AX_IE,
 602                          PS3_AUDIO_AX_IE_ASOBEIE(0) |
 603                          PS3_AUDIO_AX_IE_ASOBUIE(0));
 604                /*
 605                 * let SPDIF device re-lock with SPDIF signal,
 606                 * start with some silence
 607                 */
 608                card->silent = snd_ps3_delay_to_bytes(substream,
 609                                                      card->start_delay) /
 610                        (PS3_AUDIO_FIFO_STAGE_SIZE * 4); /* every 4 times */
 611        }
 612
 613        /* restart ring buffer pointer */
 614        spin_lock_irqsave(&card->dma_lock, irqsave);
 615        {
 616                card->dma_buffer_size = runtime->dma_bytes;
 617
 618                card->dma_last_transfer_vaddr[SND_PS3_CH_L] =
 619                        card->dma_next_transfer_vaddr[SND_PS3_CH_L] =
 620                        card->dma_start_vaddr[SND_PS3_CH_L] =
 621                        runtime->dma_area;
 622                card->dma_start_bus_addr[SND_PS3_CH_L] = runtime->dma_addr;
 623
 624                card->dma_last_transfer_vaddr[SND_PS3_CH_R] =
 625                        card->dma_next_transfer_vaddr[SND_PS3_CH_R] =
 626                        card->dma_start_vaddr[SND_PS3_CH_R] =
 627                        runtime->dma_area + (runtime->dma_bytes / 2);
 628                card->dma_start_bus_addr[SND_PS3_CH_R] =
 629                        runtime->dma_addr + (runtime->dma_bytes / 2);
 630
 631                pr_debug("%s: vaddr=%p bus=%#llx\n", __func__,
 632                         card->dma_start_vaddr[SND_PS3_CH_L],
 633                         card->dma_start_bus_addr[SND_PS3_CH_L]);
 634
 635        }
 636        spin_unlock_irqrestore(&card->dma_lock, irqsave);
 637
 638        /* ensure the hardware sees the change */
 639        mb();
 640
 641        return 0;
 642};
 643
 644static int snd_ps3_pcm_trigger(struct snd_pcm_substream *substream,
 645                               int cmd)
 646{
 647        struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
 648        int ret = 0;
 649
 650        switch (cmd) {
 651        case SNDRV_PCM_TRIGGER_START:
 652                /* clear outstanding interrupts  */
 653                update_reg(PS3_AUDIO_AX_IS, 0);
 654
 655                spin_lock(&card->dma_lock);
 656                {
 657                        card->running = 1;
 658                }
 659                spin_unlock(&card->dma_lock);
 660
 661                snd_ps3_program_dma(card,
 662                                    SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
 663                snd_ps3_kick_dma(card);
 664                while (read_reg(PS3_AUDIO_KICK(7)) &
 665                       PS3_AUDIO_KICK_STATUS_MASK) {
 666                        udelay(1);
 667                }
 668                snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
 669                snd_ps3_kick_dma(card);
 670                break;
 671
 672        case SNDRV_PCM_TRIGGER_STOP:
 673                spin_lock(&card->dma_lock);
 674                {
 675                        card->running = 0;
 676                }
 677                spin_unlock(&card->dma_lock);
 678                snd_ps3_wait_for_dma_stop(card);
 679                break;
 680        default:
 681                break;
 682
 683        }
 684
 685        return ret;
 686};
 687
 688/*
 689 * report current pointer
 690 */
 691static snd_pcm_uframes_t snd_ps3_pcm_pointer(
 692        struct snd_pcm_substream *substream)
 693{
 694        struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
 695        size_t bytes;
 696        snd_pcm_uframes_t ret;
 697
 698        spin_lock(&card->dma_lock);
 699        {
 700                bytes = (size_t)(card->dma_last_transfer_vaddr[SND_PS3_CH_L] -
 701                                 card->dma_start_vaddr[SND_PS3_CH_L]);
 702        }
 703        spin_unlock(&card->dma_lock);
 704
 705        ret = bytes_to_frames(substream->runtime, bytes * 2);
 706
 707        return ret;
 708};
 709
 710/*
 711 * SPDIF status bits controls
 712 */
 713static int snd_ps3_spdif_mask_info(struct snd_kcontrol *kcontrol,
 714                                   struct snd_ctl_elem_info *uinfo)
 715{
 716        uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
 717        uinfo->count = 1;
 718        return 0;
 719}
 720
 721/* FIXME: ps3av_set_audio_mode() assumes only consumer mode */
 722static int snd_ps3_spdif_cmask_get(struct snd_kcontrol *kcontrol,
 723                                   struct snd_ctl_elem_value *ucontrol)
 724{
 725        memset(ucontrol->value.iec958.status, 0xff, 8);
 726        return 0;
 727}
 728
 729static int snd_ps3_spdif_pmask_get(struct snd_kcontrol *kcontrol,
 730                                   struct snd_ctl_elem_value *ucontrol)
 731{
 732        return 0;
 733}
 734
 735static int snd_ps3_spdif_default_get(struct snd_kcontrol *kcontrol,
 736                                     struct snd_ctl_elem_value *ucontrol)
 737{
 738        memcpy(ucontrol->value.iec958.status, ps3av_mode_cs_info, 8);
 739        return 0;
 740}
 741
 742static int snd_ps3_spdif_default_put(struct snd_kcontrol *kcontrol,
 743                                     struct snd_ctl_elem_value *ucontrol)
 744{
 745        if (memcmp(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8)) {
 746                memcpy(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8);
 747                return 1;
 748        }
 749        return 0;
 750}
 751
 752static struct snd_kcontrol_new spdif_ctls[] = {
 753        {
 754                .access = SNDRV_CTL_ELEM_ACCESS_READ,
 755                .iface = SNDRV_CTL_ELEM_IFACE_PCM,
 756                .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
 757                .info = snd_ps3_spdif_mask_info,
 758                .get = snd_ps3_spdif_cmask_get,
 759        },
 760        {
 761                .access = SNDRV_CTL_ELEM_ACCESS_READ,
 762                .iface = SNDRV_CTL_ELEM_IFACE_PCM,
 763                .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
 764                .info = snd_ps3_spdif_mask_info,
 765                .get = snd_ps3_spdif_pmask_get,
 766        },
 767        {
 768                .iface = SNDRV_CTL_ELEM_IFACE_PCM,
 769                .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
 770                .info = snd_ps3_spdif_mask_info,
 771                .get = snd_ps3_spdif_default_get,
 772                .put = snd_ps3_spdif_default_put,
 773        },
 774};
 775
 776static struct snd_pcm_ops snd_ps3_pcm_spdif_ops = {
 777        .open = snd_ps3_pcm_open,
 778        .close = snd_ps3_pcm_close,
 779        .ioctl = snd_pcm_lib_ioctl,
 780        .hw_params = snd_ps3_pcm_hw_params,
 781        .hw_free = snd_ps3_pcm_hw_free,
 782        .prepare = snd_ps3_pcm_prepare,
 783        .trigger = snd_ps3_pcm_trigger,
 784        .pointer = snd_ps3_pcm_pointer,
 785};
 786
 787
 788static int __devinit snd_ps3_map_mmio(void)
 789{
 790        the_card.mapped_mmio_vaddr =
 791                ioremap(the_card.ps3_dev->m_region->bus_addr,
 792                        the_card.ps3_dev->m_region->len);
 793
 794        if (!the_card.mapped_mmio_vaddr) {
 795                pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n",
 796                       __func__, the_card.ps3_dev->m_region->lpar_addr,
 797                       the_card.ps3_dev->m_region->len);
 798                return -ENXIO;
 799        }
 800
 801        return 0;
 802};
 803
 804static void snd_ps3_unmap_mmio(void)
 805{
 806        iounmap(the_card.mapped_mmio_vaddr);
 807        the_card.mapped_mmio_vaddr = NULL;
 808}
 809
 810static int __devinit snd_ps3_allocate_irq(void)
 811{
 812        int ret;
 813        u64 lpar_addr, lpar_size;
 814        u64 __iomem *mapped;
 815
 816        /* FIXME: move this to device_init (H/W probe) */
 817
 818        /* get irq outlet */
 819        ret = lv1_gpu_device_map(1, &lpar_addr, &lpar_size);
 820        if (ret) {
 821                pr_info("%s: device map 1 failed %d\n", __func__,
 822                        ret);
 823                return -ENXIO;
 824        }
 825
 826        mapped = ioremap(lpar_addr, lpar_size);
 827        if (!mapped) {
 828                pr_info("%s: ioremap 1 failed \n", __func__);
 829                return -ENXIO;
 830        }
 831
 832        the_card.audio_irq_outlet = in_be64(mapped);
 833
 834        iounmap(mapped);
 835        ret = lv1_gpu_device_unmap(1);
 836        if (ret)
 837                pr_info("%s: unmap 1 failed\n", __func__);
 838
 839        /* irq */
 840        ret = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY,
 841                                 the_card.audio_irq_outlet,
 842                                 &the_card.irq_no);
 843        if (ret) {
 844                pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__, ret);
 845                return ret;
 846        }
 847
 848        ret = request_irq(the_card.irq_no, snd_ps3_interrupt, IRQF_DISABLED,
 849                          SND_PS3_DRIVER_NAME, &the_card);
 850        if (ret) {
 851                pr_info("%s: request_irq failed (%d)\n", __func__, ret);
 852                goto cleanup_irq;
 853        }
 854
 855        return 0;
 856
 857 cleanup_irq:
 858        ps3_irq_plug_destroy(the_card.irq_no);
 859        return ret;
 860};
 861
 862static void snd_ps3_free_irq(void)
 863{
 864        free_irq(the_card.irq_no, &the_card);
 865        ps3_irq_plug_destroy(the_card.irq_no);
 866}
 867
 868static void __devinit snd_ps3_audio_set_base_addr(uint64_t ioaddr_start)
 869{
 870        uint64_t val;
 871        int ret;
 872
 873        val = (ioaddr_start & (0x0fUL << 32)) >> (32 - 20) |
 874                (0x03UL << 24) |
 875                (0x0fUL << 12) |
 876                (PS3_AUDIO_IOID);
 877
 878        ret = lv1_gpu_attribute(0x100, 0x007, val, 0, 0);
 879        if (ret)
 880                pr_info("%s: gpu_attribute failed %d\n", __func__,
 881                        ret);
 882}
 883
 884static void __devinit snd_ps3_audio_fixup(struct snd_ps3_card_info *card)
 885{
 886        /*
 887         * avsetting driver seems to never change the followings
 888         * so, init them here once
 889         */
 890
 891        /* no dma interrupt needed */
 892        write_reg(PS3_AUDIO_INTR_EN_0, 0);
 893
 894        /* use every 4 buffer empty interrupt */
 895        update_mask_reg(PS3_AUDIO_AX_IC,
 896                        PS3_AUDIO_AX_IC_AASOIMD_MASK,
 897                        PS3_AUDIO_AX_IC_AASOIMD_EVERY4);
 898
 899        /* enable 3wire clocks */
 900        update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
 901                        ~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED |
 902                          PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED),
 903                        0);
 904        update_reg(PS3_AUDIO_AO_3WMCTRL,
 905                   PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT);
 906}
 907
 908static int __devinit snd_ps3_init_avsetting(struct snd_ps3_card_info *card)
 909{
 910        int ret;
 911        pr_debug("%s: start\n", __func__);
 912        card->avs.avs_audio_ch = PS3AV_CMD_AUDIO_NUM_OF_CH_2;
 913        card->avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
 914        card->avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
 915        card->avs.avs_audio_format = PS3AV_CMD_AUDIO_FORMAT_PCM;
 916        card->avs.avs_audio_source = PS3AV_CMD_AUDIO_SOURCE_SERIAL;
 917        memcpy(card->avs.avs_cs_info, ps3av_mode_cs_info, 8);
 918
 919        ret = snd_ps3_change_avsetting(card);
 920
 921        snd_ps3_audio_fixup(card);
 922
 923        /* to start to generate SPDIF signal, fill data */
 924        snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
 925        snd_ps3_kick_dma(card);
 926        pr_debug("%s: end\n", __func__);
 927        return ret;
 928}
 929
 930static int __devinit snd_ps3_driver_probe(struct ps3_system_bus_device *dev)
 931{
 932        int i, ret;
 933        u64 lpar_addr, lpar_size;
 934
 935        BUG_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1));
 936        BUG_ON(dev->match_id != PS3_MATCH_ID_SOUND);
 937
 938        the_card.ps3_dev = dev;
 939
 940        ret = ps3_open_hv_device(dev);
 941
 942        if (ret)
 943                return -ENXIO;
 944
 945        /* setup MMIO */
 946        ret = lv1_gpu_device_map(2, &lpar_addr, &lpar_size);
 947        if (ret) {
 948                pr_info("%s: device map 2 failed %d\n", __func__, ret);
 949                goto clean_open;
 950        }
 951        ps3_mmio_region_init(dev, dev->m_region, lpar_addr, lpar_size,
 952                PAGE_SHIFT);
 953
 954        ret = snd_ps3_map_mmio();
 955        if (ret)
 956                goto clean_dev_map;
 957
 958        /* setup DMA area */
 959        ps3_dma_region_init(dev, dev->d_region,
 960                            PAGE_SHIFT, /* use system page size */
 961                            0, /* dma type; not used */
 962                            NULL,
 963                            _ALIGN_UP(SND_PS3_DMA_REGION_SIZE, PAGE_SIZE));
 964        dev->d_region->ioid = PS3_AUDIO_IOID;
 965
 966        ret = ps3_dma_region_create(dev->d_region);
 967        if (ret) {
 968                pr_info("%s: region_create\n", __func__);
 969                goto clean_mmio;
 970        }
 971
 972        snd_ps3_audio_set_base_addr(dev->d_region->bus_addr);
 973
 974        /* CONFIG_SND_PS3_DEFAULT_START_DELAY */
 975        the_card.start_delay = snd_ps3_start_delay;
 976
 977        /* irq */
 978        if (snd_ps3_allocate_irq()) {
 979                ret = -ENXIO;
 980                goto clean_dma_region;
 981        }
 982
 983        /* create card instance */
 984        ret = snd_card_create(index, id, THIS_MODULE, 0, &the_card.card);
 985        if (ret < 0)
 986                goto clean_irq;
 987
 988        strcpy(the_card.card->driver, "PS3");
 989        strcpy(the_card.card->shortname, "PS3");
 990        strcpy(the_card.card->longname, "PS3 sound");
 991
 992        /* create control elements */
 993        for (i = 0; i < ARRAY_SIZE(spdif_ctls); i++) {
 994                ret = snd_ctl_add(the_card.card,
 995                                  snd_ctl_new1(&spdif_ctls[i], &the_card));
 996                if (ret < 0)
 997                        goto clean_card;
 998        }
 999
1000        /* create PCM devices instance */
1001        /* NOTE:this driver works assuming pcm:substream = 1:1 */
1002        ret = snd_pcm_new(the_card.card,
1003                          "SPDIF",
1004                          0, /* instance index, will be stored pcm.device*/
1005                          1, /* output substream */
1006                          0, /* input substream */
1007                          &(the_card.pcm));
1008        if (ret)
1009                goto clean_card;
1010
1011        the_card.pcm->private_data = &the_card;
1012        strcpy(the_card.pcm->name, "SPDIF");
1013
1014        /* set pcm ops */
1015        snd_pcm_set_ops(the_card.pcm, SNDRV_PCM_STREAM_PLAYBACK,
1016                        &snd_ps3_pcm_spdif_ops);
1017
1018        the_card.pcm->info_flags = SNDRV_PCM_INFO_NONINTERLEAVED;
1019        /* pre-alloc PCM DMA buffer*/
1020        ret = snd_pcm_lib_preallocate_pages_for_all(the_card.pcm,
1021                                        SNDRV_DMA_TYPE_DEV,
1022                                        &dev->core,
1023                                        SND_PS3_PCM_PREALLOC_SIZE,
1024                                        SND_PS3_PCM_PREALLOC_SIZE);
1025        if (ret < 0) {
1026                pr_info("%s: prealloc failed\n", __func__);
1027                goto clean_card;
1028        }
1029
1030        /*
1031         * allocate null buffer
1032         * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2
1033         * PAGE_SIZE is enogh
1034         */
1035        the_card.null_buffer_start_vaddr =
1036                dma_alloc_coherent(&the_card.ps3_dev->core,
1037                                   PAGE_SIZE,
1038                                   &the_card.null_buffer_start_dma_addr,
1039                                   GFP_KERNEL);
1040        if (!the_card.null_buffer_start_vaddr) {
1041                pr_info("%s: nullbuffer alloc failed\n", __func__);
1042                goto clean_preallocate;
1043        }
1044        pr_debug("%s: null vaddr=%p dma=%#llx\n", __func__,
1045                 the_card.null_buffer_start_vaddr,
1046                 the_card.null_buffer_start_dma_addr);
1047        /* set default sample rate/word width */
1048        snd_ps3_init_avsetting(&the_card);
1049
1050        /* register the card */
1051        snd_card_set_dev(the_card.card, &dev->core);
1052        ret = snd_card_register(the_card.card);
1053        if (ret < 0)
1054                goto clean_dma_map;
1055
1056        pr_info("%s started. start_delay=%dms\n",
1057                the_card.card->longname, the_card.start_delay);
1058        return 0;
1059
1060clean_dma_map:
1061        dma_free_coherent(&the_card.ps3_dev->core,
1062                          PAGE_SIZE,
1063                          the_card.null_buffer_start_vaddr,
1064                          the_card.null_buffer_start_dma_addr);
1065clean_preallocate:
1066        snd_pcm_lib_preallocate_free_for_all(the_card.pcm);
1067clean_card:
1068        snd_card_free(the_card.card);
1069clean_irq:
1070        snd_ps3_free_irq();
1071clean_dma_region:
1072        ps3_dma_region_free(dev->d_region);
1073clean_mmio:
1074        snd_ps3_unmap_mmio();
1075clean_dev_map:
1076        lv1_gpu_device_unmap(2);
1077clean_open:
1078        ps3_close_hv_device(dev);
1079        /*
1080         * there is no destructor function to pcm.
1081         * midlayer automatically releases if the card removed
1082         */
1083        return ret;
1084}; /* snd_ps3_probe */
1085
1086/* called when module removal */
1087static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev)
1088{
1089        int ret;
1090        pr_info("%s:start id=%d\n", __func__,  dev->match_id);
1091        if (dev->match_id != PS3_MATCH_ID_SOUND)
1092                return -ENXIO;
1093
1094        /*
1095         * ctl and preallocate buffer will be freed in
1096         * snd_card_free
1097         */
1098        ret = snd_card_free(the_card.card);
1099        if (ret)
1100                pr_info("%s: ctl freecard=%d\n", __func__, ret);
1101
1102        dma_free_coherent(&dev->core,
1103                          PAGE_SIZE,
1104                          the_card.null_buffer_start_vaddr,
1105                          the_card.null_buffer_start_dma_addr);
1106
1107        ps3_dma_region_free(dev->d_region);
1108
1109        snd_ps3_free_irq();
1110        snd_ps3_unmap_mmio();
1111
1112        lv1_gpu_device_unmap(2);
1113        ps3_close_hv_device(dev);
1114        pr_info("%s:end id=%d\n", __func__, dev->match_id);
1115        return 0;
1116} /* snd_ps3_remove */
1117
1118static struct ps3_system_bus_driver snd_ps3_bus_driver_info = {
1119        .match_id = PS3_MATCH_ID_SOUND,
1120        .probe = snd_ps3_driver_probe,
1121        .remove = snd_ps3_driver_remove,
1122        .shutdown = snd_ps3_driver_remove,
1123        .core = {
1124                .name = SND_PS3_DRIVER_NAME,
1125                .owner = THIS_MODULE,
1126        },
1127};
1128
1129
1130/*
1131 * module/subsystem initialize/terminate
1132 */
1133static int __init snd_ps3_init(void)
1134{
1135        int ret;
1136
1137        if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1138                return -ENXIO;
1139
1140        memset(&the_card, 0, sizeof(the_card));
1141        spin_lock_init(&the_card.dma_lock);
1142
1143        /* register systembus DRIVER, this calls our probe() func */
1144        ret = ps3_system_bus_driver_register(&snd_ps3_bus_driver_info);
1145
1146        return ret;
1147}
1148module_init(snd_ps3_init);
1149
1150static void __exit snd_ps3_exit(void)
1151{
1152        ps3_system_bus_driver_unregister(&snd_ps3_bus_driver_info);
1153}
1154module_exit(snd_ps3_exit);
1155
1156MODULE_LICENSE("GPL v2");
1157MODULE_DESCRIPTION("PS3 sound driver");
1158MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1159MODULE_ALIAS(PS3_MODULE_ALIAS_SOUND);
1160