Linux Audio

Check our new training course

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