xref: /linux/sound/ppc/snd_ps3.c (revision 088e88be5a380cc4e81963a9a02815da465d144f)
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  */
38 static struct snd_ps3_card_info the_card;
39 
40 static int snd_ps3_start_delay = CONFIG_SND_PS3_DEFAULT_START_DELAY;
41 
42 module_param_named(start_delay, snd_ps3_start_delay, uint, 0644);
43 MODULE_PARM_DESC(start_delay, "time to insert silent data in ms");
44 
45 static int index = SNDRV_DEFAULT_IDX1;
46 static char *id = SNDRV_DEFAULT_STR1;
47 
48 module_param(index, int, 0444);
49 MODULE_PARM_DESC(index, "Index value for PS3 soundchip.");
50 module_param(id, charp, 0444);
51 MODULE_PARM_DESC(id, "ID string for PS3 soundchip.");
52 
53 
54 /*
55  * PS3 audio register access
56  */
57 static inline u32 read_reg(unsigned int reg)
58 {
59 	return in_be32(the_card.mapped_mmio_vaddr + reg);
60 }
61 static inline void write_reg(unsigned int reg, u32 val)
62 {
63 	out_be32(the_card.mapped_mmio_vaddr + reg, val);
64 }
65 static 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 }
70 static 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  */
79 static 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 
107 static 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  */
149 static 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 
178 static 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  */
189 static 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  */
200 static 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  */
216 static 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 		/* intentionally fall thru */
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 		/* intentionally fall thru */
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  */
300 static 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  */
367 static 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  */
376 static 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  */
438 static 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  */
511 static 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 
531 static int snd_ps3_pcm_close(struct snd_pcm_substream *substream)
532 {
533 	/* mute on */
534 	snd_ps3_mute(1);
535 	return 0;
536 };
537 
538 static int snd_ps3_pcm_hw_params(struct snd_pcm_substream *substream,
539 				 struct snd_pcm_hw_params *hw_params)
540 {
541 	size_t size;
542 
543 	/* alloc transport buffer */
544 	size = params_buffer_bytes(hw_params);
545 	snd_pcm_lib_malloc_pages(substream, size);
546 	return 0;
547 };
548 
549 static int snd_ps3_pcm_hw_free(struct snd_pcm_substream *substream)
550 {
551 	return snd_pcm_lib_free_pages(substream);
552 };
553 
554 static int snd_ps3_delay_to_bytes(struct snd_pcm_substream *substream,
555 				  unsigned int delay_ms)
556 {
557 	int ret;
558 	int rate ;
559 
560 	rate = substream->runtime->rate;
561 	ret = snd_pcm_format_size(substream->runtime->format,
562 				  rate * delay_ms / 1000)
563 		* substream->runtime->channels;
564 
565 	pr_debug("%s: time=%d rate=%d bytes=%ld, frames=%d, ret=%d\n",
566 		 __func__,
567 		 delay_ms,
568 		 rate,
569 		 snd_pcm_format_size(substream->runtime->format, rate),
570 		 rate * delay_ms / 1000,
571 		 ret);
572 
573 	return ret;
574 };
575 
576 static int snd_ps3_pcm_prepare(struct snd_pcm_substream *substream)
577 {
578 	struct snd_pcm_runtime *runtime = substream->runtime;
579 	struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
580 	unsigned long irqsave;
581 
582 	if (!snd_ps3_set_avsetting(substream)) {
583 		/* some parameter changed */
584 		write_reg(PS3_AUDIO_AX_IE,
585 			  PS3_AUDIO_AX_IE_ASOBEIE(0) |
586 			  PS3_AUDIO_AX_IE_ASOBUIE(0));
587 		/*
588 		 * let SPDIF device re-lock with SPDIF signal,
589 		 * start with some silence
590 		 */
591 		card->silent = snd_ps3_delay_to_bytes(substream,
592 						      card->start_delay) /
593 			(PS3_AUDIO_FIFO_STAGE_SIZE * 4); /* every 4 times */
594 	}
595 
596 	/* restart ring buffer pointer */
597 	spin_lock_irqsave(&card->dma_lock, irqsave);
598 	{
599 		card->dma_buffer_size = runtime->dma_bytes;
600 
601 		card->dma_last_transfer_vaddr[SND_PS3_CH_L] =
602 			card->dma_next_transfer_vaddr[SND_PS3_CH_L] =
603 			card->dma_start_vaddr[SND_PS3_CH_L] =
604 			runtime->dma_area;
605 		card->dma_start_bus_addr[SND_PS3_CH_L] = runtime->dma_addr;
606 
607 		card->dma_last_transfer_vaddr[SND_PS3_CH_R] =
608 			card->dma_next_transfer_vaddr[SND_PS3_CH_R] =
609 			card->dma_start_vaddr[SND_PS3_CH_R] =
610 			runtime->dma_area + (runtime->dma_bytes / 2);
611 		card->dma_start_bus_addr[SND_PS3_CH_R] =
612 			runtime->dma_addr + (runtime->dma_bytes / 2);
613 
614 		pr_debug("%s: vaddr=%p bus=%#llx\n", __func__,
615 			 card->dma_start_vaddr[SND_PS3_CH_L],
616 			 card->dma_start_bus_addr[SND_PS3_CH_L]);
617 
618 	}
619 	spin_unlock_irqrestore(&card->dma_lock, irqsave);
620 
621 	/* ensure the hardware sees the change */
622 	mb();
623 
624 	return 0;
625 };
626 
627 static int snd_ps3_pcm_trigger(struct snd_pcm_substream *substream,
628 			       int cmd)
629 {
630 	struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
631 
632 	switch (cmd) {
633 	case SNDRV_PCM_TRIGGER_START:
634 		/* clear outstanding interrupts  */
635 		update_reg(PS3_AUDIO_AX_IS, 0);
636 
637 		spin_lock(&card->dma_lock);
638 		{
639 			card->running = 1;
640 		}
641 		spin_unlock(&card->dma_lock);
642 
643 		snd_ps3_program_dma(card,
644 				    SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
645 		snd_ps3_kick_dma(card);
646 		while (read_reg(PS3_AUDIO_KICK(7)) &
647 		       PS3_AUDIO_KICK_STATUS_MASK) {
648 			udelay(1);
649 		}
650 		snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
651 		snd_ps3_kick_dma(card);
652 		break;
653 
654 	case SNDRV_PCM_TRIGGER_STOP:
655 		spin_lock(&card->dma_lock);
656 		{
657 			card->running = 0;
658 		}
659 		spin_unlock(&card->dma_lock);
660 		snd_ps3_wait_for_dma_stop(card);
661 		break;
662 	default:
663 		break;
664 
665 	}
666 
667 	return 0;
668 };
669 
670 /*
671  * report current pointer
672  */
673 static snd_pcm_uframes_t snd_ps3_pcm_pointer(
674 	struct snd_pcm_substream *substream)
675 {
676 	struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
677 	size_t bytes;
678 	snd_pcm_uframes_t ret;
679 
680 	spin_lock(&card->dma_lock);
681 	{
682 		bytes = (size_t)(card->dma_last_transfer_vaddr[SND_PS3_CH_L] -
683 				 card->dma_start_vaddr[SND_PS3_CH_L]);
684 	}
685 	spin_unlock(&card->dma_lock);
686 
687 	ret = bytes_to_frames(substream->runtime, bytes * 2);
688 
689 	return ret;
690 };
691 
692 /*
693  * SPDIF status bits controls
694  */
695 static int snd_ps3_spdif_mask_info(struct snd_kcontrol *kcontrol,
696 				   struct snd_ctl_elem_info *uinfo)
697 {
698 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
699 	uinfo->count = 1;
700 	return 0;
701 }
702 
703 /* FIXME: ps3av_set_audio_mode() assumes only consumer mode */
704 static int snd_ps3_spdif_cmask_get(struct snd_kcontrol *kcontrol,
705 				   struct snd_ctl_elem_value *ucontrol)
706 {
707 	memset(ucontrol->value.iec958.status, 0xff, 8);
708 	return 0;
709 }
710 
711 static int snd_ps3_spdif_pmask_get(struct snd_kcontrol *kcontrol,
712 				   struct snd_ctl_elem_value *ucontrol)
713 {
714 	return 0;
715 }
716 
717 static int snd_ps3_spdif_default_get(struct snd_kcontrol *kcontrol,
718 				     struct snd_ctl_elem_value *ucontrol)
719 {
720 	memcpy(ucontrol->value.iec958.status, ps3av_mode_cs_info, 8);
721 	return 0;
722 }
723 
724 static int snd_ps3_spdif_default_put(struct snd_kcontrol *kcontrol,
725 				     struct snd_ctl_elem_value *ucontrol)
726 {
727 	if (memcmp(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8)) {
728 		memcpy(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8);
729 		return 1;
730 	}
731 	return 0;
732 }
733 
734 static struct snd_kcontrol_new spdif_ctls[] = {
735 	{
736 		.access = SNDRV_CTL_ELEM_ACCESS_READ,
737 		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
738 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
739 		.info = snd_ps3_spdif_mask_info,
740 		.get = snd_ps3_spdif_cmask_get,
741 	},
742 	{
743 		.access = SNDRV_CTL_ELEM_ACCESS_READ,
744 		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
745 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
746 		.info = snd_ps3_spdif_mask_info,
747 		.get = snd_ps3_spdif_pmask_get,
748 	},
749 	{
750 		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
751 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
752 		.info = snd_ps3_spdif_mask_info,
753 		.get = snd_ps3_spdif_default_get,
754 		.put = snd_ps3_spdif_default_put,
755 	},
756 };
757 
758 static const struct snd_pcm_ops snd_ps3_pcm_spdif_ops = {
759 	.open = snd_ps3_pcm_open,
760 	.close = snd_ps3_pcm_close,
761 	.ioctl = snd_pcm_lib_ioctl,
762 	.hw_params = snd_ps3_pcm_hw_params,
763 	.hw_free = snd_ps3_pcm_hw_free,
764 	.prepare = snd_ps3_pcm_prepare,
765 	.trigger = snd_ps3_pcm_trigger,
766 	.pointer = snd_ps3_pcm_pointer,
767 };
768 
769 
770 static int snd_ps3_map_mmio(void)
771 {
772 	the_card.mapped_mmio_vaddr =
773 		ioremap(the_card.ps3_dev->m_region->bus_addr,
774 			the_card.ps3_dev->m_region->len);
775 
776 	if (!the_card.mapped_mmio_vaddr) {
777 		pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n",
778 		       __func__, the_card.ps3_dev->m_region->lpar_addr,
779 		       the_card.ps3_dev->m_region->len);
780 		return -ENXIO;
781 	}
782 
783 	return 0;
784 };
785 
786 static void snd_ps3_unmap_mmio(void)
787 {
788 	iounmap(the_card.mapped_mmio_vaddr);
789 	the_card.mapped_mmio_vaddr = NULL;
790 }
791 
792 static int snd_ps3_allocate_irq(void)
793 {
794 	int ret;
795 	u64 lpar_addr, lpar_size;
796 	u64 __iomem *mapped;
797 
798 	/* FIXME: move this to device_init (H/W probe) */
799 
800 	/* get irq outlet */
801 	ret = lv1_gpu_device_map(1, &lpar_addr, &lpar_size);
802 	if (ret) {
803 		pr_info("%s: device map 1 failed %d\n", __func__,
804 			ret);
805 		return -ENXIO;
806 	}
807 
808 	mapped = ioremap(lpar_addr, lpar_size);
809 	if (!mapped) {
810 		pr_info("%s: ioremap 1 failed \n", __func__);
811 		return -ENXIO;
812 	}
813 
814 	the_card.audio_irq_outlet = in_be64(mapped);
815 
816 	iounmap(mapped);
817 	ret = lv1_gpu_device_unmap(1);
818 	if (ret)
819 		pr_info("%s: unmap 1 failed\n", __func__);
820 
821 	/* irq */
822 	ret = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY,
823 				 the_card.audio_irq_outlet,
824 				 &the_card.irq_no);
825 	if (ret) {
826 		pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__, ret);
827 		return ret;
828 	}
829 
830 	ret = request_irq(the_card.irq_no, snd_ps3_interrupt, 0,
831 			  SND_PS3_DRIVER_NAME, &the_card);
832 	if (ret) {
833 		pr_info("%s: request_irq failed (%d)\n", __func__, ret);
834 		goto cleanup_irq;
835 	}
836 
837 	return 0;
838 
839  cleanup_irq:
840 	ps3_irq_plug_destroy(the_card.irq_no);
841 	return ret;
842 };
843 
844 static void snd_ps3_free_irq(void)
845 {
846 	free_irq(the_card.irq_no, &the_card);
847 	ps3_irq_plug_destroy(the_card.irq_no);
848 }
849 
850 static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start)
851 {
852 	uint64_t val;
853 	int ret;
854 
855 	val = (ioaddr_start & (0x0fUL << 32)) >> (32 - 20) |
856 		(0x03UL << 24) |
857 		(0x0fUL << 12) |
858 		(PS3_AUDIO_IOID);
859 
860 	ret = lv1_gpu_attribute(0x100, 0x007, val);
861 	if (ret)
862 		pr_info("%s: gpu_attribute failed %d\n", __func__,
863 			ret);
864 }
865 
866 static void snd_ps3_audio_fixup(struct snd_ps3_card_info *card)
867 {
868 	/*
869 	 * avsetting driver seems to never change the following
870 	 * so, init them here once
871 	 */
872 
873 	/* no dma interrupt needed */
874 	write_reg(PS3_AUDIO_INTR_EN_0, 0);
875 
876 	/* use every 4 buffer empty interrupt */
877 	update_mask_reg(PS3_AUDIO_AX_IC,
878 			PS3_AUDIO_AX_IC_AASOIMD_MASK,
879 			PS3_AUDIO_AX_IC_AASOIMD_EVERY4);
880 
881 	/* enable 3wire clocks */
882 	update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
883 			~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED |
884 			  PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED),
885 			0);
886 	update_reg(PS3_AUDIO_AO_3WMCTRL,
887 		   PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT);
888 }
889 
890 static int snd_ps3_init_avsetting(struct snd_ps3_card_info *card)
891 {
892 	int ret;
893 	pr_debug("%s: start\n", __func__);
894 	card->avs.avs_audio_ch = PS3AV_CMD_AUDIO_NUM_OF_CH_2;
895 	card->avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
896 	card->avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
897 	card->avs.avs_audio_format = PS3AV_CMD_AUDIO_FORMAT_PCM;
898 	card->avs.avs_audio_source = PS3AV_CMD_AUDIO_SOURCE_SERIAL;
899 	memcpy(card->avs.avs_cs_info, ps3av_mode_cs_info, 8);
900 
901 	ret = snd_ps3_change_avsetting(card);
902 
903 	snd_ps3_audio_fixup(card);
904 
905 	/* to start to generate SPDIF signal, fill data */
906 	snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
907 	snd_ps3_kick_dma(card);
908 	pr_debug("%s: end\n", __func__);
909 	return ret;
910 }
911 
912 static int snd_ps3_driver_probe(struct ps3_system_bus_device *dev)
913 {
914 	int i, ret;
915 	u64 lpar_addr, lpar_size;
916 	static u64 dummy_mask;
917 
918 	if (WARN_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1)))
919 		return -ENODEV;
920 	if (WARN_ON(dev->match_id != PS3_MATCH_ID_SOUND))
921 		return -ENODEV;
922 
923 	the_card.ps3_dev = dev;
924 
925 	ret = ps3_open_hv_device(dev);
926 
927 	if (ret)
928 		return -ENXIO;
929 
930 	/* setup MMIO */
931 	ret = lv1_gpu_device_map(2, &lpar_addr, &lpar_size);
932 	if (ret) {
933 		pr_info("%s: device map 2 failed %d\n", __func__, ret);
934 		goto clean_open;
935 	}
936 	ps3_mmio_region_init(dev, dev->m_region, lpar_addr, lpar_size,
937 		PAGE_SHIFT);
938 
939 	ret = snd_ps3_map_mmio();
940 	if (ret)
941 		goto clean_dev_map;
942 
943 	/* setup DMA area */
944 	ps3_dma_region_init(dev, dev->d_region,
945 			    PAGE_SHIFT, /* use system page size */
946 			    0, /* dma type; not used */
947 			    NULL,
948 			    _ALIGN_UP(SND_PS3_DMA_REGION_SIZE, PAGE_SIZE));
949 	dev->d_region->ioid = PS3_AUDIO_IOID;
950 
951 	ret = ps3_dma_region_create(dev->d_region);
952 	if (ret) {
953 		pr_info("%s: region_create\n", __func__);
954 		goto clean_mmio;
955 	}
956 
957 	dummy_mask = DMA_BIT_MASK(32);
958 	dev->core.dma_mask = &dummy_mask;
959 	dma_set_coherent_mask(&dev->core, dummy_mask);
960 
961 	snd_ps3_audio_set_base_addr(dev->d_region->bus_addr);
962 
963 	/* CONFIG_SND_PS3_DEFAULT_START_DELAY */
964 	the_card.start_delay = snd_ps3_start_delay;
965 
966 	/* irq */
967 	if (snd_ps3_allocate_irq()) {
968 		ret = -ENXIO;
969 		goto clean_dma_region;
970 	}
971 
972 	/* create card instance */
973 	ret = snd_card_new(&dev->core, index, id, THIS_MODULE,
974 			   0, &the_card.card);
975 	if (ret < 0)
976 		goto clean_irq;
977 
978 	strcpy(the_card.card->driver, "PS3");
979 	strcpy(the_card.card->shortname, "PS3");
980 	strcpy(the_card.card->longname, "PS3 sound");
981 
982 	/* create control elements */
983 	for (i = 0; i < ARRAY_SIZE(spdif_ctls); i++) {
984 		ret = snd_ctl_add(the_card.card,
985 				  snd_ctl_new1(&spdif_ctls[i], &the_card));
986 		if (ret < 0)
987 			goto clean_card;
988 	}
989 
990 	/* create PCM devices instance */
991 	/* NOTE:this driver works assuming pcm:substream = 1:1 */
992 	ret = snd_pcm_new(the_card.card,
993 			  "SPDIF",
994 			  0, /* instance index, will be stored pcm.device*/
995 			  1, /* output substream */
996 			  0, /* input substream */
997 			  &(the_card.pcm));
998 	if (ret)
999 		goto clean_card;
1000 
1001 	the_card.pcm->private_data = &the_card;
1002 	strcpy(the_card.pcm->name, "SPDIF");
1003 
1004 	/* set pcm ops */
1005 	snd_pcm_set_ops(the_card.pcm, SNDRV_PCM_STREAM_PLAYBACK,
1006 			&snd_ps3_pcm_spdif_ops);
1007 
1008 	the_card.pcm->info_flags = SNDRV_PCM_INFO_NONINTERLEAVED;
1009 	/* pre-alloc PCM DMA buffer*/
1010 	snd_pcm_lib_preallocate_pages_for_all(the_card.pcm,
1011 					SNDRV_DMA_TYPE_DEV,
1012 					&dev->core,
1013 					SND_PS3_PCM_PREALLOC_SIZE,
1014 					SND_PS3_PCM_PREALLOC_SIZE);
1015 
1016 	/*
1017 	 * allocate null buffer
1018 	 * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2
1019 	 * PAGE_SIZE is enogh
1020 	 */
1021 	the_card.null_buffer_start_vaddr =
1022 		dma_alloc_coherent(&the_card.ps3_dev->core,
1023 				   PAGE_SIZE,
1024 				   &the_card.null_buffer_start_dma_addr,
1025 				   GFP_KERNEL);
1026 	if (!the_card.null_buffer_start_vaddr) {
1027 		pr_info("%s: nullbuffer alloc failed\n", __func__);
1028 		ret = -ENOMEM;
1029 		goto clean_card;
1030 	}
1031 	pr_debug("%s: null vaddr=%p dma=%#llx\n", __func__,
1032 		 the_card.null_buffer_start_vaddr,
1033 		 the_card.null_buffer_start_dma_addr);
1034 	/* set default sample rate/word width */
1035 	snd_ps3_init_avsetting(&the_card);
1036 
1037 	/* register the card */
1038 	ret = snd_card_register(the_card.card);
1039 	if (ret < 0)
1040 		goto clean_dma_map;
1041 
1042 	pr_info("%s started. start_delay=%dms\n",
1043 		the_card.card->longname, the_card.start_delay);
1044 	return 0;
1045 
1046 clean_dma_map:
1047 	dma_free_coherent(&the_card.ps3_dev->core,
1048 			  PAGE_SIZE,
1049 			  the_card.null_buffer_start_vaddr,
1050 			  the_card.null_buffer_start_dma_addr);
1051 clean_card:
1052 	snd_card_free(the_card.card);
1053 clean_irq:
1054 	snd_ps3_free_irq();
1055 clean_dma_region:
1056 	ps3_dma_region_free(dev->d_region);
1057 clean_mmio:
1058 	snd_ps3_unmap_mmio();
1059 clean_dev_map:
1060 	lv1_gpu_device_unmap(2);
1061 clean_open:
1062 	ps3_close_hv_device(dev);
1063 	/*
1064 	 * there is no destructor function to pcm.
1065 	 * midlayer automatically releases if the card removed
1066 	 */
1067 	return ret;
1068 }; /* snd_ps3_probe */
1069 
1070 /* called when module removal */
1071 static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev)
1072 {
1073 	int ret;
1074 	pr_info("%s:start id=%d\n", __func__,  dev->match_id);
1075 	if (dev->match_id != PS3_MATCH_ID_SOUND)
1076 		return -ENXIO;
1077 
1078 	/*
1079 	 * ctl and preallocate buffer will be freed in
1080 	 * snd_card_free
1081 	 */
1082 	ret = snd_card_free(the_card.card);
1083 	if (ret)
1084 		pr_info("%s: ctl freecard=%d\n", __func__, ret);
1085 
1086 	dma_free_coherent(&dev->core,
1087 			  PAGE_SIZE,
1088 			  the_card.null_buffer_start_vaddr,
1089 			  the_card.null_buffer_start_dma_addr);
1090 
1091 	ps3_dma_region_free(dev->d_region);
1092 
1093 	snd_ps3_free_irq();
1094 	snd_ps3_unmap_mmio();
1095 
1096 	lv1_gpu_device_unmap(2);
1097 	ps3_close_hv_device(dev);
1098 	pr_info("%s:end id=%d\n", __func__, dev->match_id);
1099 	return 0;
1100 } /* snd_ps3_remove */
1101 
1102 static struct ps3_system_bus_driver snd_ps3_bus_driver_info = {
1103 	.match_id = PS3_MATCH_ID_SOUND,
1104 	.probe = snd_ps3_driver_probe,
1105 	.remove = snd_ps3_driver_remove,
1106 	.shutdown = snd_ps3_driver_remove,
1107 	.core = {
1108 		.name = SND_PS3_DRIVER_NAME,
1109 		.owner = THIS_MODULE,
1110 	},
1111 };
1112 
1113 
1114 /*
1115  * module/subsystem initialize/terminate
1116  */
1117 static int __init snd_ps3_init(void)
1118 {
1119 	int ret;
1120 
1121 	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1122 		return -ENXIO;
1123 
1124 	memset(&the_card, 0, sizeof(the_card));
1125 	spin_lock_init(&the_card.dma_lock);
1126 
1127 	/* register systembus DRIVER, this calls our probe() func */
1128 	ret = ps3_system_bus_driver_register(&snd_ps3_bus_driver_info);
1129 
1130 	return ret;
1131 }
1132 module_init(snd_ps3_init);
1133 
1134 static void __exit snd_ps3_exit(void)
1135 {
1136 	ps3_system_bus_driver_unregister(&snd_ps3_bus_driver_info);
1137 }
1138 module_exit(snd_ps3_exit);
1139 
1140 MODULE_LICENSE("GPL v2");
1141 MODULE_DESCRIPTION("PS3 sound driver");
1142 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1143 MODULE_ALIAS(PS3_MODULE_ALIAS_SOUND);
1144