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