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/gfp.h> 24#include <linux/init.h> 25#include <linux/interrupt.h> 26#include <linux/io.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("%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 721static int snd_ps3_spdif_cmask_get(struct snd_kcontrol *kcontrol, 722 struct snd_ctl_elem_value *ucontrol) 723{ 724 memset(ucontrol->value.iec958.status, 0xff, 8); 725 return 0; 726} 727 728static int snd_ps3_spdif_pmask_get(struct snd_kcontrol *kcontrol, 729 struct snd_ctl_elem_value *ucontrol) 730{ 731 return 0; 732} 733 734static int snd_ps3_spdif_default_get(struct snd_kcontrol *kcontrol, 735 struct snd_ctl_elem_value *ucontrol) 736{ 737 memcpy(ucontrol->value.iec958.status, ps3av_mode_cs_info, 8); 738 return 0; 739} 740 741static int snd_ps3_spdif_default_put(struct snd_kcontrol *kcontrol, 742 struct snd_ctl_elem_value *ucontrol) 743{ 744 if (memcmp(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8)) { 745 memcpy(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8); 746 return 1; 747 } 748 return 0; 749} 750 751static struct snd_kcontrol_new spdif_ctls[] = { 752 { 753 .access = SNDRV_CTL_ELEM_ACCESS_READ, 754 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 755 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), 756 .info = snd_ps3_spdif_mask_info, 757 .get = snd_ps3_spdif_cmask_get, 758 }, 759 { 760 .access = SNDRV_CTL_ELEM_ACCESS_READ, 761 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 762 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), 763 .info = snd_ps3_spdif_mask_info, 764 .get = snd_ps3_spdif_pmask_get, 765 }, 766 { 767 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 768 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), 769 .info = snd_ps3_spdif_mask_info, 770 .get = snd_ps3_spdif_default_get, 771 .put = snd_ps3_spdif_default_put, 772 }, 773}; 774 775static struct snd_pcm_ops snd_ps3_pcm_spdif_ops = { 776 .open = snd_ps3_pcm_open, 777 .close = snd_ps3_pcm_close, 778 .ioctl = snd_pcm_lib_ioctl, 779 .hw_params = snd_ps3_pcm_hw_params, 780 .hw_free = snd_ps3_pcm_hw_free, 781 .prepare = snd_ps3_pcm_prepare, 782 .trigger = snd_ps3_pcm_trigger, 783 .pointer = snd_ps3_pcm_pointer, 784}; 785 786 787static int __devinit snd_ps3_map_mmio(void) 788{ 789 the_card.mapped_mmio_vaddr = 790 ioremap(the_card.ps3_dev->m_region->bus_addr, 791 the_card.ps3_dev->m_region->len); 792 793 if (!the_card.mapped_mmio_vaddr) { 794 pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n", 795 __func__, the_card.ps3_dev->m_region->lpar_addr, 796 the_card.ps3_dev->m_region->len); 797 return -ENXIO; 798 } 799 800 return 0; 801}; 802 803static void snd_ps3_unmap_mmio(void) 804{ 805 iounmap(the_card.mapped_mmio_vaddr); 806 the_card.mapped_mmio_vaddr = NULL; 807} 808 809static int __devinit snd_ps3_allocate_irq(void) 810{ 811 int ret; 812 u64 lpar_addr, lpar_size; 813 u64 __iomem *mapped; 814 815 816 /* get irq outlet */ 817 ret = lv1_gpu_device_map(1, &lpar_addr, &lpar_size); 818 if (ret) { 819 pr_info("%s: device map 1 failed %d\n", __func__, 820 ret); 821 return -ENXIO; 822 } 823 824 mapped = ioremap(lpar_addr, lpar_size); 825 if (!mapped) { 826 pr_info("%s: ioremap 1 failed \n", __func__); 827 return -ENXIO; 828 } 829 830 the_card.audio_irq_outlet = in_be64(mapped); 831 832 iounmap(mapped); 833 ret = lv1_gpu_device_unmap(1); 834 if (ret) 835 pr_info("%s: unmap 1 failed\n", __func__); 836 837 /* irq */ 838 ret = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, 839 the_card.audio_irq_outlet, 840 &the_card.irq_no); 841 if (ret) { 842 pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__, ret); 843 return ret; 844 } 845 846 ret = request_irq(the_card.irq_no, snd_ps3_interrupt, IRQF_DISABLED, 847 SND_PS3_DRIVER_NAME, &the_card); 848 if (ret) { 849 pr_info("%s: request_irq failed (%d)\n", __func__, ret); 850 goto cleanup_irq; 851 } 852 853 return 0; 854 855 cleanup_irq: 856 ps3_irq_plug_destroy(the_card.irq_no); 857 return ret; 858}; 859 860static void snd_ps3_free_irq(void) 861{ 862 free_irq(the_card.irq_no, &the_card); 863 ps3_irq_plug_destroy(the_card.irq_no); 864} 865 866static void __devinit snd_ps3_audio_set_base_addr(uint64_t ioaddr_start) 867{ 868 uint64_t val; 869 int ret; 870 871 val = (ioaddr_start & (0x0fUL << 32)) >> (32 - 20) | 872 (0x03UL << 24) | 873 (0x0fUL << 12) | 874 (PS3_AUDIO_IOID); 875 876 ret = lv1_gpu_attribute(0x100, 0x007, val, 0, 0); 877 if (ret) 878 pr_info("%s: gpu_attribute failed %d\n", __func__, 879 ret); 880} 881 882static void __devinit snd_ps3_audio_fixup(struct snd_ps3_card_info *card) 883{ 884 /* 885 * avsetting driver seems to never change the followings 886 * so, init them here once 887 */ 888 889 /* no dma interrupt needed */ 890 write_reg(PS3_AUDIO_INTR_EN_0, 0); 891 892 /* use every 4 buffer empty interrupt */ 893 update_mask_reg(PS3_AUDIO_AX_IC, 894 PS3_AUDIO_AX_IC_AASOIMD_MASK, 895 PS3_AUDIO_AX_IC_AASOIMD_EVERY4); 896 897 /* enable 3wire clocks */ 898 update_mask_reg(PS3_AUDIO_AO_3WMCTRL, 899 ~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED | 900 PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED), 901 0); 902 update_reg(PS3_AUDIO_AO_3WMCTRL, 903 PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT); 904} 905 906static int __devinit snd_ps3_init_avsetting(struct snd_ps3_card_info *card) 907{ 908 int ret; 909 pr_debug("%s: start\n", __func__); 910 card->avs.avs_audio_ch = PS3AV_CMD_AUDIO_NUM_OF_CH_2; 911 card->avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K; 912 card->avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16; 913 card->avs.avs_audio_format = PS3AV_CMD_AUDIO_FORMAT_PCM; 914 card->avs.avs_audio_source = PS3AV_CMD_AUDIO_SOURCE_SERIAL; 915 memcpy(card->avs.avs_cs_info, ps3av_mode_cs_info, 8); 916 917 ret = snd_ps3_change_avsetting(card); 918 919 snd_ps3_audio_fixup(card); 920 921 /* to start to generate SPDIF signal, fill data */ 922 snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL); 923 snd_ps3_kick_dma(card); 924 pr_debug("%s: end\n", __func__); 925 return ret; 926} 927 928static int __devinit snd_ps3_driver_probe(struct ps3_system_bus_device *dev) 929{ 930 int i, ret; 931 u64 lpar_addr, lpar_size; 932 933 BUG_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1)); 934 BUG_ON(dev->match_id != PS3_MATCH_ID_SOUND); 935 936 the_card.ps3_dev = dev; 937 938 ret = ps3_open_hv_device(dev); 939 940 if (ret) 941 return -ENXIO; 942 943 /* setup MMIO */ 944 ret = lv1_gpu_device_map(2, &lpar_addr, &lpar_size); 945 if (ret) { 946 pr_info("%s: device map 2 failed %d\n", __func__, ret); 947 goto clean_open; 948 } 949 ps3_mmio_region_init(dev, dev->m_region, lpar_addr, lpar_size, 950 PAGE_SHIFT); 951 952 ret = snd_ps3_map_mmio(); 953 if (ret) 954 goto clean_dev_map; 955 956 /* setup DMA area */ 957 ps3_dma_region_init(dev, dev->d_region, 958 PAGE_SHIFT, /* use system page size */ 959 0, /* dma type; not used */ 960 NULL, 961 _ALIGN_UP(SND_PS3_DMA_REGION_SIZE, PAGE_SIZE)); 962 dev->d_region->ioid = PS3_AUDIO_IOID; 963 964 ret = ps3_dma_region_create(dev->d_region); 965 if (ret) { 966 pr_info("%s: region_create\n", __func__); 967 goto clean_mmio; 968 } 969 970 snd_ps3_audio_set_base_addr(dev->d_region->bus_addr); 971 972 /* CONFIG_SND_PS3_DEFAULT_START_DELAY */ 973 the_card.start_delay = snd_ps3_start_delay; 974 975 /* irq */ 976 if (snd_ps3_allocate_irq()) { 977 ret = -ENXIO; 978 goto clean_dma_region; 979 } 980 981 /* create card instance */ 982 ret = snd_card_create(index, id, THIS_MODULE, 0, &the_card.card); 983 if (ret < 0) 984 goto clean_irq; 985 986 strcpy(the_card.card->driver, "PS3"); 987 strcpy(the_card.card->shortname, "PS3"); 988 strcpy(the_card.card->longname, "PS3 sound"); 989 990 /* create control elements */ 991 for (i = 0; i < ARRAY_SIZE(spdif_ctls); i++) { 992 ret = snd_ctl_add(the_card.card, 993 snd_ctl_new1(&spdif_ctls[i], &the_card)); 994 if (ret < 0) 995 goto clean_card; 996 } 997 998 /* create PCM devices instance */ 999 /* NOTE:this driver works assuming pcm:substream = 1:1 */ 1000 ret = snd_pcm_new(the_card.card, 1001 "SPDIF", 1002 0, /* instance index, will be stored pcm.device*/ 1003 1, /* output substream */ 1004 0, /* input substream */ 1005 &(the_card.pcm)); 1006 if (ret) 1007 goto clean_card; 1008 1009 the_card.pcm->private_data = &the_card; 1010 strcpy(the_card.pcm->name, "SPDIF"); 1011 1012 /* set pcm ops */ 1013 snd_pcm_set_ops(the_card.pcm, SNDRV_PCM_STREAM_PLAYBACK, 1014 &snd_ps3_pcm_spdif_ops); 1015 1016 the_card.pcm->info_flags = SNDRV_PCM_INFO_NONINTERLEAVED; 1017 /* pre-alloc PCM DMA buffer*/ 1018 ret = snd_pcm_lib_preallocate_pages_for_all(the_card.pcm, 1019 SNDRV_DMA_TYPE_DEV, 1020 &dev->core, 1021 SND_PS3_PCM_PREALLOC_SIZE, 1022 SND_PS3_PCM_PREALLOC_SIZE); 1023 if (ret < 0) { 1024 pr_info("%s: prealloc failed\n", __func__); 1025 goto clean_card; 1026 } 1027 1028 /* 1029 * allocate null buffer 1030 * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2 1031 * PAGE_SIZE is enogh 1032 */ 1033 the_card.null_buffer_start_vaddr = 1034 dma_alloc_coherent(&the_card.ps3_dev->core, 1035 PAGE_SIZE, 1036 &the_card.null_buffer_start_dma_addr, 1037 GFP_KERNEL); 1038 if (!the_card.null_buffer_start_vaddr) { 1039 pr_info("%s: nullbuffer alloc failed\n", __func__); 1040 goto clean_preallocate; 1041 } 1042 pr_debug("%s: null vaddr=%p dma=%#llx\n", __func__, 1043 the_card.null_buffer_start_vaddr, 1044 the_card.null_buffer_start_dma_addr); 1045 /* set default sample rate/word width */ 1046 snd_ps3_init_avsetting(&the_card); 1047 1048 /* register the card */ 1049 snd_card_set_dev(the_card.card, &dev->core); 1050 ret = snd_card_register(the_card.card); 1051 if (ret < 0) 1052 goto clean_dma_map; 1053 1054 pr_info("%s started. start_delay=%dms\n", 1055 the_card.card->longname, the_card.start_delay); 1056 return 0; 1057 1058clean_dma_map: 1059 dma_free_coherent(&the_card.ps3_dev->core, 1060 PAGE_SIZE, 1061 the_card.null_buffer_start_vaddr, 1062 the_card.null_buffer_start_dma_addr); 1063clean_preallocate: 1064 snd_pcm_lib_preallocate_free_for_all(the_card.pcm); 1065clean_card: 1066 snd_card_free(the_card.card); 1067clean_irq: 1068 snd_ps3_free_irq(); 1069clean_dma_region: 1070 ps3_dma_region_free(dev->d_region); 1071clean_mmio: 1072 snd_ps3_unmap_mmio(); 1073clean_dev_map: 1074 lv1_gpu_device_unmap(2); 1075clean_open: 1076 ps3_close_hv_device(dev); 1077 /* 1078 * there is no destructor function to pcm. 1079 * midlayer automatically releases if the card removed 1080 */ 1081 return ret; 1082}; /* snd_ps3_probe */ 1083 1084/* called when module removal */ 1085static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev) 1086{ 1087 int ret; 1088 pr_info("%s:start id=%d\n", __func__, dev->match_id); 1089 if (dev->match_id != PS3_MATCH_ID_SOUND) 1090 return -ENXIO; 1091 1092 /* 1093 * ctl and preallocate buffer will be freed in 1094 * snd_card_free 1095 */ 1096 ret = snd_card_free(the_card.card); 1097 if (ret) 1098 pr_info("%s: ctl freecard=%d\n", __func__, ret); 1099 1100 dma_free_coherent(&dev->core, 1101 PAGE_SIZE, 1102 the_card.null_buffer_start_vaddr, 1103 the_card.null_buffer_start_dma_addr); 1104 1105 ps3_dma_region_free(dev->d_region); 1106 1107 snd_ps3_free_irq(); 1108 snd_ps3_unmap_mmio(); 1109 1110 lv1_gpu_device_unmap(2); 1111 ps3_close_hv_device(dev); 1112 pr_info("%s:end id=%d\n", __func__, dev->match_id); 1113 return 0; 1114} /* snd_ps3_remove */ 1115 1116static struct ps3_system_bus_driver snd_ps3_bus_driver_info = { 1117 .match_id = PS3_MATCH_ID_SOUND, 1118 .probe = snd_ps3_driver_probe, 1119 .remove = snd_ps3_driver_remove, 1120 .shutdown = snd_ps3_driver_remove, 1121 .core = { 1122 .name = SND_PS3_DRIVER_NAME, 1123 .owner = THIS_MODULE, 1124 }, 1125}; 1126 1127 1128/* 1129 * module/subsystem initialize/terminate 1130 */ 1131static int __init snd_ps3_init(void) 1132{ 1133 int ret; 1134 1135 if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) 1136 return -ENXIO; 1137 1138 memset(&the_card, 0, sizeof(the_card)); 1139 spin_lock_init(&the_card.dma_lock); 1140 1141 /* register systembus DRIVER, this calls our probe() func */ 1142 ret = ps3_system_bus_driver_register(&snd_ps3_bus_driver_info); 1143 1144 return ret; 1145} 1146module_init(snd_ps3_init); 1147 1148static void __exit snd_ps3_exit(void) 1149{ 1150 ps3_system_bus_driver_unregister(&snd_ps3_bus_driver_info); 1151} 1152module_exit(snd_ps3_exit); 1153 1154MODULE_LICENSE("GPL v2"); 1155MODULE_DESCRIPTION("PS3 sound driver"); 1156MODULE_AUTHOR("Sony Computer Entertainment Inc."); 1157MODULE_ALIAS(PS3_MODULE_ALIAS_SOUND); 1158