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